生产者:提供服务
消费者:消费服务配置中心:保存了生产者和消费者之间的契约关系所以,sprngcloud微服务的架构基础 :生产者(client),消费者(client),注册中心/配置中心(server)废话不多说,先看demo结构:,共需要四个项目
一,创建eurekaserver
1.创建maven项目eureka-server,打包方式jar包,
2.pom.xml如下,eureka依赖中内置了ribbon包,并且配置了jre版本为1.8,所有导入pom后先update项目,更新jdk为1.8:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.jorian</groupId> <artifactId>sc_eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 导入父工程 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath /> </parent> <!-- 设置jdk版本等信息 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <!-- 导入eureka依赖 --> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <!-- dependency management --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement></project>3.创建启动类,用于启动springboot项目,启动类一定创建在父级包目录,否则会 扫描不到某些类package com.jorian.sc_cloud;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer//表示是一个eurekaserver@SpringBootApplicationpublic class EurekaServerRunApp { public static void main(String[] args) { // TODO Auto-generated method stub SpringApplication.run(EurekaServerRunApp.class, args); }}4.创建springboot配置文件,名称一定为application.yml,格式为yml,因为在springboot的默认配置中扫描的就是application开头的配置文件,注意 :yml格式的文件有特殊要求,必须树形结构,不能有tab,具体请百度
其中的server-port为eureka服务的端口,一般默认8761,不需修改
security:
basic: enabled: true user: name: user password: password123server: port: 8761eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://user:password123@localhost:8761/eurekalogging: level:5.启动服务:执行main方法
6.在浏览器访问:localhost:8761,查看服务是否启动
启动成功界面
二,创建生产者1,
1.新建maven项目provider1,打包方式jar包修改pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.jorian</groupId> <artifactId>sc_eureka-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--boot eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- boot web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement></project>2.创建启动类,用于springboot项目启动:package com.jorian.sc_cloud;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient//表示是一个eurekaserver@SpringBootApplicationpublic class ProviderRunApp { public static void main(String[] args) { // TODO Auto-generated method stub SpringApplication.run(ProviderRunApp.class, args); }}3.修改application.yml配置文件port,application都可以自定义,defaultzone和eureka一致
server:
port: 7900spring: application: name: provider-usereureka: client: serviceUrl: defaultZone: http://user:password123@localhost:8761/eurekalogging: level: root: INFO4.创建controller,提供服务package com.jorian.sc_cloud.controller;
import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class provider_Controller { @RequestMapping("/hello") public String sayhello(){ return "i`m provider1,hello consumer!"; }}5.启动项目,不要关闭之前启动的eureka服务。执行main方法,参考上边
provier1启动后,查看之前的服务中心,刷新页面可以看到有一个provider-user(yml中配置的application-name)已经注册
三,创建消费者
1.创建maven项目consumer,打包方式jar包
2.修改pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jorian</groupId> <artifactId>sc_eureka-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement></project>3.创建启动类,用于启动springboot项目:package com.jorian.sc_cloud;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplication@EnableEurekaClientpublic class ConsumerRunApp { @Bean @LoadBalanced //Ribbon负载均衡 public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { // TODO Auto-generated method stub SpringApplication.run(ConsumerRunApp.class, args); }}4.创建消费类,调用后台的服务package com.jorian.sc_cloud.controller;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class HelloController { @Autowired private RestTemplate resttemplate; @RequestMapping("/hello") public String hello(){ String url="http://provider-user/hello"; //返回值类型和我们的业务返回值一致 return resttemplate.getForObject(url, String.class); }}5.修改yml文件,修改port,sapplication-name,不要和其他重复了
server:
port: 7902spring: application: name: consumer-helloeureka: client: serviceUrl: defaultZone: http://user:password123@localhost:8761/eurekalogging: level: root: INFO6.启动springboot服务,在eureka服务上查看,可以看到消费者已经注册
四,测试
在地址栏直接访问消费者地址,localhost:7900(看自己的地址多少),成功!
五,开启多个provider实例,
可以采用修改yml文件 中的端口号重复启动实例,也可以另外新建多个provider,修改其中的yml文件,依次启动。
由于eureka配置类ribbon负载均衡策略,消费者的请求会在客户端被决定好发送到哪台服务提供者进行处理。
开启多个实例如下;