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