前言
本文采用Spring cloud本文為2.1.8RELEASE,version=Greenwich.SR3
本文基於前兩篇文章eureka-server和eureka-client的實現。
參考
創建Feign工程
1.1 創建sping boot工程:eureka-feign
1.2 添加pom.xml相關依賴
<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-openfeign</artifactId>
</dependency>
1.3 application添加配置信息
spring:
application:
name: eureka-feign
server:
port: 8601
eureka:
instance:
hostname: localhost
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
1.4 啟動類EurekaFeignApplication增加注解
package spring.cloud.demo.eurekafeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class EurekaFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaFeignApplication.class, args);
}
}
@EnableDiscoveryClient:這里使用EnableDiscoveryClient注解,在eureka-client已說明,這邊就不在闡述
@EnableFeignClients:啟用Feign客戶端
1.5 創建服務接口EurekaFeignService
package spring.cloud.demo.eurekafeign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @auther: maomao
* @DateT: 2019-09-17
*/
@FeignClient(value = "eureka-client")
public interface EurekaFeignService {
@RequestMapping(value = "/info")
String syaHello();
}
@FeignClient:定義Feign客戶端,調用遠程client。eureka-client代表client的spring.application.name,調用遠程地址示例:http://eureka-client/info
1.6 創建EurekaFeignController控制類
package spring.cloud.demo.eurekafeign.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.cloud.demo.eurekafeign.service.EurekaFeignService;
import javax.annotation.Resource;
/**
* @auther: maomao
* @DateT: 2019-09-17
*/
@RestController
@RequestMapping("/feign")
public class EurekaFeignController {
@Resource
private EurekaFeignService eurekaFeignService;
@RequestMapping("/sayHello")
public String sayHello() {
return "feign result: " + eurekaFeignService.syaHello();
}
}
1.7 啟動eureka-feign服務
可以在eureka-server服務注冊中心看到eureka-feign是否注冊成功。
紅框中內容代表eureka-feign已經正常啟動並成功注冊到eureka-server服務注冊中心。
訪問http://localhost:8601/feign/sayHello,顯示如下:
多刷新幾次可以在瀏覽器中看到不通的結果,端口是變化的。
Feign默認的負載均衡策略是輪詢方式。如果想修改Feign的負載均衡策略請參考eureka-ribbon中的配置
至此,一個簡單的單機Feign服務消費者工程就搭建完成了。
彩蛋
Feign集成Hystrix熔斷機制
pom.xml增加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
增加application.yml配置
feign:
hystrix:
enabled: true
修改EurekaFeignService
在@FeignClient注解中增加fallback
package spring.cloud.demo.eurekafeign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @auther: maomao
* @DateT: 2019-09-17
*/
@FeignClient(value = "eureka-client", fallback = EurekaFeignServiceFailure.class)
public interface EurekaFeignService {
@RequestMapping(value = "/info")
String syaHello();
}
增加EurekaFeignServiceFailure類:
package spring.cloud.demo.eurekafeign.service;
import org.springframework.stereotype.Service;
/**
* @auther: maomao
* @DateT: 2019-09-17
*/
@Service
public class EurekaFeignServiceFailure implements EurekaFeignService {
@Override
public String syaHello() {
return "網絡繁忙,請稍后在試";
}
}
啟動eureka-feign服務
首先在eureka-client全不啟動的情況,訪問http://localhost:8601/feign/sayHello看是否全不正常,然后停掉其中一個eureka-client,在多次刷新http://localhost:8601/feign/sayHello,顯示如下:
說明增加的Hystrix已經生效。
至此,Feign集成Hystrix熔斷機制全部完成。
總結
本文主要簡單實現了feign作為服務消費的簡單應用和Hystrix的集成。
代碼地址
- spring cloud 2.x版本 Eureka Server服務注冊中心教程
- spring cloud 2.x版本 Eureka Client服務提供者教程
- spring cloud 2.x版本 Ribbon服務發現教程(內含集成Hystrix熔斷機制)
- spring cloud 2.x版本 Feign服務發現教程(內含集成Hystrix熔斷機制)
- spring cloud 2.x版本 Zuul路由網關教程
- spring cloud 2.x版本 config分布式配置中心教程
- spring cloud 2.x版本 Hystrix Dashboard斷路器教程
轉載請注明出處,
- 聯系方式:4272231@163.com