本文主要是介绍SpringCloud-Fegion的Hystrix支持,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
概述
Fegion的Hystrix支持
如果Hystrix在类路径上,feign.hystrix.enable=true,Feign将用断路器包装所有方法。
Hystrix支持回退的概念:当电路断开或出现错误时执行的默认代码路径。要为给定的@FeignClient启用回退,请将fallback属性设置为实现回退的类名。
Fign已经自带断路器了,就像自带了ribbon的负载均衡,如果想关闭feign自带的断路器,application.properties中添加feign.hystrix.enabled=false;
创建工程
pom.xml
microservice-consumer-movie-feign-with-hystrix/pom.xml
<?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"><modelVersion>4.0.0</modelVersion><artifactId>microservice-consumer-movie-feign-with-hystrix</artifactId><packaging>jar</packaging><parent><groupId>com.itmuch.cloud</groupId><artifactId>microservice-spring-cloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency></dependencies>
</project>
说明:
1)增加引用spring-cloud-starter-feign;
application.yml
spring:application:name: microservice-consumer-movie-feign-with-hystrix
server:port: 7901
eureka:client:healthcheck:enabled: trueserviceUrl:defaultZone: http://user:password123@localhost:8761/eurekainstance:prefer-ip-address: true
说明:
1)增加配置启用,开启Hystrix的断路器功能,配置文件加上
eureka:
client:
healthcheck:
enabled: true
User
com.itmuch.cloud.entity.User
package com.itmuch.cloud.entity;import java.math.BigDecimal;public class User {private Long id;private String username;private String name;private Short age;private BigDecimal balance;public Long getId() {return this.id;}public void setId(Long id) {this.id = id;}public String getUsername() {return this.username;}public void setUsername(String username) {this.username = username;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public Short getAge() {return this.age;}public void setAge(Short age) {this.age = age;}public BigDecimal getBalance() {return this.balance;}public void setBalance(BigDecimal balance) {this.balance = balance;}}
UserFeignClient
com.itmuch.cloud.feign.UserFeignClient
package com.itmuch.cloud.feign;import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;import com.itmuch.cloud.entity.User;@FeignClient(name = "microservice-provider-user", fallback = HystrixClientFallback.class)
public interface UserFeignClient {@RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)public User findById(@PathVariable("id") Long id);
}
说明:
1)Feign访问服务接口,方法定义;
2)有支持降级回调处理的配置;
HystrixClientFallback
com.itmuch.cloud.feign.HystrixClientFallback
package com.itmuch.cloud.feign;import org.springframework.stereotype.Component;import com.itmuch.cloud.entity.User;@Component
public class HystrixClientFallback implements UserFeignClient {@Overridepublic User findById(Long id) {User user = new User();user.setId(0L);return user;}
}
说明:
1)UserFeignClientHystrix 需要实现UserFeignClient;
2)Feign降级处理具体方法的实现;
MovieController
com.itmuch.cloud.controller.MovieController
package com.itmuch.cloud.controller;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;import com.itmuch.cloud.entity.User;
import com.itmuch.cloud.feign.UserFeignClient;@RestController
public class MovieController {@Autowiredprivate UserFeignClient userFeignClient;@GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {return this.userFeignClient.findById(id);}
}
说明:
1)通过Feign的方式访问服务;
ConsumerMovieFeignApplication
com.itmuch.cloud.ConsumerMovieFeignApplication
package com.itmuch.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
public class ConsumerMovieFeignApplication {public static void main(String[] args) {SpringApplication.run(ConsumerMovieFeignApplication.class, args);}
}
说明:
1)启动类增加注解
2)加上@EnableFeignClients和@EnableCircuitBreaker;
小结
1)增加引用
2)增加配置启用
3)启动类增加注解
4)增加UserFeignClient业务接口,并配置fallback
5)增加HystrixClientFallback类
测试
http://localhost:7901/movie/1
==============================
QQ群:143522604
群里有相关资源
欢迎和大家一起学习、交流、提升!
==============================
这篇关于SpringCloud-Fegion的Hystrix支持的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!