您好,登錄后才能下訂單哦!
在微服務(wù)架構(gòu)中,根據(jù)業(yè)務(wù)需求拆分成一個(gè)個(gè)的微小服務(wù),然后服務(wù)與服務(wù)之間可以相互RPC遠(yuǎn)程調(diào)用。在Spring Cloud可以使用RestTemplate+Ribbon或者Feign來(lái)進(jìn)行RPC遠(yuǎn)程調(diào)用。為了保證服務(wù)高可用性,單個(gè)服務(wù)通常會(huì)進(jìn)行集群部署。由于網(wǎng)絡(luò)原因或者自身的原因,服務(wù)并不能保證百分之一百可用,如果服務(wù)方出現(xiàn)問題,調(diào)用這個(gè)服務(wù)就會(huì)出現(xiàn)線程阻塞,此時(shí)若有出現(xiàn)大量請(qǐng)求,導(dǎo)致服務(wù)方癱瘓。這時(shí)斷路器就派上用場(chǎng)了。
當(dāng)對(duì)某個(gè)服務(wù)的調(diào)用的不可用達(dá)到一個(gè)閥值(Hystric 默認(rèn)是5秒20次) 斷路器將會(huì)被自動(dòng)被打開。斷路打開后, fallback方法可以直接返回一個(gè)預(yù)先設(shè)置的固定值。
1、 新建項(xiàng)目sc-eureka-client-consumer-ribbon-hystrix,對(duì)應(yīng)的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已經(jīng)在spring cloud 2.x標(biāo)注成過期。推薦使用spring-cloud-starter-netflix-hystrix
2、 新建spring boot 啟動(dòng)類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);
}
}
可以看出這個(gè)啟動(dòng)類只是《服務(wù)發(fā)現(xiàn)&服務(wù)消費(fèi)者Ribbon》的啟動(dòng)類添加多了一個(gè)注解EnableHystrix(啟動(dòng)斷路器)
3、 編寫服務(wù)類,并添加斷路器注解
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注解,對(duì)應(yīng)的參數(shù)fallbackMethod值為當(dāng)方式服務(wù)方無(wú)法調(diào)用時(shí),返回預(yù)設(shè)值得方法名。
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:
#由于該應(yīng)用為注冊(cè)中心,所以設(shè)置為false,代表不向注冊(cè)中心注冊(cè)自己
registerWithEureka: true
#由于注冊(cè)中心的職責(zé)就是維護(hù)服務(wù)實(shí)例,它并不需要去檢索服務(wù),所以也設(shè)置為false
fetchRegistry: true
serviceUrl:
defaultZone: http://127.0.0.1:5001/eureka/
5、 其他項(xiàng)目文件參加下圖
6、 分別啟動(dòng)注冊(cè)中心sc-eureka-server和服務(wù)提供者sc-eureka-client-provider
7、 啟動(dòng)sc-eureka-client-consumer-ribbon-hystrix,并驗(yàn)證是否啟動(dòng)成功
方式一:查看日志
方式二:查看注冊(cè)中心是否注冊(cè)成功
8、 驗(yàn)證斷路器是否起作用
(1) 服務(wù)提供者正常時(shí)訪問:
http://127.0.0.1:5600/cli/user/getUser/4
正常返回?cái)?shù)據(jù)庫(kù)里的數(shù)據(jù)
(2) 服務(wù)提供者關(guān)閉時(shí)訪問:
http://127.0.0.1:5600/cli/user/getUser/4
對(duì)比兩個(gè)返回的數(shù)據(jù),可以看出服務(wù)提供者關(guān)閉時(shí),返回的數(shù)據(jù)是在程序?qū)懰赖臄?shù)據(jù),如下圖:
其他方法可以自行按照以上方式進(jìn)行驗(yàn)證。
公眾號(hào):java樂園
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。