Spring Cloud 之 Feign Client服务消费(六)

上一篇讲了Ribbon的使用,会发现其实Ribbon的使用还是比较繁琐的,其实微服务系统内部调用一般都是通过Feign完成调用的。本篇讲一下Feign。

1、创建一个module名称为x-demo-service-ribbon的ribbon服务

2、build.gradle中加入依赖

1 dependencies {
2     compile("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
3     compile("org.springframework.cloud:spring-cloud-starter-openfeign")
4 }

3、创建Service层

注意第7行value配置,必须和x-demo-service中定义的接口path一致

1 /**
2  * @author Leo
3  */
4 @FeignClient(value = "x-demo-service")
5 public interface FeignClientDemoService {
6 
7     @RequestMapping(value = "demo/service", method = RequestMethod.GET)
8     String service();
9 }

4、创建Controller层

 1 @RestController
 2 @RequestMapping("feign")
 3 public class FeignClientDemoController {
 4 
 5     @Autowired
 6     FeignClientDemoService feignClientDemoService;
 7 
 8     @RequestMapping(value = "service", method = RequestMethod.GET)
 9     public String service() {
10         return feignClientDemoService.service();
11     }
12 }

5、bootstrap.yml配置

 1 spring:
 2   application:
 3     name: x-demo-service-feign
 4 
 5 server:
 6   port: 8092
 7 
 8 eureka:
 9   client:
10     serviceUrl:
11       defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/

6、创建启动类

 1 /**
 2  * @author Leo
 3  */
 4 @SpringBootApplication
 5 @EnableEurekaClient
 6 @EnableFeignClients
 7 public class FeignServerApplication {
 8 
 9     public static void main(String[] args) {
10         SpringApplication.run(FeignServerApplication.class, args);
11     }
12 }

7、启动服务,验证Feign调用

浏览器中输入:http://localhost:8092/feign/service,多次刷新后会返回如下结果:

x-demo-service hello.8081

x-demo-service hello.8082

x-demo-service hello.8083

至此,说明我们的Feign调用已经完成。通过同一个接口访问,会访问到8081,8082,8083三个不同的服务提供者。

同时可以注意到后台日志输出:

2021-02-23 13:50:12.655  INFO 54776 --- [nio-8092-exec-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client x-demo-service initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=x-demo-service,current list of Servers=[windows10.microdone.cn:8082, windows10.microdone.cn:8081, windows10.microdone.cn:8083],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;    Instance count:3;    Active connections count: 0;    Circuit breaker tripped count: 0;    Active connections per server: 0.0;]
},Server stats: [[Server:windows10.microdone.cn:8082;    Zone:defaultZone;    Total Requests:0;    Successive connection failure:0;    Total blackout seconds:0;    Last connection made:Thu Jan 01 08:00:00 CST 1970;    First connection made: Thu Jan 01 08:00:00 CST 1970;    Active Connections:0;    total failure count in last (1000) msecs:0;    average resp time:0.0;    90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;    max resp time:0.0;    stddev resp time:0.0]
, [Server:windows10.microdone.cn:8083;    Zone:defaultZone;    Total Requests:0;    Successive connection failure:0;    Total blackout seconds:0;    Last connection made:Thu Jan 01 08:00:00 CST 1970;    First connection made: Thu Jan 01 08:00:00 CST 1970;    Active Connections:0;    total failure count in last (1000) msecs:0;    average resp time:0.0;    90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;    max resp time:0.0;    stddev resp time:0.0]
, [Server:windows10.microdone.cn:8081;    Zone:defaultZone;    Total Requests:0;    Successive connection failure:0;    Total blackout seconds:0;    Last connection made:Thu Jan 01 08:00:00 CST 1970;    First connection made: Thu Jan 01 08:00:00 CST 1970;    Active Connections:0;    total failure count in last (1000) msecs:0;    average resp time:0.0;    90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;    max resp time:0.0;    stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@7655c176

看到这个日志,是不是想起了Ribbon,没错!Feign底层就是封装了Ribbon实现的负载均衡,而Feign只是一种声明式的服务调用。

原文地址:https://www.cnblogs.com/shileibrave/p/14435678.html