基于SpringBoot整合SpringCloud微服务框架--Eureka注册中心及Feign远程调用/Ribbon负载均衡


一、整合步骤

1、根项目springcloud_study

(1)目录结构

(2)步骤

 1 1、创建及配置Root项目springcloud_study
 2 (1)在pom.xml进行依赖添加
 3     org.lkw
 4     springcloud_study
 5     pom
 6     1.0-SNAPSHOT
 7     
 8         springcloud_study_eurekaserver
 9         springcloud_study_helloservice
10         springcloud_study_feignconsumer
11         springcloud_study_ribbonconsumer
12     
13 
14     
15     
16         org.springframework.boot
17         spring-boot-starter-parent
18         2.1.17.RELEASE
19         
20     
21 
22 
23     
24         UTF-8
25         UTF-8
26         1.8
27         Greenwich.RELEASE
28     
29 
30     
31     
32         
33             org.springframework.boot
34             spring-boot-starter-test
35             provided
36         
37     
38 
39     
40     
41         
42             
43                 org.springframework.cloud
44                 spring-cloud-dependencies
45                 ${spring-cloud.version}
46                 pom
47                 import
48             
49         
50     
51 
52     
53         
54             
55                 org.springframework.boot
56                 spring-boot-maven-plugin
57             
58         
59     

2、子模块springcloud_study_eurekaserver

(1)目录结构

(2)步骤

 1 2、添加子模块Eureka server(注册中心)
 2 (1)在pom.xml进行依赖添加
 3     
 4         springcloud_study
 5         org.lkw
 6         1.0-SNAPSHOT
 7     
 8     4.0.0
 9 
10     springcloud_study_eurekaserver
11 
12     
13     
14         
15             org.springframework.cloud
16             spring-cloud-starter-netflix-eureka-server
17         
18     
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     
 4         springcloud_study
 5         org.lkw
 6         1.0-SNAPSHOT
 7     
 8     4.0.0
 9 
10     springcloud_study_helloservice
11 
12     
13         
14             org.springframework.cloud
15             spring-cloud-starter-netflix-eureka-client
16         
17         
18             org.springframework.boot
19             spring-boot-starter-web
20         
21     
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     
 4         springcloud_study
 5         org.lkw
 6         1.0-SNAPSHOT
 7     
 8     4.0.0
 9 
10     springcloud_study_feignconsumer
11 
12     
13         
14             org.springframework.cloud
15             spring-cloud-starter-netflix-eureka-client
16         
17         
18             org.springframework.boot
19             spring-boot-starter-web
20         
21         
22         
23             org.springframework.cloud
24             spring-cloud-starter-openfeign
25         
26     
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     
 4         springcloud_study
 5         org.lkw
 6         1.0-SNAPSHOT
 7     
 8     4.0.0
 9 
10     springcloud_study_ribbonconsumer
11 
12     
13         
14             org.springframework.cloud
15             spring-cloud-starter-netflix-eureka-client
16         
17         
18             org.springframework.boot
19             spring-boot-starter-web
20         
21         
22         
23             org.springframework.cloud
24             spring-cloud-starter-netflix-ribbon
25         
26     
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消费者方式,进行远程方法调用以及负载均衡