本文共 5102 字,大约阅读时间需要 17 分钟。
本篇的 Feign 和 上一篇的 Ribbon 都是用于调用其他服务,但是调用的方式不同,Ribbon 需要搭配 RestTemplate 进行 Http 请求,步骤较为繁琐;Feign 是 Ribbon 的改进,整合了 Ribbon 和 Hystrix,不需要构建 Http 请求,并且采用接口的方式声明即可调用。
创建 maven 工程,添加核心的父 maven 依赖,所有子 pom 文件继承这个父 pom 文件。
UTF-8 1.8 1.8 Finchley.RELEASE org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE junit junit 4.11 test org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import
添加 maven 依赖
com.hly 03-spring-cloud-feign 1.0-SNAPSHOT UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-web
application.yml
server: port: 8761eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/spring: application: name: eureka-server
SpringBoot 启动类
@SpringBootApplication@EnableEurekaServerpublic class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}
添加 maven 依赖
com.hly 03-spring-cloud-feign 1.0-SNAPSHOT UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web
添加配置信息 application.yml
server: port: 8762spring: application: name: eureka-clienteureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
SpringBoot 启动类
@SpringBootApplication@RestController@EnableEurekaClientpublic class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } @Value("${server.port}") String port; @RequestMapping("/hello") public String home(@RequestParam(value = "name", defaultValue = "hly") String name) { return "hi " + name + " ,i am from port:" + port; }}
添加 maven 依赖
com.hly 03-spring-cloud-feign 1.0-SNAPSHOT UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-openfeign
添加 application.yml 配置
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/server: port: 8765spring: application: name: service-feign # 设置feign客户端超时时间feign: client: config: default: connectTimeout: 1 readTimeout: 1 # feign: # hystrix: #enabled : true
SpringBoot 启动类
@SpringBootApplication@EnableEurekaClient//让注册中心扫描到,EnableEurekaClient只适用于 eureka,@EnableDiscoveryClient 可以是其他注册中心@EnableDiscoveryClient@EnableFeignClientspublic class EurekaFeignApplication { public static void main(String[] args) { SpringApplication.run(EurekaFeignApplication.class, args); }}
服务层接口
//服务名指定调用哪个服务@FeignClient(value = "eureka-client")public interface ServiceHi { //这里的映射名和需要调用的服务的映射名一样 @RequestMapping(value = "/hello",method = RequestMethod.GET) String sayHiFromClientOne(@RequestParam(value = "name") String name);}
控制层
@RestControllerpublic class HiController { @Autowired ServiceHi serviceHi; /** * 消费服务 * 访问:http://localhost:8765/hi?name=hly 交替输出响应两个服务 * @param name * @return */ @GetMapping(value = "/hi") public String sayHi(@RequestParam String name) { return serviceHi.sayHiFromClientOne( name ); }}
1、依次启动 注册中心 eureka-server ,启动两个 eureka-client(启动一个后,修改application.yml 配置文件的端口,再次启动一次),接着启动 Feign 客户端,访问:
http://localhost:8765/hi?name=hly ,刷新浏览器,实现负载均。官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-openfeign/2.2.2.RELEASE/reference/html/#spring-cloud-feign-hystrix-fallback
03-spring-cloud-feign:
公众号:【星尘Pro】
github:
推荐阅读
转载地址:http://pufsi.baihongyu.com/