上文Spring cloud系列六 Ribbon的功能概述、主要组件和属性文件配置已经介绍了ribbon的主要组件和用法。我们已知Spring Cloud的@Feign已经集成了ribbon的功能。本文我们介绍如何为集成在@Feign中的ribbon进行个性化配置。
主要内容如下:
本文涉及到以下几个工程:
模拟服务端,注册到注册中心并对外提供调用接口。这个服务非常简单,对外提供2个http rest接口供客户端调用
<parent>
<artifactId>cloudgparentartifactId>
<groupId>com.hry.spring.cloudgroupId>
<version>0.0.1-SNAPSHOTversion>
<relativePath>../cloud-parent/pom.xmlrelativePath>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-service-ribbonartifactId>
配置注册中心的地址和自己的服务名称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
提供简单的服务接口
@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);
}
}
@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);
}
}
ribbon的客户端,使用ribbon调用cloud-service-ribbon的接口
配置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();
}
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 getInitialListOfServers() {
System.out.println("MyDiscoveryEnabledNIWSServerList getInitialListOfServers ... ");
return super.getInitialListOfServers();
}
@Override
public List getUpdatedListOfServers(){
System.out.println("MyDiscoveryEnabledNIWSServerList getUpdatedListOfServers ... ");
return super.getUpdatedListOfServers();
}
}
MyDefaultRibbonConfig
此类初始化ribbon需要的组件对象
@Configuration
public class MyDefaultRibbonConfig {
// @Bean
// public IRule ribbonRule() {
// return new MyRule();
// }
@Bean
public IPing ribbonPing() {
return new MyPingUrl(new NIWSDiscoveryPing());
}
}
MyRibbonClients
@RibbonClients:设置所有的@RibbonClient的默认配置。参数defaultConfiguration 指定初始化类
@RibbonClients(defaultConfiguration = MyDefaultRibbonConfig.class)
public class MyRibbonClients {
}
@RibbonClients定义所有的ribbon客户的默认配置,如果只为特定ribbon客户提供配置,可以使用@RibbonClient,但是必须保证MyDefaultRibbonConfig不能被@ComponentScan扫描掉,否则MyDefaultRibbonConfig会被所有@RibbonClients共享
/**
* 如果使用@RibbonClient,则MyDefaultRibbonConfig必须使用@Configuration 注解。但是MyDefaultRibbonConfig不能被@ComponentScan扫描掉,否则MyDefaultRibbonConfig会被所有@RibbonClients共享 *
*/
@RibbonClient(name = "foo", configuration = MyDefaultRibbonConfig.class)
public class MyRibbonClients {
}
配置ribbon的参数,除了@RibbonClients和@RibbonClient外,还可以使用属性文件。这里将属性配置到application-ribbon.yml中,内容如下:
# 配置ribbon的参数,其他参数为CommonClientConfigKey
ribbon:
# Connect timeout used by Apache HttpClient
ConnectTimeout: 10000
# Read timeout used by Apache HttpClient
ReadTimeout: 10000
cloud-ribbon-service:
ribbon:
NIWSServerListClassName: com.hry.spring.cloud.consumer.ribbon.ribbonclient.self.MyDiscoveryEnabledNIWSServerList
NFLoadBalancerRuleClassName: com.hry.spring.cloud.consumer.ribbon.ribbonclient.self.MyRule
参数说明:
1. ribbon.*:配置ribbon的参数,其他参数见com.netflix.client.config.CommonClientConfigKey
2. cloud-ribbon-service.ribbon.*:为名为”cloud-ribbon-service”的ribbon客户配置自己的特定的组件。这里配置只能作用在名为cloud-ribbon-service的客户端。这个名称必须和”@FeignClient(name=”cloud-ribbon-service”)” name值相同
其它的ribbon组件的配置参数如下,
可配置的ribbon组件的名称如下,都是以 .ribbon.开头,不可省略:
NFLoadBalancerClassName: 实现接口 ILoadBalancer
NFLoadBalancerRuleClassName: 实现接口 IRule
NFLoadBalancerPingClassName: 实现接口 IPing
NIWSServerListClassName: 实现接口 ServerList
NIWSServerListFilterClassName 实现接口 ServerListFilter
spring cloud的通用配置
# port
server:
port: 11701
spring:
application:
# 本服务注册到注册到服务器的名称, 这个名称就是后面调用服务时的服务标识符
name: cloud-ribbon-consumer
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
@SpringBootApplication
@EnableEurekaClient // 配置本应用将使用服务注册和服务发现
@EnableFeignClients // 启用feign REST访问
public class RibbonCloudConsumerApplication {
public static void main(String[] args) {
args = new String[1];
args[0] = "--spring.profiles.active=ribbon";
SpringApplication.run(RibbonCloudConsumerApplication.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);
}
}
如果为了更好的测试ribbon负载的功能,可以启动多个RibbonCloudServiceApplication,只需要修改工程的bootstrap-ribbon.yml里配置的端口即可
执行http://127.0.0.1:11701/ribbon/simple:返回”rsp: ribbon18: 478”
查看控制台输出,根据控制台的输出,说明我们的自定义的ribbon组件配置成功了
// 说明我们配置IRule已经启作用
MyRule choose null ...
MyRule - getLoadBalancer ...
// 说明我们配置IPing已经启作用
MyPingUrl 10.242.5.144:11083 isAlive = true; info=10.242.5.144:11083
// 说明我们配置ServerList已经启作用
MyDiscoveryEnabledNIWSServerList getUpdatedListOfServers ...
执行http://127.0.0.1:11701/ribbon/sleep,此方法会执行5s,如果没有配置以下参数,则调用会超时失败。
如果配置如下参数后,尽管超时5s,但是方法会执行成功。说明ribbon的配置成功
ribbon:
# Connect timeout used by Apache HttpClient
ConnectTimeout: 10000
以上的详细的代码见下面
github代码,请尽量使用tag v0.6,不要使用master,因为我不能保证master代码一直不变