前言
Github:https://github.com/yihonglei/thinking-in-springcloud
Eureka註冊中心:eureka-server
服務提供者(訂單服務):eureka-provider-order
Feign-api(服務接口抽象):eureka-feign-api
Feign客戶端消費:eureka-consumer-feign-hystrix
一 Feign支持Hystrix
Feign是一個聲明式的僞RPC的REST客戶端,基於接口的註解方式,很方便客戶端配置。
Spring Cloud集成Ribbon和Eureka以在使用Feign時提供負載均衡的http客戶端。
Hystrix基於開源框架Netflix實現了Spring Cloud Hystrix,該框架的目標在於通過控制哪些訪問遠程系統、
服務等,從而對於網絡延遲和故障提供更強大的容錯能力。
二 啓動eureka-server(註冊中心)
執行eureka-server項目EurekaServerApplication類的main方法。
三 啓動eureka-provider-order(服務提供者)
執行eureka-provider-order項目EurekaOrderApplication類的main方法。
四 eureka-feign-api(feign接口抽象,添加hystrix)
1、項目結構
2、pom.xml依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3、OrderApi(feign抽象order服務接口)
package com.lanhuigu.order;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* order服務,FeignClient標註服務提供者信息
*
* @auther: yihonglei
* @date: 2019-06-30 21:17
*/
@FeignClient(name = "eureka-provider-order", fallback = OrderApiFallBack.class, path = "/order")
public interface OrderApi {
@RequestMapping("/queryOrderInfo")
String queryOrdersByUserId();
}
@FeignClient註解name指明要調用的服務,fallback指定降級類,path訪問路徑。
4、OrderApiFallBack(hystrix接口降級實現)
package com.lanhuigu.order;
import org.springframework.stereotype.Component;
/**
* Order訂單,服務降級接口
*
* @auther: yihonglei
* @date: 2019-07-06 21:51
*/
@Component
public class OrderApiFallBack implements OrderApi {
@Override
public String queryOrdersByUserId() {
return "觸發服務降級接口";
}
}
服務降級類必須實現對應的接口,以及必須添加@Component註解。
當接口出現問題時,調用服務降級接口返回給調用者。
5、打成jar包,供別的工程使用
五 eureka-consumer-feign-hystrix(feign開啓hystrix)
1、項目結構
2、pom.xml依賴
<!-- Eureka Client -->
<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-netflix-hystrix</artifactId>
</dependency>
<!-- feign-api -->
<dependency>
<groupId>com.lanhuigu</groupId>
<artifactId>eureka-feign-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
3、application.yml(配置文件)
# 註冊到eureka服務端的微服務名稱
spring:
application:
name: eureka-consumer-feign-hystrix
# 服務提供端口
server:
port: 8008
# 註冊到eureka服務端的地址
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
# 顯示指定微服務的名稱,默認ip:應用名稱:端口(192.168.1.7:eureka-consumer-feign-hystrix:8008)
instance:
instance-id: eureka-consumer-feign-hystrix-8008
prefer-ip-address: true
# 開啓feign支持hystrix,默認關閉
feign:
hystrix:
enabled: true
feign.hystrix.enabled開啓feign支持hystrix,在Spring Cloud的E版本之後,默認是關閉的。
4、UserController(控制層)
package com.lanhuigu.controller;
import com.lanhuigu.order.OrderApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @RestController這個註解等價於spring mvc用法中的@Controller+@ResponseBody
*
* @auther: yihonglei
* @date: 2019-06-17 22:30
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private OrderApi orderApi;
@RequestMapping(value = "/queryUserInfo", method = {RequestMethod.GET, RequestMethod.POST})
public String queryUserInfo() {
String orderInfo = orderApi.queryOrdersByUserId();
return orderInfo;
}
}
通過OrderApi直接調用服務接口。
5、EurekaConsumerFeignHystrixApplication(應用啓動類)
package com.lanhuigu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
/**
* @SpringBootApplication 啓動一個Spring Boot應用程序
* @EnableDiscoveryClient 服務發現與註冊,當應用啓動時,將應用註冊到配置的註冊中心
*
* @auther: yihonglei
* @date: 2019-06-19 11:27
*/
@EnableDiscoveryClient
@SpringBootApplication
@EnableCircuitBreaker
@EnableFeignClients(basePackages = "com.lanhuigu")
public class EurekaConsumerFeignHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerFeignHystrixApplication.class, args);
}
}
@EnableCircuitBreaker開啓hystrix,@EnableFeignClients激活feign。
運行main方法,啓動服務。
六 服務訪問,熔斷測試
1、註冊中心
http://localhost:9000
註冊中心可以看到eureka-provider-order-8001服務和eureka-consumer-feign-hystrix。
2、訪問服務
http://localhost:8008/user/queryUserInfo
3、斷掉order服務,再次訪問
當order服務斷掉後,服務接口訪問不同,自動訪問降級接口,返回預期結果,從而實現熔斷服務降級。