Feign默认集成了Ribbon, 并和Eureka结合, 默认实现了负载均衡的效果. 接口注解调用. 方便开发;java
1, 建立服务 cloud-d SERVCIE-D, pom.xml git
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.gy.cloud</groupId> <artifactId>cloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>cloud-d</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> </dependencies> </project>
2, cloud-d application.ymlweb
server: port: 8764 spring: application: name: service-d eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ # Feign 断路由配置 feign: hystrix: enabled: true
3, cloud-d CloudDApplicationspring
package com.gy.cloud.cloudd; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class CloudDApplication { public static void main(String[] args) { SpringApplication.run(CloudDApplication.class, args); System.out.println("=== 消费服务D启动成功 ==="); } }
4, HiControllerapache
package com.gy.cloud.cloudd; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HiController { @Autowired private SchedulingService schedulingService; @GetMapping("hi") public Object hi(){ return schedulingService.sayHiFromClientOne("SERVICE-D"); } }
5, SchedulingServiceapp
package com.gy.cloud.cloudd; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "service-b", fallback = SchedulingServiceImpl.class) public interface SchedulingService { @GetMapping("hi") String sayHiFromClientOne(@RequestParam(value = "name") String name); }
6, SchedulingServiceImpl负载均衡
package com.gy.cloud.cloudd; import org.springframework.stereotype.Component; @Component public class SchedulingServiceImpl implements SchedulingService { @Override public String sayHiFromClientOne(String name) { return "Sorry " + name; } }
7, 启动服务 SERVICE-D 访问 http://localhost:8764/himaven
8, 断开服务 SERVICE-B 访问 : http://localhost:8764/hiide
Feign 中 使用 断路器spring-boot
# Feign 断路由配置 feign: hystrix: enabled: true
学习文档
方志朋的博客 : https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f3-feign/
项目源码: https://gitee.com/ge.yang/spring-demo/tree/master/cloud