十年網站開發(fā)經驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網站問題一站解決
在微服務架構中,根據業(yè)務需求拆分成一個個的微小服務,然后服務與服務之間可以相互RPC遠程調用。在Spring Cloud可以使用RestTemplate+Ribbon或者Feign來進行RPC遠程調用。為了保證服務高可用性,單個服務通常會進行集群部署。由于網絡原因或者自身的原因,服務并不能保證百分之一百可用,如果服務方出現問題,調用這個服務就會出現線程阻塞,此時若有出現大量請求,導致服務方癱瘓。這時斷路器就派上用場了。
當對某個服務的調用的不可用達到一個閥值(Hystric 默認是5秒20次) 斷路器將會被自動被打開。斷路打開后, fallback方法可以直接返回一個預先設置的固定值。
成都創(chuàng)新互聯公司是一家專業(yè)提供常山企業(yè)網站建設,專注與做網站、網站制作、H5頁面制作、小程序制作等業(yè)務。10年已為常山眾多企業(yè)、政府機構等服務。創(chuàng)新互聯專業(yè)網站設計公司優(yōu)惠進行中。
1、 新建項目sc-eureka-client-consumer-ribbon-hystrix,對應的pom.xml文件
4.0.0
spring-cloud
sc-eureka-client-consumer-ribbon
0.0.1-SNAPSHOT
jar
sc-eureka-client-consumer-ribbon
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Finchley.RELEASE
pom
import
UTF-8
1.8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
2.0.1.RELEASE
備注: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);
}
}
可以看出這個啟動類只是《服務發(fā)現&服務消費者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 getUser(Long id) {
return restTemplate.getForObject("http://sc-eureka-client-provider:8200/user/getUser/{1}", Map.class, id);
}
public Map getUserError(Long id){
Map map = new HashMap();
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 listUser() {
return restTemplate.getForObject("http://sc-eureka-client-provider:8200/user/listUser", Map.class);
}
public Map listUserError(){
Map map = new HashMap();
map.put("code", "000000");
map.put("msg", "ok");
map.put("body", new ArrayList());
return map;
}
@HystrixCommand(fallbackMethod = "addUserError")
@Override
public Map addUser(User user) {
return restTemplate.postForObject("http://sc-eureka-client-provider:8200/user/addUser", user, Map.class);
}
public Map addUserError(User user){
Map map = new HashMap();
map.put("code", "000000");
map.put("msg", "ok");
map.put("body", 0);
return map;
}
@HystrixCommand(fallbackMethod = "updateUserError")
@Override
public Map updateUser(User user) {
restTemplate.put("http://sc-eureka-client-provider:8200/user/updateUser",user);
Map map = new HashMap();
map.put("code", "000000");
map.put("msg", "ok");
return map;
}
public Map updateUserError(User user){
Map map = new HashMap();
map.put("code", "000000");
map.put("msg", "ok");
map.put("body", 0);
return map;
}
@HystrixCommand(fallbackMethod = "deleteUserError")
@Override
public Map deleteUser(Long id) {
restTemplate.delete("http://sc-eureka-client-provider:8200/user/deleteUser/{id}", id);
Map map = new HashMap();
map.put("code", "000000");
map.put("msg", "ok");
return map;
}
public Map deleteUserError(Long id){
Map map = new HashMap();
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樂園