前言
本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3
本文基于前两篇文章eureka-server和eureka-client的实现。
参考
eureka-server
eureka-client
创建Feign工程
1.1 创建sping boot工程:eureka-feign
1.2 添加pom.xml相关依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
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增加依赖
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
增加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,显示如下:
至此,Feign集成Hystrix熔断机制全部完成。
总结
本文主要简单实现了feign作为服务消费的简单应用和Hystrix的集成。
代码地址
gitHub地址
- 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断路器教程
转载请注明出处,
- 联系方式:[email protected]