在微服务架构中,根据业务需求拆分成一个个的微小服务,然后服务与服务之间可以相互RPC远程调用。在Spring Cloud可以使用RestTemplate+Ribbon或者Feign来进行RPC远程调用。为了保证服务高可用性,单个服务通常会进行集群部署。由于网络原因或者自身的原因,服务并不能保证百分之一百可用,如果服务方出现问题,调用这个服务就会出现线程阻塞,此时若有出现大量请求,导致服务方瘫痪。这时断路器就派上用场了。
当对某个服务的调用的不可用达到一个阀值(Hystric 默认是5秒20次) 断路器将会被自动被打开。断路打开后, fallback方法可以直接返回一个预先设置的固定值。
1、 新建项目sc-eureka-client-consumer-ribbon-hystrix,对应的pom.xml文件
<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> <groupId>spring-cloud</groupId> <artifactId>sc-eureka-client-consumer-ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sc-eureka-client-consumer-ribbon</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>1.4.5.RELEASE</version> </dependency> --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.4.5.RELEASE</version> </dependency> --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.0.1.RELEASE</version> </dependency> </dependencies> </project>
备注:spring-cloud-starter-hystrix已经在spring cloud 2.x标注成过期。推荐使用spring-cloud-starter-netflix-hystrix
2、 新建spring boot 启动类ConsumerApplication.java
package sc.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; @SpringBootApplication @EnableEurekaClient @EnableHystrix public class ConsumerHystrixApplication { public static void main(String[] args) { SpringApplication.run(ConsumerHystrixApplication.class, args); } }
可以看出这个启动类只是《服务发现&服务消费者Ribbon》的启动类添加多了一个注解EnableHystrix(启动断路器)
3、 编写服务类,并添加断路器注解
package sc.consumer.service.impl; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import com.github.andrewoma.dexx.collection.ArrayList; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import sc.consumer.model.User; import sc.consumer.service.UserService; @Service public class UserServiceImpl implements UserService{ @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "getUserError") @Override public Map<String, Object> getUser(Long id) { return restTemplate.getForObject("http://sc-eureka-client-provider:8200/user/getUser/{1}", Map.class, id); } public Map<String, Object> getUserError(Long id){ Map<String, Object> map = new HashMap<String, Object>(); map.put("code", "000000"); map.put("msg", "ok"); User u = new User(); u.setId(-1L); u.setUserName("failName"); map.put("body", u); return map; } @HystrixCommand(fallbackMethod = "listUserError") @Override public Map<String, Object> listUser() { return restTemplate.getForObject("http://sc-eureka-client-provider:8200/user/listUser", Map.class); } public Map<String, Object> listUserError(){ Map<String, Object> map = new HashMap<String, Object>(); map.put("code", "000000"); map.put("msg", "ok"); map.put("body", new ArrayList<User>()); return map; } @HystrixCommand(fallbackMethod = "addUserError") @Override public Map<String, Object> addUser(User user) { return restTemplate.postForObject("http://sc-eureka-client-provider:8200/user/addUser", user, Map.class); } public Map<String, Object> addUserError(User user){ Map<String, Object> map = new HashMap<String, Object>(); map.put("code", "000000"); map.put("msg", "ok"); map.put("body", 0); return map; } @HystrixCommand(fallbackMethod = "updateUserError") @Override public Map<String, Object> updateUser(User user) { restTemplate.put("http://sc-eureka-client-provider:8200/user/updateUser",user); Map<String, Object> map = new HashMap<String, Object>(); map.put("code", "000000"); map.put("msg", "ok"); return map; } public Map<String, Object> updateUserError(User user){ Map<String, Object> map = new HashMap<String, Object>(); map.put("code", "000000"); map.put("msg", "ok"); map.put("body", 0); return map; } @HystrixCommand(fallbackMethod = "deleteUserError") @Override public Map<String, Object> deleteUser(Long id) { restTemplate.delete("http://sc-eureka-client-provider:8200/user/deleteUser/{id}", id); Map<String, Object> map = new HashMap<String, Object>(); map.put("code", "000000"); map.put("msg", "ok"); return map; } public Map<String, Object> deleteUserError(Long id){ Map<String, Object> map = new HashMap<String, Object>(); map.put("code", "000000"); map.put("msg", "ok"); map.put("body", 0); return map; } }
添加HystrixCommand注解,对应的参数fallbackMethod值为当方式服务方无法调用时,返回预设值得方法名。
4、 新建配置文件bootstrap.yml和application.yml
bootstrap.yml
server: port: 5600 application.yml spring: application: name: sc-eureka-client-consumer-ribbon-hystrix eureka: instance: hostname: 127.0.0.1 client: #由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己 registerWithEureka: true #由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false fetchRegistry: true serviceUrl: defaultZone: http://127.0.0.1:5001/eureka/
5、 其他项目文件参加下图
6、 分别启动注册中心sc-eureka-server和服务提供者sc-eureka-client-provider
7、 启动sc-eureka-client-consumer-ribbon-hystrix,并验证是否启动成功
方式一:查看日志
方式二:查看注册中心是否注册成功
8、 验证断路器是否起作用
(1) 服务提供者正常时访问:
http://127.0.0.1:5600/cli/user/getUser/4
正常返回数据库里的数据
(2) 服务提供者关闭时访问:
http://127.0.0.1:5600/cli/user/getUser/4
对比两个返回的数据,可以看出服务提供者关闭时,返回的数据是在程序写死的数据,如下图:
其他方法可以自行按照以上方式进行验证。
公众号:java乐园