通过在@FeignClient(value = "service-hi",fallback = UserServiceBack.class ) 的fallback属性实现。
1、service-hello项目pom文件增加
···
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
···
完整如下
<?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.cloud</groupId>
<artifactId>cloud-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cloud</groupId>
<artifactId>service-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-hello</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
</project>
2、启动类增加注解@EnableHystrix
package com.cloud.service.hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrix
public class ServiceHelloApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHelloApplication.class, args);
}
}
3、配置文件把hystrix开关打开
spring:
application:
name: service-hello
server:
port: 8763
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
feign:
hystrix:
enabled: true
4、userService接口增加fallback配置
package com.cloud.service.hello;
import com.cloud.service.hello.backup.UserServiceBack;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author: zhouwei
* @version: v1.0
* @description: com.cloud.service.hello
* @date:2021-01-25
*/
@FeignClient(value = "service-hi",fallback = UserServiceBack.class ) //要调用的服务的名称
public interface UserService {
@GetMapping("/user/{id}") //mapping地址
String getUser(@RequestParam long id);
}
5、UserServiceBack实现UserService接口
package com.cloud.service.hello.backup;
import com.cloud.service.hello.UserService;
import org.springframework.stereotype.Component;
/**
* @author: zhouwei
* @version: v1.0
* @description: com.cloud.service.hello.backup
* @date:2021-01-25
*/
@Component
public class UserServiceBack implements UserService {
@Override
public String getUser(long id) {
return "走到fallback里面来了";
}
}
停掉service-hi服务,访问