
实现demo步骤如下
1.创建在spring-cloud-demo下创建hystrix-fallback模块
pom,从feigin中copy过来(hystrix是一个服务调用者,所以要调用feigin-client)
<?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">
<parent>
<artifactId>spring-cloud-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc</groupId>
<artifactId>hystrix-fallback</artifactId>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.imooc</groupId>
<artifactId>feign-client-intf</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
</dependency>
</dependencies>
</project>
2.创建hystrix启动类(4个注解?)
HystrixFallbackApplication:
package com.imooc.springcloud;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* Created by 半仙.
*/
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
public class HystrixFallbackApplication {
public static void main(String[] args) throws NoSuchMethodException {
new SpringApplicationBuilder(HystrixFallbackApplication.class)
.web(WebApplicationType.SERVLET)
.run(args);
// System.out.println(Feign.configKey(MyService.class,
// MyService.class.getMethod("retry", int.class)));
}
}
3.定义一个接口MyService,与“害群之马”联系上,并且加上fallback
MyService:
package com.imooc.springcloud;
import org.springframework.cloud.openfeign.FeignClient;
@FeignClient(name = "feign-client",fallback = "Fallback.class")
public interface MyService extends IService {
}
4.定义一个Hystrix的fallback类
//超时也算是一种错误
1.创建包com/imooc/springcloud/hystrix
2.创建Fallback类
- Fallback类实现上面创建的MyService的接口,实现error接口的静默
package com.imooc.springcloud.hystrix;
import com.imooc.springcloud.Friend;
import com.imooc.springcloud.MyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class Fallback implements MyService {
@Override
public String error() {
log.info("Fallback:I am not a back sheep any more")
return "Fallback:I am not a back sheep any more";
}
@Override
public String sayHi() {
return null;
}
@Override
public Friend sayHiPost(Friend friend) {
return null;
}
@Override
public String retry(int timeout) {
return null;
}
}
5.创建Controller
package com.imooc.springcloud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@Autowired
private MyService myService;
@GetMapping("/fallback")
public String fallback(){
return myService.error();
}
}
6.在feign-client-intf模块创建error方法(创建服务提供者)
IService:
package com.imooc.springcloud;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
/**
* Created by 半仙.
*/
@FeignClient("feign-client")
public interface IService {
@GetMapping("/sayHi")
public String sayHi();
@PostMapping("/sayHi")
public Friend sayHiPost(@RequestBody Friend friend);
@GetMapping("/retry")
public String retry(@RequestParam(name = "timeout") int timeout);
@GetMapping("/error")
public String error();
}
IService的实现方法Controller(定义一个害群之马):
package com.imooc.springcloud;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by 半仙.
*/
@RestController
@Slf4j
public class Controller implements IService {
@Value("${server.port}")
private String port;
@GetMapping("/sayHi2")
public String sayHi2() {
return "This is " + port;
}
@Override
public String sayHi() {
return "This is " + port;
}
@Override
public Friend sayHiPost(@RequestBody Friend friend) {
log.info("You are " + friend.getName());
friend.setPort(port);
return friend;
}
@Override
public String retry(@RequestParam(name = "timeout") int timeout) {
while (--timeout >= 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
log.info("retry " + port);
return port;
}
@Override
public String error() {
throw new RuntimeException("black sheep");
}
}
7.配置文件
application.properties :
spring.application.name=hystrix-consumer
server.port=50000
spring.main.allow-bean-definition-overriding=true
eureka.client.serviceUrl.defaultZone=http://localhost:20000/eureka/
# 开启Feign下面的Hystrix功能
feign.hystrix.enabled=true
# 是否开启服务降级
hystrix.command.default.fallback.enabled=true
8.启动
eureka-server -> feigin -> hystrix
网友评论