更改spring云loadbalancer的默认循环算法

2nc8po8w  于 2021-07-23  发布在  Java
关注(0)|答案(2)|浏览(511)

我正在尝试从ribbon迁移到SpringCloudLoadBalancer,因为ribbon现在处于维护模式,并且无法使用默认的SpringBoot2.4.2版本在SpringInitialize中添加ribbon依赖项。我目前正在使用openfeign,我正在做一些测试,试图将默认的roundrobin负载平衡器配置更改为random rule。这就是我所拥有的
使用ribbon时,我只需更改默认配置

@Configuration
public class RibbonConfiguration {

    @Bean
    public IRule loadBalancingRule() {
//        return new RoundRobinRule();
        return new RandomRule();
    }

}

并在客户机中添加注解@ribbonclient(在mainapplication类中也可以添加)

@FeignClient("first-service")
@RibbonClient(name = "first-service", configuration = RibbonConfiguration.class)
public interface HelloClient {
    @GetMapping("/hello")
    String hello();
}

但我无法使用SpringCloudLoadBalancer使其正常工作。我知道在springcloudstarteropenfeign依赖项中默认包含了pom,所以我的pom是这样的
pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>edge-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>edge-service</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>2020.0.1</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

有我这样的假客户(没有任何功能区注解)

@FeignClient("first-service")
public interface HelloClient {
    @GetMapping("/hello")
    String hello();
}

@FeignClient("second-service")
public interface WorldClient {
    @GetMapping("/world")
    String world();
}

我的控制器是这样的

@RestController
public class HelloWorldController {
    @Autowired
    private HelloClient helloClient;
    @Autowired
    private WorldClient worldClient;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @GetMapping("/hello-world")
    public String helloWorld() {
        logger.info("Calling the clients");
        return helloClient.hello() + " - " + worldClient.world();
    }
}

在不做任何其他事情的情况下,它似乎可以实现负载平衡(两个hello服务示例仍在运行,并且正在调用alternate),但是我想将默认的负载均衡器算法(roundrobin)更改为另一个(比如random或any custom)来测试差异。我想我必须在faignconfiguration类中更改一些内容,但是我搜索没有成功。
希望你能帮我摆脱ribbon,开始使用SpringCloudLoadBalancer而没有问题!:c

7y4bm7vi

7y4bm7vi1#

功能区配置将不适用于spring cloud loadbalancer。如果您添加springcloudstarterloadbalancer,那么将为您创建默认的roundrobin bean,因此您不需要设置它。但是,如果要切换到随机算法,则需要添加bean以覆盖配置中的默认bean,如下所示:

public class CustomLoadBalancerConfiguration {

@Bean
ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,
        LoadBalancerClientFactory loadBalancerClientFactory) {
    String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
    return new RandomLoadBalancer(loadBalancerClientFactory
            .getLazyProvider(name, ServiceInstanceListSupplier.class),
            name); 
  }
}

你可以在这里了解更多。
一旦添加了带有新bean的loadbalancer配置类,就可以将客户机设置为与 @LoadBalancerClient 或者 @LoadBalancerClients 注解。你可以在文档中详细阅读如何做。

am46iovg

am46iovg2#

我认为您只需要创建reactorserviceinstanceloadbalancer类型的bean,它将被用来代替默认的循环bean。

@Configuration
public class LoadBalancingConfiguration {

    @Bean
    public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment,
        LoadBalancerClientFactory loadBalancerClientFactory) {
      String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
      return new RandomLoadBalancer(
          loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
    }

}

上面的示例是从spring云loadbalancer项目的loadbalancerclientconfiguration类中的默认bean创建复制的

@Bean
@ConditionalOnMissingBean
public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment,
        LoadBalancerClientFactory loadBalancerClientFactory) {
    String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
    return new RoundRobinLoadBalancer(
            loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
}

我正在努力弄清楚这一切目前是如何工作的,这样我就可以让负载平衡代码在不需要发现服务器的情况下使用wiremock服务器进行测试。但是,我还没有找到多少有用的文档来解释如何从ribbon迁移到spring负载平衡器。
下面的代码可以让我在测试中使用wiremock。假设wiremock服务器在本地运行并监听端口9999。

@Bean
public RoundRobinLoadBalancer loadBalancer() {
  DefaultServiceInstance wiremock = new DefaultServiceInstance("", "", "localhost", 9999, false);
  return new RoundRobinLoadBalancer(ServiceInstanceListSuppliers.toProvider("", wiremock), "");
}

相关问题