一, 简介
Feign默认集成了Ribbon, 并和Eureka结合, 默认实现了负载均衡的效果. 接口注解调用. 方便开发;
二, 具体实现
1, 创建服务 cloud-d SERVCIE-D, pom.xml
<?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.yml
server:
port: 8764
spring:
application:
name: service-d
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
# Feign 断路由配置
feign:
hystrix:
enabled: true
3, cloud-d CloudDApplication
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, HiController
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, SchedulingService
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/hi
8, 断开服务 SERVICE-B 访问 : http://localhost:8764/hi
三, 总结
Feign 中 使用 断路器
# Feign 断路由配置
feign:
hystrix:
enabled: true