公眾號: java樂園
成都創(chuàng)新互聯(lián)公司是一家專注于成都做網(wǎng)站、成都網(wǎng)站制作與策劃設(shè)計,青銅峽網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:青銅峽等地區(qū)。青銅峽做網(wǎng)站價格咨詢:18980820575上編說了《RestTemplate+Ribbon整合斷路器Hystrix》,這篇來看看如何Feign整合斷路器Hystrix,F(xiàn)eign整合斷路器Hystrix也是相對比較簡單的。Feign默認已經(jīng)自帶斷路器Hystrix,所以不需要像RestTemplate+Ribbon整合斷路器Hystrix那樣需要在SpringBoot的啟動類添加注解。但是Feign自帶斷路器并沒有打開,需要做些額外的配置。
feign:
hystrix:
enabled: true
1、 新建項目sc-eureka-client-consumer-feign-hystrix,對應(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-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sc-eureka-client-consumer-feign</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>
<!-- 說明是一個 eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-feign</artifactId>
<version>1.4.5.RELEASE</version>
</dependency> -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
備注:從繼續(xù)關(guān)系可以看出spring-cloud-starter-openfeign已經(jīng)集成斷路器Hystrix
2、 新建springboot啟動類
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.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerFeignApplication.class, args);
}
}
3、 新建配置文件bootstrap.yml和application.yml
bootstrap.yml
server:
port: 5800
application.yml
spring:
application:
name: sc-eureka-client-consumer-feign-hystrix
eureka:
client:
registerWithEureka: true #是否將自己注冊到Eureka服務(wù)中,默認為true
fetchRegistry: true #是否從Eureka中獲取注冊信息,默認為true
serviceUrl:
defaultZone: http://localhost:5001/eureka/
feign:
hystrix:
enabled: true
說明:在application.yml配置文件添加了開啟斷路器Hystrix的配置項
4、 新建服務(wù)消費者類UserService.java
package sc.consumer.service;
import java.util.Map;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import sc.consumer.model.User;
import sc.consumer.service.hystrix.UserServiceHystrix;
@FeignClient(value="sc-eureka-client-provider", fallback=UserServiceHystrix.class)
public interface UserService {
@GetMapping("/user/getUser/{id}")
Map<String, Object> getUser(@PathVariable(value ="id") Long id);
@RequestMapping("/user/listUser")
Map<String, Object> listUser();
@PostMapping("/user/addUser")
Map<String, Object> addUser(@RequestBody User user);
@PutMapping("/user/updateUser")
Map<String, Object> updateUser(@RequestBody User user);
@DeleteMapping("/user/deleteUser/{id}")
Map<String, Object> deleteUser(@PathVariable(value ="id") Long id);
}
可以看到在該類的FeignClient注解上添加了一個屬性fallback
5、 新建斷路器處理類UserServiceHystrix.java
package sc.consumer.service.hystrix;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;
import sc.consumer.model.User;
import sc.consumer.service.UserService;
@Component
public class UserServiceHystrix implements UserService{
@Override
public Map<String, Object> getUser(Long id) {
Map<String, Object> result = new HashMap<String, Object>();
result.put("code", "000000");
result.put("msg", "success");
User u = new User();
u.setId(-1L);
u.setUserName("failBackName");
result.put("body", u);
return result;
}
@Override
public Map<String, Object> listUser() {
Map<String, Object> result = new HashMap<String, Object>();
result.put("code", "000000");
result.put("msg", "success");
List<User> list = new ArrayList<User>();
User u = new User();
u.setId(-1L);
u.setUserName("failBackName");
list.add(u);
result.put("body", list);
return result;
}
@Override
public Map<String, Object> addUser(User user) {
Map<String, Object> result = new HashMap<String, Object>();
result.put("code", "000000");
result.put("msg", "success");
result.put("body", 0);
return result;
}
@Override
public Map<String, Object> updateUser(User user) {
Map<String, Object> result = new HashMap<String, Object>();
result.put("code", "000000");
result.put("msg", "success");
result.put("body", 0);
return result;
}
@Override
public Map<String, Object> deleteUser(Long id) {
Map<String, Object> result = new HashMap<String, Object>();
result.put("code", "000000");
result.put("msg", "success");
result.put("body", 0);
return result;
}
}
該類實現(xiàn)了UserService接口,并實現(xiàn)了該接口的所有方法。
6、 分別啟動注冊中心sc-eureka-server和服務(wù)提供者sc-eureka-client-provider
7、 啟動sc-eureka-client-consumer-feign-hystrix,并驗證是否啟動成功
方式一:查看日志看看對應(yīng)的端口是啟動
方式二:查看注冊中心是否注冊成功
8、 使用postman驗證斷路器是否啟用
上篇從服務(wù)提供者是否正常啟動驗證斷路器是否啟動,今天看看從數(shù)據(jù)庫是否啟動來驗證斷路器是否啟動
(1)MySQL服務(wù)正常啟動時訪問:
http://127.0.0.1:5800/feign/user/listUser
(2)MySQL服務(wù)關(guān)閉時訪問
http://127.0.0.1:5800/feign/user/listUser
再看下后臺日志:
其他接口可以自行按照以上方式進行驗證,看看斷路器是否啟動
源碼 https://gitee.com/hjj520/spring-cloud-2.x
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站欄目:12、Feign整合斷路器Hystrix-創(chuàng)新互聯(lián)
標題URL:http://jinyejixie.com/article18/jgcdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、自適應(yīng)網(wǎng)站、定制開發(fā)、軟件開發(fā)、網(wǎng)站收錄、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容