正文
1. 概述
上文
Spring cloud系列六 Ribbon的功能概述、主要组件和属性文件配置
已经介绍了ribbon的主要组件和用法。我们已知Spring Cloud的@Feign已经集成了ribbon的功能。本文我们介绍如何为集成在@Feign中的ribbon进行个性化配置。
主要内容如下:
-
为集成在@Feign中的ribbon进行个性化配置
-
通过@RibbonClients和@RibbonClient配置ribbon
-
通过属性文件配置ribbon
2. 工程说明
本文涉及到以下几个工程:
-
cloud-registration-center:注册中心,关于这个工程,详细见之前的本系列文章
-
cloud-service-ribbon:模拟服务端,注册到注册中心并对外提供调用接口
-
cloud-consumer-ribbon:模拟客户端,配置个性化ribbon,通过@Fegin(含ribbon)调用服务接口
3. cloud-service-ribbon
模拟服务端,注册到注册中心并对外提供调用接口。这个服务非常简单,对外提供2个http rest接口供客户端调用
3.1. pom.xml
<parent>
<artifactId>cloudgparent</artifactId>
<groupId>com.hry.spring.cloud</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../cloud-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-service-ribbon</artifactId>
3.2. bootstrap-ribbon.yml
配置注册中心的地址和自己的服务名称cloud-ribbon-service
# port
server:
port: 11083
spring:
application:
# 本服务注册到注册到服务器的名称, 这个名称就是后面调用服务时的服务标识符
name: cloud-ribbon-service
eureka:
client:
serviceUrl:
# 服务器注册/获取服务器的zone
defaultZone: http://127.0.0.1:10761/eureka/
# defaultZone: http://192.168.21.3:10761/eureka/,http://192.168.21.4:10761/eureka/
instance:
prefer-ip-address: true
3.3. SimpleCtl.java:
提供简单的服务接口
-
ribbonClientCall:立即返回一个随机字符串
-
ribbonClientCallSleep:休息5s,然后返回一个随机字符串。用于测试ribbon的超时调用
@RestController
public class SimpleCtl {
private AtomicInteger count = new AtomicInteger();
private AtomicInteger sleepCount = new AtomicInteger();
@RequestMapping(value="/ribbon/simple")
public String ribbonClientCall(){
int newCount = count.incrementAndGet();
return "ribbon" + newCount + ": " + ThreadLocalRandom.current().nextInt(1000);
}
@RequestMapping(value="/ribbon/sleep")
public String ribbonClientCallSleep(){
try {
Thread.sleep(1000 * 5);
} catch (InterruptedException e) {
e.printStackTrace();
}
int newCount = sleepCount.incrementAndGet();
return "ribbon sleep " + newCount + ": " + ThreadLocalRandom.current().nextInt(1000);
}
}
3.4. 启动类 RibbonCloudServiceApplication
@SpringBootApplication
@EnableDiscoveryClient // 通过eureka注册服务注册中心
public class RibbonCloudServiceApplication {
public static void main(String[] args) {
args = new String[1];
args[0] = "--spring.profiles.active=ribbon";
SpringApplication.run(RibbonCloudServiceApplication.class, args);
}
/**
* 使用fastjson做为json的解析器
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
// fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}
4. cloud-consumer-ribbon
ribbon的客户端,使用ribbon调用cloud-service-ribbon的接口
4.1. feign配置
配置feign调用服务cloud-ribbon-service的url为/ribbon/simple和/ribbon/sleep接口
@FeignClient(name="cloud-ribbon-service")
public interface IRibbonClient {
@RequestMapping(method = RequestMethod.GET, value = "/ribbon/simple")
String ribbonClientCall();
@RequestMapping(method = RequestMethod.GET, value="/ribbon/sleep")
String ribbonClientCallSleep();
}
4.2 自定义ribbon的组件类
MyRule
定义自己的IRule,继承RoundRobinRule ,对重写IRule接口的方法,在调用父方法之前,多了一条打印语句。如果程序运行时,控制台输出这条语句,说明我们使用自定义组件成功。
public class MyRule extends RoundRobinRule {
@Override
public Server choose(Object key) {
System.out.println("MyRule choose " + key + " ... ");
return super.choose(key);
}
@Override
public void setLoadBalancer(ILoadBalancer lb) {
System.out.println("MyRule setLoadBalancer ... ");
super.setLoadBalancer(lb);
}
@Override
public ILoadBalancer getLoadBalancer() {
System.out.println("MyRule getLoadBalancer ... ");
return super.getLoadBalancer();
}
}
MyPingUrl
定义自己的IPing,通过代理的方式实现。在调用代理方法后,多了一条打印语句。如果程序运行时,控制台输出这条语句,说明我们使用自定义组件成功。
public class MyPingUrl implements IPing {
private IPing pingUrl;
public MyPingUrl(IPing ping){
this.pingUrl = ping;
}
@Override
public boolean isAlive(Server server) {
boolean isAlive = pingUrl.isAlive(server);
System.out.println("MyPingUrl " + server.getHostPort() + " isAlive = " + isAlive + "; info=" + server.toString());
return isAlive;
}
}
MyDiscoveryEnabledNIWSServerList
定义自己的ServerList,继承DiscoveryEnabledNIWSServerList ,对重写ServerList接口的方法,在调用父方法之前,多了一条打印语句。如果程序运行时,控制台输出这条语句,说明我们使用自定义组件成功。
public class MyDiscoveryEnabledNIWSServerList extends DiscoveryEnabledNIWSServerList {
public List<DiscoveryEnabledServer> getInitialListOfServers() {
System.out.println("MyDiscoveryEnabledNIWSServerList getInitialListOfServers ... ");
return super.getInitialListOfServers();
}
@Override
public List<DiscoveryEnabledServer> getUpdatedListOfServers(){
System.out.println("MyDiscoveryEnabledNIWSServerList getUpdatedListOfServers ... ");
return super.getUpdatedListOfServers();
}
}
4.3. 通过@RibbonClients和@RibbonClient配置自定义的组件
MyDefaultRibbonConfig