我正在尝试从 Ribbon 迁移到 Spring Cloud LoadBalancer,因为 Ribbon 现在处于维护模式,并且我无法使用默认的 spring-boot 2.4.2 版本在 spring 初始化中添加功能区依赖项。我目前正在使用 OpenFeign,并且正在做一些测试,试图将默认的 RoundRobin 负载均衡器配置更改为随机规则。这就是我所拥有的
使用功能区时,我只需要更改默认配置
@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();
}
但我无法使用 spring-cloud-loadbalancer 使其工作。我知道默认情况下包含在 spring-cloud-starter-openfeign 依赖项中,所以我的 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>
让我的 FeignClients 像这样(没有任何 Ribbon 注释)
@FeignClient("first-service")
public interface HelloClient {
@GetMapping("/hello")
String hello();
}
@FeignClient("second-service")
public interface WorldClient {
@GetMapping("/world")
String world();
}
我的 RestController 像这样
@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-service 实例仍在运行并且正在调用备用),但我想将默认负载平衡器算法(RoundRobin)更改为另一个(如随机或任何自定义)到测试差异。我想我必须更改 FeignConfiguration 类中的某些内容,但我已经搜索但没有成功。
希望您能帮助我,这样我就可以摆脱 Ribbon 并毫无问题地开始使用 Spring Cloud LoadBalancer!:C