SpringCloud-RestClient Feign
Feign是一个声明性的Web服务客户端。它使编写Web服务客户端变得更容易。 Feign增加了对Spring MVC注解的支持,并使用了Spring MVC中相同的HttpMessageConverters。 Feign集成了Ribbon和Eureka,Hystrix 在使用Feign时提供负载均衡的http客户端
记得先启动Eureka
快速入门
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
FeignSpringBootApplication
@SpringBootApplication
@EnableFeignClients
public class FeignSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(FeignSpringBootApplication.class,args);
}
}
application.properties
eureka.client.register-with-eureka=false
eureka.client.healthcheck.enabled=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://CentOSA:1111/eureka/,http://CentOSB:1111/eureka/,http://CentOSC:1111/eureka/
然后FeignClient
这个可以这样配 将你要调用服务的Controller复制过来然后改为接口 加上FeignClient注解 写上你要调用的注册中心的服务
@FeignClient(name = "USER-SERVICE")
public interface UserFeignControllerClient {
//这里必须这样写为了之后的reign的配置
@PostMapping(value = "/formUserManager/addUser")
@ResponseBody
public void addUser(User user) throws IOException;
@GetMapping(value = "/formUserManager/queryUserById")
@ResponseBody
public User queryUserById(@RequestParam(value = "id") Integer id);
@GetMapping(value = "/formUserManager/queryUserByPage")
@ResponseBody
public Map<String, Object> queryUserByPage(@RequestParam(value = "page", defaultValue = "1") Integer pageNow,
@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
@RequestParam(value = "column", required = false) String column,
@RequestParam(value = "value", required = false) String value);
@DeleteMapping(value = "/formUserManager/delteUserByIds")
@ResponseBody
public void delteUserByIds(@RequestParam(value = "ids") Integer[] ids);
@PutMapping(value = "/formUserManager/updateUser")
@ResponseBody
public void updateUser(User user) throws IOException;
}
其中的IUserServiceFeignClient的name=USER-SERVICE属性指的是Feign会从eureka中查找命名服务,底层会更具接口方法的配置信息参数发送请求给远程的服务器这种实现比单纯的使用Ribbon更加的优雅。实际上Feign底层就是对Ribbon组件的封装。
然后当程序入口类启动的时候会扫描有@FeignClient这个的注解 然后扫描关于spring的注解 创建代理的RestTemplate
这个时候你再去掉远程服务的时间就不用去考虑url了 更加舒服的调度
User user = userFeignControllerClient.queryUserById(30);
Feigen 熔断
默认Feign没有开启熔断策略,需要用户在配置文件中指定
feign.hystrix.enabled=true
写一个类实现你的ReignClient然后里面写你的FallBack方法
@Component
public class UserFeignControllerFailBack implements UserFeignControllerClient {
@Override
public void addUser(User user) throws IOException {
}
@Override
public User queryUserById(Integer id) {
User user = new User("故障", false, "***", new Date(), "xxxx");
user.setId(id);
return user;
}
@Override
public Map<String, Object> queryUserByPage(Integer pageNow, Integer pageSize, String column, String value) {
return null;
}
@Override
public void delteUserByIds(Integer[] ids) {
}
@Override
public void updateUser(User user) throws IOException {
}
}
再给他配置进去
@FeignClient(name = "USER-SERVICE",fallback = UserFeignControllerFailBack.class)
要想测试 配置application.proeprties设置对应服务的请求超时时间
feign.client.config.USER-SERVICE.connect-timeout=500
feign.client.config.USER-SERVICE.read-timeout=500
注意:一般企业中 不常直接使用Feign的 因为好多的配置都是没法直接去配置的 一般都是直接使用ribbon和hystrix等组件
如果你想要去配置熔断的话
Hystrix Dashboard
配置pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
配置的properties
eureka.client.register-with-eureka=false
eureka.client.healthcheck.enabled=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://CentOSA:1111/eureka/,http://CentOSB:1111/eureka/,http://CentOSC:1111/eureka/
feign.client.config.USER-SERVICE.connect-timeout=500
feign.client.config.USER-SERVICE.read-timeout=500
feign.hystrix.enabled=true
management.endpoints.web.exposure.include=*
FeignSpringBootApplication
@SpringBootApplication
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class FeignSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(FeignSpringBootApplication.class,args);
}
}
配置HyStrix属性
在和Feign整合后,用户无法配置Feign的ComandProperties,但是可以通过配置Bean的形式配置
@SpringBootApplication
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class FeignSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(FeignSpringBootApplication.class,args);
}
@Bean
public SetterFactory setterFactory(){
SetterFactory setterFactory =new SetterFactory() {
@Override
public HystrixCommand.Setter create(Target<?> target, Method method) {
String groupKey = target.name();
String commandKey = Feign.configKey(target.type(), method);
HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter()
//设置统计指标5秒为一个时间窗口
.withMetricsRollingStatisticalWindowInMilliseconds(5000)
//十个分区
.withMetricsRollingPercentileBucketSize(10);
//操过50%失败率
.withCircuitBreakerErrorThresholdPercentage(50)
//全闭变半开的时间
.withCircuitBreakerSleepWindowInMilliseconds(5000);
//操作5个开启短路器
.withCircuitBreakerRequestVolumeThreshold(5)
//设置线程隔离
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)
//设置断路器的开启时间为5秒
.withCircuitBreakerSleepWindowInMilliseconds(5000);
return HystrixCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
.andCommandPropertiesDefaults(setter);
}
};
return setterFactory;
}
}