Spring cloud系列七 为@Feign中集成的Ribbon进行个性化配置

来源:互联网 发布:邻里中国网php面试题 编辑:程序博客网 时间:2024/05/29 02:13

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

# portserver:  port: 11083spring:  application:    # 本服务注册到注册到服务器的名称, 这个名称就是后面调用服务时的服务标识符    name: cloud-ribbon-serviceeureka:  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:

提供简单的服务接口

  1. ribbonClientCall:立即返回一个随机字符串
  2. ribbonClientCallSleep:休息5s,然后返回一个随机字符串。用于测试ribbon的超时调用
@RestControllerpublic 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
此类初始化ribbon需要的组件对象

@Configurationpublic 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 {}

4.4. 通过属性文件配置ribbon

配置ribbon的参数,除了@RibbonClients和@RibbonClient外,还可以使用属性文件。这里将属性配置到application-ribbon.yml中,内容如下:

# 配置ribbon的参数,其他参数为CommonClientConfigKeyribbon:  # Connect timeout used by Apache HttpClient  ConnectTimeout: 10000  # Read timeout used by Apache HttpClient  ReadTimeout: 10000cloud-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组件的名称如下,都是以 <clientName>.ribbon.开头,<clientName>不可省略:NFLoadBalancerClassName: 实现接口 ILoadBalancerNFLoadBalancerRuleClassName: 实现接口 IRuleNFLoadBalancerPingClassName: 实现接口 IPingNIWSServerListClassName: 实现接口 ServerListNIWSServerListFilterClassName 实现接口 ServerListFilter

4.5 bootstrap-ribbon.yml

spring cloud的通用配置

# portserver:  port: 11701spring:  application:    # 本服务注册到注册到服务器的名称, 这个名称就是后面调用服务时的服务标识符    name: cloud-ribbon-consumereureka:  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

4.6 启动类RibbonCloudConsumerApplication

@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);    }}

5. 测试

5.1. 服务启动

  1. 启动cloud-registration-center下的SimpleCloudRegistrationCenterApplication
  2. 启动cloud-service-ribbon下的RibbonCloudServiceApplication
  3. 启动下cloud-consumer-ribbon的RibbonCloudConsumerApplication

如果为了更好的测试ribbon负载的功能,可以启动多个RibbonCloudServiceApplication,只需要修改工程的bootstrap-ribbon.yml里配置的端口即可

5.2. 调用/ribbon/simple

执行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  ... 

5.3. 调用/ribbon/sleep

执行http://127.0.0.1:11701/ribbon/sleep,此方法会执行5s,如果没有配置以下参数,则调用会超时失败。
如果配置如下参数后,尽管超时5s,但是方法会执行成功。说明ribbon的配置成功

ribbon:  # Connect timeout used by Apache HttpClient  ConnectTimeout: 10000

6. 代码

以上的详细的代码见下面
github代码,请尽量使用tag v0.6,不要使用master,因为我不能保证master代码一直不变

阅读全文
0 0
原创粉丝点击