本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3
本文基于前两篇文章eureka-server和eureka-client的实现。
参考
eureka-server
eureka-client
<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>
spring: application: name: eureka-feign server: port: 8601 eureka: instance: hostname: localhost lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 10 client: service-url: defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
package spring.cloud.demo.eurekafeign; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableDiscoveryClient @EnableFeignClients @SpringBootApplication public class EurekaFeignApplication { public static void main(String[] args) { SpringApplication.run(EurekaFeignApplication.class, args); } }
@EnableDiscoveryClient:这里使用EnableDiscoveryClient注解,在 eureka-client 已说明,这边就不在阐述
@EnableFeignClients:启用Feign客户端
package spring.cloud.demo.eurekafeign.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; /** * @auther: maomao * @DateT: 2019-09-17 */ @FeignClient(value = "eureka-client") public interface EurekaFeignService { @RequestMapping(value = "/info") String syaHello(); }
@FeignClient:定义Feign客户端,调用远程client。eureka-client代表client的spring.application.name,调用远程地址示例:http://eureka-client/info
package spring.cloud.demo.eurekafeign.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import spring.cloud.demo.eurekafeign.service.EurekaFeignService; import javax.annotation.Resource; /** * @auther: maomao * @DateT: 2019-09-17 */ @RestController @RequestMapping("/feign") public class EurekaFeignController { @Resource private EurekaFeignService eurekaFeignService; @RequestMapping("/sayHello") public String sayHello() { return "feign result: " + eurekaFeignService.syaHello(); } }
可以在eureka-server服务注册中心看到eureka-feign是否注册成功。
红框中内容代表eureka-feign已经正常启动并成功注册到eureka-server服务注册中心。
访问http://localhost:8601/feign/sayHello,显示如下:
多刷新几次可以在浏览器中看到不通的结果,端口是变化的。
Feign默认的负载均衡策略是轮询方式。如果想修改Feign的负载均衡策略请参考 eureka-ribbon 中的配置
至此,一个简单的单机Feign服务消费者工程就搭建完成了。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
feign: hystrix: enabled: true
在@FeignClient注解中增加fallback
package spring.cloud.demo.eurekafeign.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; /** * @auther: maomao * @DateT: 2019-09-17 */ @FeignClient(value = "eureka-client", fallback = EurekaFeignServiceFailure.class) public interface EurekaFeignService { @RequestMapping(value = "/info") String syaHello(); }
增加EurekaFeignServiceFailure类:
package spring.cloud.demo.eurekafeign.service; import org.springframework.stereotype.Service; /** * @auther: maomao * @DateT: 2019-09-17 */ @Service public class EurekaFeignServiceFailure implements EurekaFeignService { @Override public String syaHello() { return "网络繁忙,请稍后在试"; } }
首先在eureka-client全不启动的情况,访问http://localhost:8601/feign/sayHello看是否全不正常,然后停掉其中一个eureka-client,在多次刷新http://localhost:8601/feign/sayHello,显示如下:
说明增加的Hystrix已经生效。
至此,Feign集成Hystrix熔断机制全部完成。
本文主要简单实现了feign作为服务消费的简单应用和Hystrix的集成。
gitHub地址