Greenwich.SR2版本的Spring Cloud Feign实例
前面我们了解了Spring Cloud Ribbon和Hystrix,在使用上它们基本上会成队出现,那么是不是可以把它们组合起来使用?而且我们发现,在服务消费方a-beautiful-client里通过REST调用服务提供方时,会有很多RestTemplate的代码,这些重复代码能否简化掉呢?答案是肯定的,Spring Cloud为我们提供了Feign,就是整合了Ribbon和Hystrix,并且提供了一种声明式的Web服务客户端定义方式,让我们只需要定义好REST接口即可,服务消费方无需再写实现了。
我们这次新增一个a-feign-client吧,把它跟a-beautiful-client(参见Greenwich.SR2版本的Spring Cloud Eureka实例)来做一番比较。三板斧祭出:
1、pom里我们去掉了ribbon和hystrix,只需引入openfeign即可:
<?xml version="1.0" encoding="UTF-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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>feign</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、application:
#本机端口 server.port=8764
#本机服务名
spring.application.name=a-feign-client
#服务提供方实例地址 app.service.url=http://A-BOOTIFUL-CLIENT/ #注册中心地址 eureka.client.service-url.defaultZone=http://localhost:8888/eureka/ #开启熔断 feign.hystrix.enabled=true #负载均衡配置 a-bootiful-client.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
主类我们用@EnableFeignClients代替了@EnableCircuitBreaker和@LoadBalance:
package hello; import hello.service.ConsumerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.web.bind.annotation.*; import java.util.List; @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class FeignApplication { public static void main(String[] args) { SpringApplication.run(FeignApplication.class, args); } } @RestController class ServiceInstanceRestController { @Autowired private ConsumerService consumerService; @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/consumer/sayHi") public String sayHi(@RequestParam(value = "name") String name, @RequestParam(value = "accessToken", required = false) String accessToken) { return name + " say hi: " + consumerService.hello(name); } @RequestMapping("/service-instances/{applicationName}") public List<ServiceInstance> serviceInstancesByApplicationName( @PathVariable String applicationName) { return this.discoveryClient.getInstances(applicationName); } }
3、我们改写一下接口,再新增一个熔断的服务降级类:
package hello.service; import hello.service.impl.BackUPCallHi; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Component @FeignClient(name = "a-bootiful-client", fallback = BackUPCallHi.class) public interface ConsumerService { @RequestMapping("/hello") String hello(@RequestParam(value = "name") String name); }
package hello.service.impl; import hello.service.ConsumerService; import org.springframework.stereotype.Component; @Component public class BackUPCallHi implements ConsumerService { @Override public String hello(String name) { return "I'm feign hystrix."; } }
搞定,可以看到原来a-beautiful-client能干的事情,a-feign-client也做到了:
负载均衡:
熔断:
上面我们看到url后面多了一个accessToken的参数,这个其实是给网关鉴权用的,详见Greenwich.SR2版本的Spring Cloud Zuul实例。