ribbon client load balancing

Client load balancing: The client will have a list of server addresses, select a server through a load balancing algorithm before sending a request, and then access it.

illustrate

The ribbon client needs to be used with zuul.

#  zuul 配置
zuul:
  routes:
    custom:  # route key, 随意,不重复即可
      path: /target/page/** # 路由表达式
      serviceId: customId # 客户端id
      stripPrefix: false   # 路由时保留 表达式前缀

# ribbon
ribbon:
  eureka:
    enabled: false #不使用注册中心

When ribbon generates a service list, only the server address + port information will be retained, and the following paths will be ignored, so the zuul configuration stripPrefix=truereserves the prefix for routing to the correct address.

1. Configuration file method

1.1 ribbon client configuration

# ribbon 客户端,名字与zuul里serverId保持一致
customId:
  ribbon:
    # 服务端列表
    listOfServers: http://localhost:8081,http://localhost:8081

1.2 Reviews

Advantages: Through simple configuration, the client can be generated without extra code, which is suitable for the situation where the client service list is stable and the load balancing does not need special customization.

Disadvantages: The client load uses the default implementation of ribbon, which cannot meet specific scenarios.

2. Annotation method

2.1 Annotating RibbonClient

Spring cloud allows us to customize the client through annotation configuration, and we need to remove the ribbon client configuration in the configuration file.

@Configuration
@RibbonClient(name = "customId", configuration = CustomConfiguration.class)
public class TestConfiguration {
}

A client named customId will be generated. This configuration cannot be scanned by the main application context and needs to be placed in a separate package (I suspect that configuring multiple clients may cause configuration data sharing problems).

class CustomConfiguration{

	@Bean
	public IRule ribbonRule() {
		return new BestAvailableRule();
	}

	@Bean
	public IPing ribbonPing() {
		return new PingUrl();
	}

	  @Bean
    public ServerList<Server> ribbonServerList(){
        //配置服务列表
        Server[] serveArray = new Server[3];
        serveArray[0] = (new Server("localhost", 9999));

        ServerList<Server> serverServerList = new StaticServerList<>(serveArray);

        return serverServerList;
    }

	@Bean
	public ServerListSubsetFilter serverListFilter() {
		ServerListSubsetFilter filter = new ServerListSubsetFilter();
		return filter;
	}
}

2.2 Reviews

Advantages: With a little code, the customization problem of special needs in load balancing is solved.

Disadvantage: Can't solve the problem of dynamically changing the client service list.

3 Dynamic configuration methods

3.1 archaius

When selecting a service in load balancing, ribbon needs to read the service list information of the client. You can obtain the load balancer through clientId and change the relevant configuration information, but the refresh mechanism of ribbon is to read data from the configuration by default, and the data in the service list will be replaced quickly. If we implement the configuration that changes on demand , we listOfServerscan achieve the purpose of dynamically refreshing the client service list.

The role of archius is to dynamically change the configuration.

 public static void createRibbonClient(){
        //设置:配置项 (ribbon 会读取此配置)
        //根据 archaius 动态配置的特性 , 服务列表更新时只需重新赋值即可
        ConfigurationManager.getConfigInstance().setProperty(CLIENT_ID + ".ribbon.listOfServers", LIST_SERVERS);

        // 获取客户端, 若不存在则创建
        ClientFactory.getNamedClient(CLIENT_ID);

        // 负载均衡服务
        DynamicServerListLoadBalancer serverListLoadBalancer = (DynamicServerListLoadBalancer) ClientFactory.getNamedLoadBalancer(CLIENT_ID);
        
        //根据需求定制化负载属性:
        //轮询方式、 Rule、 Ping 等
		//serverListLoadBalancer.setPing(new PingUrl());

    }

source code

client dynamic loadbalance

Reference resources

Client Side Load Balancer: Ribbon
Spring Cloud Source Code Analysis (2) Ribbon
Archaius

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324408594&siteId=291194637