1、服务feign-ha
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yzm</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>feign-ha</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>feign-ha</name>
<description>Demo project for Spring Boot</description>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 8094
spring:
application:
name: feign-ha
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8088/eureka/
#启用熔断机制
feign:
hystrix:
enabled: true
启动类
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class FeignHaApplication {
...
}
Controller
@RestController
public class HaController {
@Resource
private HaFeign haFeign;
@Value("${server.port}")
private String port;
@GetMapping("hello")
public String hello(@RequestParam String name) {
return "ha," + name + " ! " + "访问端口号:" + port;
}
@GetMapping("ha")
public String ha(@RequestParam String name) {
return haFeign.callHi(name);
}
}
Service
// 请求转发到 feign-hi 服务,服务不可达时,回调Haback
@FeignClient(value = "feign-hi", fallback = HaBack.class)
public interface HaFeign {
@GetMapping("hello")
String callHi(@RequestParam String name);
}
@Service
public class HaBack implements HaFeign {
@Override
public String callHi(String name) {
return "feign-hi 服务挂了";
}
}
2、服务feign-hi
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yzm</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>feign-hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>feign-hi</name>
<description>Demo project for Spring Boot</description>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 8095
spring:
application:
name: feign-hi
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8088/eureka/
feign:
hystrix:
enabled: true
启动类
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class FeignHiApplication {
...
}
Controller
@RestController
public class HiController {
@Resource
private HiFeign hiFeign;
@Value("${server.port}")
private String port;
@GetMapping("hello")
public String hello(@RequestParam String name) {
return "hi," + name + " ! " + "访问端口号:" + port;
}
@GetMapping("hi")
public String hi(@RequestParam String name) {
return hiFeign.callHa(name);
}
}
Service
@FeignClient(value = "feign-ha", fallback = HiBack.class)
public interface HiFeign {
@GetMapping("hello")
String callHa(@RequestParam String name);
}
@Service
public class HiBack implements HiFeign {
@Override
public String callHa(String name) {
return "feign-ha 服务挂了";
}
}
3、路由转发
依次启动eureka、feign-ha、feign-hi
访问 http://localhost:8095/hi?name=yzm
转发成功!
4、负载均衡
启动feign-hi:18095服务
刷新 http://localhost:8094/ha?name=yzm
负载成功!
5、熔断降级
关闭feign-ha:8094服务
访问 http://localhost:8095/hi?name=yzm
熔断成功!