一、整合步骤
1、根项目springcloud_study
(1)目录结构
(2)步骤
1 1、创建及配置Root项目springcloud_study
2 (1)在pom.xml进行依赖添加
3 <groupId>org.lkw</groupId>
4 <artifactId>springcloud_study</artifactId>
5 <packaging>pom</packaging>
6 <version>1.0-SNAPSHOT</version>
7 <modules>
8 <module>springcloud_study_eurekaserver</module>
9 <module>springcloud_study_helloservice</module>
10 <module>springcloud_study_feignconsumer</module>
11 <module>springcloud_study_ribbonconsumer</module>
12 </modules>
13
14 <!-- 引入继承SpringBoot框架父依赖 -->
15 <parent>
16 <groupId>org.springframework.boot</groupId>
17 <artifactId>spring-boot-starter-parent</artifactId>
18 <version>2.1.17.RELEASE</version>
19 <relativePath/>
20 </parent>
21
22
23 <properties>
24 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
26 <java.version>1.8</java.version>
27 <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
28 </properties>
29
30 <!-- 引入SpringBoot的测试依赖 -->
31 <dependencies>
32 <dependency>
33 <groupId>org.springframework.boot</groupId>
34 <artifactId>spring-boot-starter-test</artifactId>
35 <scope>provided</scope>
36 </dependency>
37 </dependencies>
38
39 <!-- 声明SpringCloud使用版本,但不自动导入依赖 -->
40 <dependencyManagement>
41 <dependencies>
42 <dependency>
43 <groupId>org.springframework.cloud</groupId>
44 <artifactId>spring-cloud-dependencies</artifactId>
45 <version>${spring-cloud.version}</version>
46 <type>pom</type>
47 <scope>import</scope>
48 </dependency>
49 </dependencies>
50 </dependencyManagement>
51
52 <build>
53 <plugins>
54 <plugin>
55 <groupId>org.springframework.boot</groupId>
56 <artifactId>spring-boot-maven-plugin</artifactId>
57 </plugin>
58 </plugins>
59 </build>
2、子模块springcloud_study_eurekaserver
(1)目录结构
(2)步骤
1 2、添加子模块Eureka server(注册中心)
2 (1)在pom.xml进行依赖添加
3 <parent>
4 <artifactId>springcloud_study</artifactId>
5 <groupId>org.lkw</groupId>
6 <version>1.0-SNAPSHOT</version>
7 </parent>
8 <modelVersion>4.0.0</modelVersion>
9
10 <artifactId>springcloud_study_eurekaserver</artifactId>
11
12 <!--eureka注册中心服务模块引入eureka依赖-->
13 <dependencies>
14 <dependency>
15 <groupId>org.springframework.cloud</groupId>
16 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
17 </dependency>
18 </dependencies>
19 (2)在application.properties进行服务配置
20 # 服务注册中心端口号
21 server.port=8761
22 # 服务注册中心主机名
23 eureka.instance.hostname=127.0.0.1
24 # 不向注册中心注册自己
25 eureka.client.register-with-eureka=false
26 # 不需要检索服务
27 eureka.client.fetch-registry=false
28 # 注册中心地址,会自动填充主机名和端口名
29 eureka.client.service-url.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
30 (3)创建Eureka项目的启动类
31 /**
32 * @author lkw
33 * eureka server启动类
34 */
35 @EnableEurekaServer
36 @SpringBootApplication
37 public class EurekaServerApplication {
38
39 public static void main(String[] args){
40 SpringApplication.run(EurekaServerApplication.class,args);
41 }
42 }
43 (4)启动项目,在浏览器输入ip:端口进行eureka注册中心访问
3、子模块springcloud_study_helloservice
(1)目录结构
(2)步骤
1 3、添加子模块Hello Service(创建服务方:服务提供者)
2 (1)在pom.xml进行依赖添加
3 <parent>
4 <artifactId>springcloud_study</artifactId>
5 <groupId>org.lkw</groupId>
6 <version>1.0-SNAPSHOT</version>
7 </parent>
8 <modelVersion>4.0.0</modelVersion>
9
10 <artifactId>springcloud_study_helloservice</artifactId>
11
12 <dependencies>
13 <dependency>
14 <groupId>org.springframework.cloud</groupId>
15 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
16 </dependency>
17 <dependency>
18 <groupId>org.springframework.boot</groupId>
19 <artifactId>spring-boot-starter-web</artifactId>
20 </dependency>
21 </dependencies>
22 (2)在application.yml进行服务配置
23 # 服务提供者配置
24 server:
25 #当前服务端口号
26 port: 8763
27 spring:
28 application:
29 #当前应用的名称
30 name: service-hello
31 eureka:
32 client:
33 serviceUrl:
34 #注册中心的地址
35 defaultZone: http://localhost:8761/eureka/
36 (3)添加Hello服务方的启动类
37 /**
38 * @author lkw
39 * Hello服务启动类
40 */
41 @SpringBootApplication
42 @EnableEurekaClient
43 public class HelloServiceApplication {
44
45 public static void main(String[] args){
46 SpringApplication.run(HelloServiceApplication.class,args);
47 }
48 }
49 (4)添加控制器进行测试
50 /**
51 * @author lkw
52 * Hello服务控制器
53 *
54 */
55 @RestController
56 public class HelloController {
57
58 @Value("${server.port}")
59 private String port;
60
61 @RequestMapping(value="hello",method = RequestMethod.GET)
62 public String index(){
63 return "eurekaClient: Hello World "+this.port;
64 }
65 }
66 (5)更改端口,进行多实例启动,在eureka注册中心查看是否注册了此多用例服务
67 多实例启动方法:先在yml配置文件里更改不同的端口,然后在Edit configurations里勾选Allow parallel run(允许平行运行),最后启动项目即可
4、子模块springcloud_study_feignconsumer
(1)目录结构
(2)步骤
1 4、添加子模块Feign consumer(基于Feign微服务框架实现消费者)
2 (1)在pom.xml引入相关依赖
3 <parent>
4 <artifactId>springcloud_study</artifactId>
5 <groupId>org.lkw</groupId>
6 <version>1.0-SNAPSHOT</version>
7 </parent>
8 <modelVersion>4.0.0</modelVersion>
9
10 <artifactId>springcloud_study_feignconsumer</artifactId>
11
12 <dependencies>
13 <dependency>
14 <groupId>org.springframework.cloud</groupId>
15 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
16 </dependency>
17 <dependency>
18 <groupId>org.springframework.boot</groupId>
19 <artifactId>spring-boot-starter-web</artifactId>
20 </dependency>
21 <!-- 引入feign远程调用框架依赖 -->
22 <dependency>
23 <groupId>org.springframework.cloud</groupId>
24 <artifactId>spring-cloud-starter-openfeign</artifactId>
25 </dependency>
26 </dependencies>
27 (2)在application.yml进行服务配置
28 # 服务调用者即消费者配置
29 server:
30 port: 8765
31 spring:
32 application:
33 # 该服务的名字
34 name: server-feign
35 eureka:
36 client:
37 serviceUrl:
38 # eureka服务的地址
39 defaultZone: http://localhost:8761/eureka/
40 (3)创建feign项目启动类
41 /*
42 *@author lkw
43 * 服务消费者启动类
44 */
45 @SpringBootApplication
46 @EnableEurekaClient
47 @EnableFeignClients
48 @EnableDiscoveryClient
49 public class FeignApplication {
50
51 public static void main(String[] args){
52 SpringApplication.run(FeignApplication.class,args);
53 }
54 }
55 (4)创建控制器
56 /**
57 * @author lkw
58 * Hello服务调用控制器
59 */
60 @RestController
61 public class HelloController {
62
63 @Autowired
64 private HelloFeign helloFeign;
65
66 @GetMapping(value="/hello")
67 public String getHello(){
68 return helloFeign.hello();
69 }
70
71
72 }
73 (5)创建feign服务进行远程方法调用
74 /**
75 * @author lkw
76 * 使用Feign对Hello服务进行远程调用(Feign自身封装了ribbon,所以已具备负载均衡功能)
77 *
78 */
79 @FeignClient(value="service-hello") //远程服务名
80 public interface HelloFeign {
81
82 @RequestMapping(value = "/hello") //远程服务方法
83 String hello();
84 }
85 (6)启动项目,在浏览器访问ip:端口/hello,多刷新几次,注意观察远程服务的端口变化即负载均衡是否生效
5、子模块springcloud_study_ribbonconsumer
(1)目录结构
(2)步骤
1 5、添加子模块Ribbon consumer(基于Ribbon微服务框架实现消费者)
2 (1)在pom.xml引入相关依赖
3 <parent>
4 <artifactId>springcloud_study</artifactId>
5 <groupId>org.lkw</groupId>
6 <version>1.0-SNAPSHOT</version>
7 </parent>
8 <modelVersion>4.0.0</modelVersion>
9
10 <artifactId>springcloud_study_ribbonconsumer</artifactId>
11
12 <dependencies>
13 <dependency>
14 <groupId>org.springframework.cloud</groupId>
15 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
16 </dependency>
17 <dependency>
18 <groupId>org.springframework.boot</groupId>
19 <artifactId>spring-boot-starter-web</artifactId>
20 </dependency>
21 <!-- 引入Ribbon负载均衡框架依赖 -->
22 <dependency>
23 <groupId>org.springframework.cloud</groupId>
24 <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
25 </dependency>
26 </dependencies>
27 (2)在appplication.yml进行服务配置
28 # 服务调用者即消费者配置
29 server:
30 port: 8764
31 spring:
32 application:
33 # 该服务的名字
34 name: server-ribbon
35 eureka:
36 client:
37 serviceUrl:
38 # eureka服务的地址
39 defaultZone: http://localhost:8761/eureka/
40 (3)创建Ribbon项目启动类
41 /**
42 * @author lkw
43 * 启动类:使用Ribbon实现消费者客户端对远程服务调用以及负载均衡
44 */
45 @SpringBootApplication
46 @EnableEurekaClient
47 @EnableDiscoveryClient
48 public class RibbonApplication {
49
50 public static void main(String[] args){
51 SpringApplication.run(RibbonApplication.class,args);
52 }
53
54 @Bean
55 @LoadBalanced //使RestTemplate具有负载均衡
56 public RestTemplate restTemplate(){
57 return new RestTemplate();
58 }
59 }
60 (4)创建控制器,进行远程服务调用
61 /**
62 * @author lkw
63 * Hello服务调用控制器
64 */
65 @RestController
66 public class HelloController {
67
68 @Autowired
69 private RestTemplate restTemplate;
70
71 @GetMapping(value="/ribbonHello")
72 public String getRibbonHello(){
73
74 String urlHelloService="http://service-hello/hello"; //远程服务及方法urk
75 String result=restTemplate.getForObject(urlHelloService,String.class);
76
77 System.out.println("使用ribbbon访问结果:"+result);
78 return "使用ribbbon访问结果:"+result;
79 }
80
81 }
82 (5)启动项目,在浏览器访问ip:端口/ribbonHello,注意观察远程服务的端口变化即负载均衡是否生效
二、FAQ(问题集锦)
三、成果
1、访问Eureka注册中心,初始时,服务列表为空
2、启动服务提供者HelloService,在注册中心的服务列表可以看到它的注册信息
3、启动消费者Feign,可以在注册中心的服务列表看到它的注册信息
4、使用Feign消费者方式,进行远程方法调用以及负载均衡
5、使用Ribbon消费者方式,进行远程方法调用以及负载均衡