07、SpringCloud Feign整合Hystrix
2020-03-29 本文已影响0人
adced06edef5
一、代码示例
说明:此处使用的SpringBoot版本为2.1.13.RELEASE,SpringCloud版本为Greenwich.SR5
此处不再贴server与client代码。
1、maven依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
2、application.yml
增加配置
feign:
hystrix:
enabled: true
server:
port: 9005
spring:
application:
name: feign-hystrix
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: feign-hystrix-9005
client:
service-url:
defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
feign:
hystrix:
enabled: true
#info信息
info:
app:
name: feign-hystrix-9005
company:
name: www.xxx.com
build:
artifactId: ${project.artifactId}
version: ${project.version}
3、启动类
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class FeignHystrix9005Application {
public static void main(String[] args) {
SpringApplication.run(FeignHystrix9005Application.class,args);
}
}
4、其他java类
定义接口FeignService ,增加@FeignClient,指明调用的服务,以及fallback
@FeignClient(value = "CLIENT",fallback = FeignServiceFallback.class)
public interface FeignService {
@GetMapping("/hello/{name}")
public String hello(@PathVariable("name") String name);
}
增加FeignService 接口的FeignServiceFallback,该类需注入到容器
package org.example.service;
import org.springframework.stereotype.Component;
@Component
public class FeignServiceFallback implements FeignService{
@Override
public String hello(String name) {
return "helloError,"+name;
}
}
Controller类
package org.example.controller;
import org.example.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignHystrixController {
@Autowired
private FeignService feignService;
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name){
return feignService.hello(name);
}
}
2、测试验证
先后启动server、client和本服务,访问http://localhost:7001/
再访问http://localhost:9005/hello/zs
image.png
然后,停止client,再次访问
image.png
表明代码生效。
github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
参考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
还有尚硅谷周阳老师的视频