Ribbon负载均衡及Feign消费者调用服务

 

微服务调用Ribbon 

简介

前面讲了eureka服务注册与发现,但是结合eureka集群的服务调用没讲。

这里的话 就要用到Ribbon,结合eureka,来实现服务的调用;

 

Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端的行为。

为Ribbon配置服务提供者地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求。Ribbon默认为我们提供了很多负载均衡算法,例如轮询、随机等。当然,我们也可为Ribbon实现自定义的负载均衡算法。

Spring Cloud中,当Ribbon与Eureka配合使用时,Ribbon可自动从Eureka Server获取服务提供者地址列表,并基于负载均衡算法,请求其中一个服务提供者实例。展示了Ribbon与Eureka配合使用时的架构。

 

 

初步应用

Ribbon是客户端负载均衡,所以肯定集成再消费端,也就是consumer端

我们修改microservice-student-consumer-80

首先,引入依赖,pom.xml 加入 ribbon相关依赖

 1 <!--ribbon相关依赖-->
 2         <dependency>
 3             <groupId>org.springframework.cloud</groupId>
 4             <artifactId>spring-cloud-starter-eureka</artifactId>
 5         </dependency>
 6         <dependency>
 7             <groupId>org.springframework.cloud</groupId>
 8             <artifactId>spring-cloud-starter-ribbon</artifactId>
 9         </dependency>
10         <dependency>
11             <groupId>org.springframework.cloud</groupId>
12             <artifactId>spring-cloud-starter-config</artifactId>
13         </dependency>

 

application.yml加

1 server:
2   port: 81
3   context-path: /
4 eureka:
5   client:
6     service-url:
7       defaultZone: http://eureka2001.yuan.com:2001/eureka/,http://eureka2002.yuan.com:2002/eureka/,http://eureka2003.yuan.com:2003/eureka/
8     register-with-eureka: false

ribbon结合eureka来调用服务提供者;

 

SpringCloudConfig也改成 要加个负载均衡配置 @LoadBalanced

 1 package com.yuan.microservicestudentconsumer80.config;
 2 
 3 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.web.client.RestTemplate;
 7 
 8 @Configuration
 9 public class SpringCloudConfig {
10 
11     @LoadBalanced     // 引入ribbon负载均衡
12     @Bean
13     public RestTemplate getRestTemplate(){
14         return new RestTemplate();
15     }
16 }

因为和eureka整合,所以启动类StudentConsumerApplication_80 加个注解 @EnableEurekaClient

 1 package com.yuan.microservicestudentconsumer80;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 6 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
 7 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 8 
 9 @EnableEurekaClient
10 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
11 public class MicroserviceStudentConsumer80Application {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(MicroserviceStudentConsumer80Application.class, args);
15     }
16 
17 }

这里还有一个,要修改下StudentConsumerController的PRE_HOST,改成指定的微服务应用名称;

当然这里要先在服务提供者microservice-student-provider-1001的application.yml加下配置,指定下应用名称:

application:

    name: microservice-student

 

我们的微服务应用名称是 microservice-student

所以服务调用者这边的控制器里PRE_HOST改成 http://MICROSERVICE-STUDENT即可;

MICROSERVICE-STUDENT为Eureka注册中心的应用名称

microservicestudentconsumer80/controller/StudentConsumerController

 

上面配置好后,我们可以测试下;

先启动三个eureka,然后再启动服务提供者,再启动服务消费者;

 

 

 

结果就出来了,说明配置OK;

 

Ribbon负载均衡

按照它microservice-student-provider-1001建立一个microservice-student-provider子项目,然后将microservice-student-provider-1001这个子项目干掉;

 

前面搭建了初步例子,但是还没实现真正负载均衡,我们这里要先搞三个服务提供者集群,然后才能演示负载均衡,以及负载均衡策略;

 

新建项目microservice-student-provider-1002,microservice-student-provider-1003

pom.xml,application.yml,以及java类都复制一份,启动类名称对应的改下;

yml配置文件有两处要对应的改下,port端口改下,以及服务实例名称改下;

pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.yuan</groupId>
 7         <artifactId>t226microservice</artifactId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <artifactId>microservice-student-provider</artifactId>
11 
12     <properties>
13         <java.version>1.8</java.version>
14     </properties>
15     <dependencies>
16         <dependency>
17             <groupId>org.springframework.boot</groupId>
18             <artifactId>spring-boot-starter-web</artifactId>
19         </dependency>
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-test</artifactId>
23             <scope>test</scope>
24         </dependency>
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-data-jpa</artifactId>
28         </dependency>
29         <dependency>
30             <groupId>mysql</groupId>
31             <artifactId>mysql-connector-java</artifactId>
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-tomcat</artifactId>
36         </dependency>
37         <dependency>
38             <groupId>com.alibaba</groupId>
39             <artifactId>druid-spring-boot-starter</artifactId>
40         </dependency>
41         <!--  修改后立即生效,热部署  -->
42         <dependency>
43             <groupId>org.springframework</groupId>
44             <artifactId>springloaded</artifactId>
45         </dependency>
46         <dependency>
47             <groupId>org.springframework.boot</groupId>
48             <artifactId>spring-boot-devtools</artifactId>
49         </dependency>
50         <dependency>
51             <groupId>com.yuan</groupId>
52             <artifactId>microservice-common</artifactId>
53             <version>1.0-SNAPSHOT</version>
54             <scope>compile</scope>
55         </dependency>
56         <!--添加注册中心Eureka相关配置-->
57         <dependency>
58             <groupId>org.springframework.cloud</groupId>
59             <artifactId>spring-cloud-starter-eureka</artifactId>
60         </dependency>
61         <dependency>
62             <groupId>org.springframework.cloud</groupId>
63             <artifactId>spring-cloud-starter-config</artifactId>
64         </dependency>
65         <!-- actuator监控引入 -->
66         <dependency>
67             <groupId>org.springframework.boot</groupId>
68             <artifactId>spring-boot-starter-actuator</artifactId>
69         </dependency>
70         <dependency>
71             <groupId>com.yuan</groupId>
72             <artifactId>microservice-student-provider-1001</artifactId>
73             <version>1.0-SNAPSHOT</version>
74             <scope>compile</scope>
75         </dependency>
76 
77     </dependencies>
78     <build>
79         <plugins>
80             <plugin>
81                 <groupId>org.springframework.boot</groupId>
82                 <artifactId>spring-boot-maven-plugin</artifactId>
83             </plugin>
84         </plugins>
85     </build>
86 
87 </project>

 

application.yml

  1 ---
  2 server:
  3   port: 1001
  4   context-path: /
  5 spring:
  6   datasource:
  7     type: com.alibaba.druid.pool.DruidDataSource
  8     driver-class-name: com.mysql.jdbc.Driver
  9     url: jdbc:mysql://localhost:3306/j2ee?useUnicode=true&characterEncoding=utf8
 10     username: root
 11     password: 12345
 12   jpa:
 13     hibernate:
 14       ddl-auto: update
 15     show-sql: true
 16   application:
 17     name: microservice-student
 18   profiles: provider-1001
 19 
 20 eureka:
 21   instance:
 22     hostname: localhost
 23     appname: microservice-student
 24     instance-id: microservice-student:1001
 25     prefer-ip-address: true
 26   client:
 27     service-url:
 28       defaultZone: http://eureka2001.yuan.com:2001/eureka/,http://eureka2002.yuan.com:2002/eureka/,http://eureka2003.yuan.com:2003/eureka/
 29 
 30 info:
 31   groupId: com.yuan.testSpringcloud
 32   artifactId: microservice-student-provider-1001
 33   version: 1.0-SNAPSHOT
 34   userName: http://yuan.com
 35   phone: 123456
 36 
 37 ---
 38 server:
 39   port: 1002
 40   context-path: /
 41 spring:
 42   datasource:
 43     type: com.alibaba.druid.pool.DruidDataSource
 44     driver-class-name: com.mysql.jdbc.Driver
 45     url: jdbc:mysql://localhost:3306/j2ee?useUnicode=true&characterEncoding=utf8
 46     username: root
 47     password: 12345
 48   jpa:
 49     hibernate:
 50       ddl-auto: update
 51     show-sql: true
 52   application:
 53     name: microservice-student
 54   profiles: provider-1002
 55 
 56 eureka:
 57   instance:
 58     hostname: localhost
 59     appname: microservice-student
 60     instance-id: microservice-student:1002
 61     prefer-ip-address: true
 62   client:
 63     service-url:
 64       defaultZone: http://eureka2001.yuan.com:2001/eureka/,http://eureka2002.yuan.com:2002/eureka/,http://eureka2003.yuan.com:2003/eureka/
 65 
 66 info:
 67   groupId: com.yuan.testSpringcloud
 68   artifactId: microservice-student-provider-1002
 69   version: 1.0-SNAPSHOT
 70   userName: http://yuan.com
 71   phone: 123456
 72 
 73 ---
 74 server:
 75   port: 1003
 76   context-path: /
 77 spring:
 78   datasource:
 79     type: com.alibaba.druid.pool.DruidDataSource
 80     driver-class-name: com.mysql.jdbc.Driver
 81     url: jdbc:mysql://localhost:3306/j2ee?useUnicode=true&characterEncoding=utf8
 82     username: root
 83     password: 12345
 84   jpa:
 85     hibernate:
 86       ddl-auto: update
 87     show-sql: true
 88   application:
 89     name: microservice-student
 90   profiles: provider-1003
 91 
 92 eureka:
 93   instance:
 94     hostname: localhost
 95     appname: microservice-student
 96     instance-id: microservice-student:1003
 97     prefer-ip-address: true
 98   client:
 99     service-url:
100       defaultZone: http://eureka2001.yuan.com:2001/eureka/,http://eureka2002.yuan.com:2002/eureka/,http://eureka2003.yuan.com:2003/eureka/
101 
102 info:
103   groupId: com.yuan.testSpringcloud
104   artifactId: microservice-student-provider-1003
105   version: 1.0-SNAPSHOT
106   userName: http://yuan.com
107   phone: 123456

 

启动类MicroserviceStudentProviderApplication

 1 package com.yuan.microservicestudentprovider;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.autoconfigure.domain.EntityScan;
 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 7 
 8 @EntityScan("com.yuan.*.*")
 9 @EnableEurekaClient
10 @SpringBootApplication
11 public class MicroserviceStudentProviderApplication {
12 
13     public static void main(String[] args) {
14         SpringApplication.run(MicroserviceStudentProviderApplication.class, args);
15     }
16 
17 }

StudentProviderController

 1 package com.yuan.microservicestudentprovider.controller;
 2 
 3 import com.yuan.microservicecommon.entity.Student;
 4 import com.yuan.microservicestudentprovider.service.StudentService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.beans.factory.annotation.Value;
 7 import org.springframework.web.bind.annotation.*;
 8 
 9 import java.util.List;
10 
11 @RestController
12 @RequestMapping("/student")
13 public class StudentProviderController {
14  
15     @Autowired
16     private StudentService studentService;
17 
18     @Value("${server.port}")
19     private String port;
20      
21     @PostMapping(value="/save")
22     public boolean save(Student student){
23         try{
24             studentService.save(student);  
25             return true;
26         }catch(Exception e){
27             return false;
28         }
29     }
30      
31     @GetMapping(value="/list")
32     public List<Student> list(){
33         return studentService.list();
34     }
35      
36     @GetMapping(value="/get/{id}")
37     public Student get(@PathVariable("id") Integer id){
38         return studentService.findById(id);
39     }
40      
41     @GetMapping(value="/delete/{id}")
42     public boolean delete(@PathVariable("id") Integer id){
43         try{
44             studentService.delete(id);
45             return true;
46         }catch(Exception e){
47             return false;
48         }
49     }
50 
51     @RequestMapping("/ribbon")
52     public String ribbon(){
53         return "工号【"+port+"】正在为您服务";
54     }
55 
56 }

StudentConsumerController   80消费端口

 1 package com.yuan.microservicestudentconsumer80.controller;
 2 
 3 import com.yuan.microservicecommon.entity.Student;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.web.bind.annotation.*;
 6 import org.springframework.web.client.RestTemplate;
 7 
 8 import java.util.List;
 9 
10 @RestController
11 @RequestMapping("/student")
12 public class StudentConsumerController {
13 
14     private final static String SERVER_IP_PORT = "http://MICROSERVICE-STUDENT";
15  
16      @Autowired
17      private RestTemplate restTemplate;
18       
19      @PostMapping(value="/save")
20      private boolean save(Student student){
21          return restTemplate.postForObject(SERVER_IP_PORT+"/student/save", student, Boolean.class);
22      }
23       
24     @GetMapping(value="/list")
25     public List<Student> list(){
26         return restTemplate.getForObject(SERVER_IP_PORT+"/student/list", List.class);
27     }
28      
29     @GetMapping(value="/get/{id}")
30     public Student get(@PathVariable("id") Integer id){
31         return restTemplate.getForObject(SERVER_IP_PORT+"/student/get/"+id, Student.class);
32     }
33      
34     @GetMapping(value="/delete/{id}")
35     public boolean delete(@PathVariable("id") Integer id){
36         try{
37             restTemplate.getForObject(SERVER_IP_PORT+"/student/delete/"+id, Boolean.class);
38             return true;
39         }catch(Exception e){
40             return false;
41         }
42     }
43     @RequestMapping("/ribbon")
44     public String ribbon(){
45         return restTemplate.getForObject(SERVER_IP_PORT + "/student/ribbon", String.class);
46     }
47     
48 }

 

修改配置运行

1、选择Provider1001Application,点击最左上角复制按钮然后进行下一步操作==》

2、将用红线划中的地方修改

         如右边第一处:Provider1002Application

   第二处:同样

  第三处:provider-1002

 

 

 

启动顺序

1、启动2001、2001

2、启动1001、1002、1003

3、启动80

 

访问结果

 

 

停掉1001工号

再次访问到1001时就会报错,重新刷新页面就不会再出项1001工号。

 

 

Feign简介及应用

简介

声明式服务调用Feign简单介绍下;

Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。

我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。

Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

 

这段话看起来比较懵逼,这里说下实际使用,前面Ribbon调用服务提供者,我们通过restTemplate调用,

缺点是,多个地方调用,同一个请求要写多次,不方便统一维护,这时候Feign来了,就直接把请求统一搞一个service作为FeignClient,

然后其他调用Controller需要用到的,直接注入service,直接调用service方法即可;同时Feign整合了Ribbon和Eureka,所以要配置负载均衡的话,直接配置Ribbon即可,无其他特殊地方;当然Fiegn也整合了服务容错保护,断路器Hystrix,后面再说。

 

应用

1、在common项目里建一个service(实际项目肯定是多个service)作为Feign客户端,用Feign客户端来调用服务器提供者,当然可以配置负载均衡;Feign客户端定义的目的,就是为了方便给其他项目调用;

修改 microservice-common

pom.xml引入Feign依赖:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.yuan</groupId>
 7         <artifactId>t226microservice</artifactId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <artifactId>microservice-common</artifactId>
11 
12     <properties>
13         <java.version>1.8</java.version>
14     </properties>
15 
16     <dependencies>
17         <dependency>
18             <groupId>org.springframework.boot</groupId>
19             <artifactId>spring-boot-starter</artifactId>
20         </dependency>
21         <dependency>
22             <groupId>org.springframework.boot</groupId>
23             <artifactId>spring-boot-starter-test</artifactId>
24             <scope>test</scope>
25             <exclusions>
26                 <exclusion>
27                     <groupId>org.junit.vintage</groupId>
28                     <artifactId>junit-vintage-engine</artifactId>
29                 </exclusion>
30             </exclusions>
31         </dependency>
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-starter-data-jpa</artifactId>
35         </dependency>
36         <!--引入Feign依赖-->
37         <dependency>
38             <groupId>org.springframework.cloud</groupId>
39             <artifactId>spring-cloud-starter-feign</artifactId>
40         </dependency>
41     </dependencies>
42     <build>
43         <plugins>
44             <plugin>
45                 <groupId>org.springframework.boot</groupId>
46                 <artifactId>spring-boot-maven-plugin</artifactId>
47             </plugin>
48         </plugins>
49     </build>
50 
51 </project>

 

StudentClientService接口;

 1 package com.yuan.microservicecommon.service;
 2 
 3 import com.yuan.microservicecommon.entity.Student;
 4 import org.springframework.cloud.netflix.feign.FeignClient;
 5 import org.springframework.web.bind.annotation.GetMapping;
 6 import org.springframework.web.bind.annotation.PathVariable;
 7 import org.springframework.web.bind.annotation.PostMapping;
 8 import org.springframework.web.bind.annotation.RequestMapping;
 9 
10 import java.util.List;
11 
12 /**
13  * Student Feign接口客户端
14  * @author Administrator
15  *
16  */
17 @FeignClient(value="MICROSERVICE-STUDENT")
18 public interface StudentClientService {
19  
20     /**
21      * 根据id查询学生信息
22      * @param id
23      * @return
24      */
25     @GetMapping(value="/student/get/{id}")
26     public Student get(@PathVariable("id") Integer id);
27      
28     /**
29      * 查询学生信息
30      * @return
31      */
32     @GetMapping(value="/student/list")
33     public List<Student> list();
34      
35     /**
36      * 添加或者修改学生信息
37      * @param student
38      * @return
39      */
40     @PostMapping(value="/student/save")
41     public boolean save(Student student);
42      
43     /**
44      * 根据id删除学生信息
45      * @return
46      */
47     @GetMapping(value="/student/delete/{id}")
48     public boolean delete(@PathVariable("id") Integer id);
49 
50     @RequestMapping("/student/ribbon")
51     public String ribbon();
52 }

 

2、新建一个Feign消费者项目;

参考microservice-student-consumer-80建一个microservice-student-consumer-feign-80

代码都复制一份,包括pom.xml

 

pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>com.yuan</groupId>
 7         <artifactId>t226microservice</artifactId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <artifactId>microservice-student-consumer-feign-80</artifactId>
11 
12     <properties>
13         <java.version>1.8</java.version>
14     </properties>
15     <dependencies>
16         <dependency>
17             <groupId>org.springframework.boot</groupId>
18             <artifactId>spring-boot-starter-web</artifactId>
19         </dependency>
20         <dependency>
21             <groupId>org.springframework.boot</groupId>
22             <artifactId>spring-boot-starter-test</artifactId>
23             <scope>test</scope>
24         </dependency>
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-data-jpa</artifactId>
28         </dependency>
29         <dependency>
30             <groupId>mysql</groupId>
31             <artifactId>mysql-connector-java</artifactId>
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-tomcat</artifactId>
36         </dependency>
37         <!--  修改后立即生效,热部署  -->
38         <dependency>
39             <groupId>org.springframework</groupId>
40             <artifactId>springloaded</artifactId>
41         </dependency>
42         <dependency>
43             <groupId>org.springframework.boot</groupId>
44             <artifactId>spring-boot-devtools</artifactId>
45         </dependency>
46         <dependency>
47             <groupId>com.yuan</groupId>
48             <artifactId>microservice-common</artifactId>
49             <version>1.0-SNAPSHOT</version>
50             <scope>compile</scope>
51         </dependency>
52 
53         <!--ribbon相关依赖-->
54         <dependency>
55             <groupId>org.springframework.cloud</groupId>
56             <artifactId>spring-cloud-starter-eureka</artifactId>
57         </dependency>
58         <dependency>
59             <groupId>org.springframework.cloud</groupId>
60             <artifactId>spring-cloud-starter-ribbon</artifactId>
61         </dependency>
62         <dependency>
63             <groupId>org.springframework.cloud</groupId>
64             <artifactId>spring-cloud-starter-config</artifactId>
65         </dependency>
<!--引入Feign依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
66 
67     </dependencies>
68     <build>
69         <plugins>
70             <plugin>
71                 <groupId>org.springframework.boot</groupId>
72                 <artifactId>spring-boot-maven-plugin</artifactId>
73             </plugin>
74         </plugins>
75     </build>
76 
77 </project>

SpringCloudConfig.java

 1 package com.yuan.microservicestudentconsumerfeign80.config;
 2 
 3 import com.netflix.loadbalancer.IRule;
 4 import com.netflix.loadbalancer.RetryRule;
 5 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 6 import org.springframework.context.annotation.Bean;
 7 import org.springframework.context.annotation.Configuration;
 8 import org.springframework.web.client.RestTemplate;
 9 
10 @Configuration
11 public class SpringCloudConfig {
12 
13     @LoadBalanced
14     @Bean
15     public RestTemplate getRestTemplate(){
16         return new RestTemplate();
17     }
18 
19     /**
20      * 自定义调用规则(服务提供者掉线后不再调用,解决轮询问题)
21      * @return
22      */
23     @Bean
24     public IRule myRule(){
25         return new RetryRule();
26 //        return new RandomRule();
27     }
28 
29 }

 

添加yml文件配置

1 server:
2   port: 81
3   context-path: /
4 eureka:
5   client:
6     service-url:
7       defaultZone: http://eureka2001.yuan.com:2001/eureka/,http://eureka2002.yuan.com:2002/eureka/,http://eureka2003.yuan.com:2003/eureka/
8     register-with-eureka: false

 

3、修改启动类名称,和加注解

启动类名称改下,改成StudentConsumerFeignApplication_80,同时加个注解@EnableFeignClients

支持下Feign;

 1 package com.yuan.microservicestudentconsumerfeign80;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 6 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
 7 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 8 import org.springframework.cloud.netflix.feign.EnableFeignClients;
 9 
10 @EnableEurekaClient
11 @EnableFeignClients(value = "com.yuan.*.*")
12 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
13 public class MicroserviceStudentConsumerFeign80Application {
14 
15     public static void main(String[] args) {
16         SpringApplication.run(MicroserviceStudentConsumerFeign80Application.class, args);
17     }
18 
19 }

StudentConsumerController.java

 1 package com.yuan.microservicestudentconsumerfeign80.controller;
 2 
 3 import com.yuan.microservicecommon.entity.Student;
 4 import com.yuan.microservicecommon.service.StudentClientService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.*;
 7 import org.springframework.web.client.RestTemplate;
 8 
 9 import java.util.List;
10 
11 @RestController
12 @RequestMapping("/student")
13 public class StudentConsumerController {
14 
15     @Autowired
16     private StudentClientService studentClientService;
17 
18     @Autowired
19     private RestTemplate restTemplate;
20 
21     @PostMapping(value = "/save")
22     private boolean save(Student student) {
23         return studentClientService.save(student);
24     }
25 
26     @GetMapping(value = "/list")
27     public List<Student> list() {
28         return studentClientService.list();
29     }
30 
31     @GetMapping(value = "/get/{id}")
32     public Student get(@PathVariable("id") Integer id) {
33         return studentClientService.get(id);
34     }
35 
36     @GetMapping(value = "/delete/{id}")
37     public boolean delete(@PathVariable("id") Integer id) {
38         try {
39             studentClientService.delete(id);
40             return true;
41         } catch (Exception e) {
42             return false;
43         }
44     }
45 
46     @RequestMapping("/ribbon")
47     public String ribbon(){
48         return studentClientService.ribbon();
49     }
50 }

 

因为现在用Fiegn,所以把restTemplate去掉,改成注入service,调用service方法来实现服务的调用;

 

访问结果是没问题的,然后我们将1002服务断掉

 

再次访问时它不会出现1002服务,只有1001和1003。重新启动1002才可继续服务

 

谢谢观看!!!

 

posted @ 2019-12-07 18:25  Me*源  阅读(248)  评论(0编辑  收藏  举报