前言
本文采用 Spring cloud 本文为 2.1.8RELEASE,version=Greenwich.SR3
本文基于前两篇文章 eureka-server 和 eureka-client 的实现.
参考
- eureka-server https://juejin.im/post/5d8194a2e51d4561ea1a9506
- eureka-client https://juejin.im/post/5d82061be51d4561f95eeaf5
创建 Feign 工程
1.1 创建 sping boot 工程: eureka-feign
1.2 添加 pom.xml 相关依赖
- <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>
1.3 application 添加配置信息
- 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/
1.4 启动类 EurekaFeignApplication 增加注解
- 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 https://juejin.im/post/5d82061be51d4561f95eeaf5 已说明, 这边就不在阐述
@EnableFeignClients: 启用 Feign 客户端
1.5 创建服务接口 EurekaFeignService
- 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
1.6 创建 EurekaFeignController 控制类
- 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();
- }
- }
1.7 启动 eureka-feign 服务
可以在 eureka-server 服务注册中心看到 eureka-feign 是否注册成功.
红框中内容代表 eureka-feign 已经正常启动并成功注册到 eureka-server 服务注册中心.
访问 http://localhost:8601/feign/sayHello, 显示如下:
多刷新几次可以在浏览器中看到不通的结果, 端口是变化的.
Feign 默认的负载均衡策略是轮询方式. 如果想修改 Feign 的负载均衡策略请参考 eureka-ribbon https://juejin.im/post/5d82ef25f265da03f12e920f 中的配置
至此, 一个简单的单机 Feign 服务消费者工程就搭建完成了.
彩蛋
Feign 集成 Hystrix 熔断机制
pom.xml 增加依赖
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
- </dependency>
增加 application.YAML 配置
- feign:
- hystrix:
- enabled: true
修改 EurekaFeignService
在 @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-feign 服务
首先在 eureka-client 全不启动的情况, 访问 http://localhost:8601/feign/sayHello 看是否全不正常, 然后停掉其中一个 eureka-client, 在多次刷新 http://localhost:8601/feign/sayHello, 显示如下:
说明增加的 Hystrix 已经生效.
至此, Feign 集成 Hystrix 熔断机制全部完成.
总结
本文主要简单实现了 feign 作为服务消费的简单应用和 Hystrix 的集成.
代码地址
GitHub 地址
《Srping Cloud 2.X 小白教程》目录
spring cloud 2.x 版本 Eureka Server 服务注册中心教程 https://juejin.im/post/5d8194a2e51d4561ea1a9506
spring cloud 2.x 版本 Eureka Client 服务提供者教程 https://juejin.im/post/5d82061be51d4561f95eeaf5
spring cloud 2.x 版本 Ribbon 服务发现教程 (内含集成 Hystrix 熔断机制) https://juejin.im/post/5d82ef25f265da03f12e920f
spring cloud 2.x 版本 Feign 服务发现教程 (内含集成 Hystrix 熔断机制) https://juejin.im/post/5d82061be51d4561f95eeaf5
spring cloud 2.x 版本 Zuul 路由网关教程 https://juejin.im/post/5d8451bbf265da03da24cf7c
spring cloud 2.x 版本 config 分布式配置中心教程 https://juejin.im/post/5d8860cb6fb9a06aeb10fc3e
spring cloud 2.x 版本 Hystrix Dashboard 断路器教程 https://juejin.im/post/5d898a366fb9a06b0c08a6df
来源: https://www.cnblogs.com/fengfujie/p/11798197.html