Feign使用接口方式调用服务以及Hystrix服务熔断
- Feign负载均衡
- 客户端集成Feign
- Hystrix
- Hystrix定义
- 服务熔断
- 服务端集成Hystrix(springcloud-provider-dept-hystrix)实现服务熔断
- 客户端集成Hystrix实现服务降级
- 服务监控
Feign负载均衡
简介
客户端集成Feign
- pom文件
<!--Feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
- DeptClientService调用文件
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT")
public interface DeptClientService {
@GetMapping("/dept/{deptNo}")
Dept queryByDeptNo(@PathVariable("deptNo") Long deptNo);
@GetMapping("/dept/queryAll")
List<Dept> queryAll();
@PostMapping("/dept/add")
boolean addDept(@RequestBody Dept dept);
}
- 客户端调用
@RestController
public class FeignController {
@Autowired
private DeptClientService clientService;
@GetMapping("/consumer/dept/{deptNo}")
public Dept get(@PathVariable("deptNo") Long deptNo){
return clientService.queryByDeptNo(deptNo);
}
@GetMapping("/consumer/list")
public List<Dept> queryAll(){
return clientService.queryAll();
}
@PostMapping("/consumer/dept/add")
public boolean add(@RequestBody Dept dept){
return clientService.addDept(dept);
}
}
- 客户端启动类
添加注解:@EnableFeignClients(basePackages = {"com.service"})
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.service"})
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class,args);
}
}
Hystrix
分布式系统面临的问题
复杂分布式体系结构中的应用程序十分依赖关系,每个依赖关系在某些时候将不可避免的失败!
服务雪崩
Hystrix定义
服务熔断
服务端集成Hystrix(springcloud-provider-dept-hystrix)实现服务熔断
- pom.xml
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--完善监控信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.5</version>
</dependency>
</dependencies>
- yml文件
server:
port: 8002
#mybatis配置
mybatis:
type-aliases-package: com.api.model
mapper-locations: classpath:mybatis/mapper/*.xml
config-location: classpath:mybatis/mybatis-config.xml
#spring配置
spring:
application:
name: springcloud-provider-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/iwmstest?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
#Eureka配置,服务注册到哪里
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: springcloud-server-provider-hystrix-8002 #修改Eureka的默认描述信息
#info配置
info:
app.name: zzq-springcloud
company.name: blog.com
- controller配置
加注解:@HystrixCommand(fallbackMethod = "hystrixGet")
@RestController
public class DeptHystrixController {
@Autowired
private DeptHystrixService deptService;
@HystrixCommand(fallbackMethod = "hystrixGet")
@GetMapping("/dept/{deptNo}")
public Dept get(@PathVariable("deptNo") Long deptNo){
Dept dept = deptService.queryById(deptNo);
if (dept == null) {
throw new RuntimeException("不存在该用户");
}
return dept;
}
//备选方法
public Dept hystrixGet(Long deptNo){
return new Dept()
.setDeptNo(deptNo)
.setDName("没有用户")
.setDbSource("no database");
}
}
- 启动类开启熔断机制
@EnableCircuitBreaker //启动服务熔断
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker //启动服务熔断
public class HystrixApplicaiton {
public static void main(String[] args) {
SpringApplication.run(HystrixApplicaiton.class,args);
}
}
客户端集成Hystrix实现服务降级
- DeptClientServiceFailBackFactory.java
@Component
public class DeptClientServiceFailBackFactory implements FallbackFactory<DeptClientService> {
@Override
public DeptClientService create(Throwable throwable) {
return new DeptClientService() {
@Override
public Dept queryByDeptNo(Long deptNo) {
return new Dept().setDeptNo(deptNo)
.setDName("没有对应的信息")
.setDbSource("no database")
;
}
@Override
public List<Dept> queryAll() {
return null;
}
@Override
public boolean addDept(Dept dept) {
return false;
}
};
}
}
- DeptClientService.java
添加:fallbackFactory = DeptClientServiceFailBackFactory.class
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT",fallbackFactory = DeptClientServiceFailBackFactory.class)
public interface DeptClientService {
@GetMapping("/dept/{deptNo}")
Dept queryByDeptNo(@PathVariable("deptNo") Long deptNo);
@GetMapping("/dept/queryAll")
List<Dept> queryAll();
@PostMapping("/dept/add")
boolean addDept(@RequestBody Dept dept);
}
客户端配置文件开启服务降级
开启服务降级 feign: hystrix: enabled: true
server:
port: 8081
#Eureka配置
eureka:
client:
register-with-eureka: false #不向Eureka注册自己
service-url:
defaultZone: http://localhost:8761/eureka/ #客户端注册进Eureka
# 开启服务降级
feign:
hystrix:
enabled: true
服务熔断和服务降级总结