1. 程式人生 > 其它 >Ribbon(5)--->spring-cloud-netflix-ribbon 中配置說明

Ribbon(5)--->spring-cloud-netflix-ribbon 中配置說明

技術標籤:springcloud-Netflixribbonspring cloud

1、在使用spring-cloud-netflix-ribbon的時候我們可以使用如下的配置方式來進行配置

#配置order-service 服務的地址
order-service.ribbon.listOfServers=http://localhost:7071,http://localhost:7072

#請求連線超時時間,實現原理是通過設定請求的http.connection.timeout
ribbon.ConnectTimeout=1800

#請求處理的超時時間,實現原理是通過設定請求的http.socket.timeout來實現的
ribbon.ReadTimeout=500

#是否對所有操作請求都進行重試
order-service.ribbon.OkToRetryOnAllOperations=false

#切換例項的重試次數
order-service.ribbon.MaxAutoRetriesNextServer=3

#對當前例項的重試次數
order-service.ribbon.maxAutoRetries=2

這種配置方式就是在配置名稱=order-service 的ribbonClient

當然我們也可以通過@RibbonClient來進行配置:

@Configuration
@RibbonClients(
        value = {
           @RibbonClient(name = "order-service", configuration = {MyRibbonClientConfiguration.OrderRibbonClientConfiguration.class}),
           @RibbonClient(name = "product-service",configuration = {MyRibbonClientConfiguration.OrderRibbonClientConfiguration.class})
        },
      defaultConfiguration = {MyRibbonClientConfiguration.OrderRibbonClientConfiguration.class}
)
public class MyRibbonClientConfiguration {

    @Configuration
    public class OrderRibbonClientConfiguration {

        public static final int DEFAULT_CONNECT_TIMEOUT = 1000;
        public static final int DEFAULT_READ_TIMEOUT = 1000;
        public static final boolean DEFAULT_GZIP_PAYLOAD = true;

        @RibbonClientName
        private String name = "client";

        @Autowired
        private PropertiesFactory propertiesFactory;

        @Bean
        @ConditionalOnMissingBean
        public IClientConfig ribbonClientConfig() {
            DefaultClientConfigImpl config = new DefaultClientConfigImpl();
            config.loadProperties(this.name);
            config.set(CommonClientConfigKey.ConnectTimeout, DEFAULT_CONNECT_TIMEOUT);
            config.set(CommonClientConfigKey.ReadTimeout, DEFAULT_READ_TIMEOUT);
            config.set(CommonClientConfigKey.GZipPayload, DEFAULT_GZIP_PAYLOAD);
            return config;
        }

        @Bean
        @ConditionalOnMissingBean
        public IRule ribbonRule(IClientConfig config) {
            if (this.propertiesFactory.isSet(IRule.class, name)) {
                return this.propertiesFactory.get(IRule.class, config, name);
            }
            ZoneAvoidanceRule rule = new ZoneAvoidanceRule();
            rule.initWithNiwsConfig(config);
            return rule;
        }

        @Bean
        @ConditionalOnMissingBean
        public IPing ribbonPing(IClientConfig config) {
            if (this.propertiesFactory.isSet(IPing.class, name)) {
                return this.propertiesFactory.get(IPing.class, config, name);
            }
            return new DummyPing();
        }

        @Bean
        @ConditionalOnMissingBean
        @SuppressWarnings("unchecked")
        public ServerList<Server> ribbonServerList(IClientConfig config) {
            if (this.propertiesFactory.isSet(ServerList.class, name)) {
                return this.propertiesFactory.get(ServerList.class, config, name);
            }
            ConfigurationBasedServerList serverList = new ConfigurationBasedServerList();
            serverList.initWithNiwsConfig(config);
            return serverList;
        }
    }
}

2、配置注意點:

#開啟使用 http client 進行請求傳送,注意不開啟的話超時時間將會失效。
# !!!!注意單獨是用ribbon的時候只有開啟ribbon.http.client.enabled 或者ribbon.restclient.enabled的時候如下功能才生效:
#            1、ConnectTimeout
#            2、ReadTimeout
#            3、重試相關的功能

#ribbon.http.client.enabled=true
#ribbon.restclient.enabled=true