无注册中心版本!!
适用于spring-cloud 版本 Hoxton.SR7,其他版本没有尝试
与feign集成
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
application.yml 关闭ribbon
spring:
cloud:
loadbalancer:
ribbon:
enabled: false
定义 feign 接口 (服务调用方)
// 不用再指定 url 属性
@FeignClient(name = "customer")
public interface FeignService {
@GetMapping("/test1/")
String test1();
}
沿用loadbalancer的基配置
/**
* loadbalancer 服务实例列表
*/
public class DemoServiceInstanceListSuppler implements ServiceInstanceListSupplier {
private final String serviceId;
DemoServiceInstanceListSuppler(String serviceId) {
this.serviceId = serviceId;
}
@Override
public String getServiceId() {
return serviceId;
}
/**
* 如果采用注册中心,则不需要当前方法的配置,loadbalancer 会获取注册中心的服务注册列表自动进行路由
* 如果不采用注册中心,一种是像下面的方法一样在代码中配置,一种是可以把服务列表信息配置在属性文件中
*/
@Override
public Flux<List<ServiceInstance>> get() {
return Flux.just(Arrays
.asList(new DefaultServiceInstance(serviceId + "1", serviceId, "localhost", 8085, false),
new DefaultServiceInstance(serviceId + "2", serviceId, "localhost", 8084, false)));
}
}
@Configuration
// 定义服务提供方的服务名称为“customer”
@LoadBalancerClient(name = "customer", configuration = LbConfiguration.class)
public class LbConfiguration {
@Bean
@Primary
ServiceInstanceListSupplier serviceInstanceListSupplier() {
return new DemoServiceInstanceListSuppler("customer");
}
@LoadBalanced
@Bean
WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
}
controller 调用
@Autowired
private FeignService feignService;
@GetMapping("/test1")
public String test1(){
return feignService.test1();
}
一样可以实现feign 接口的负载均衡。
与gateway 集成
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
application.yml
spring:
cloud:
gateway:
routes:
- id: path
uri: lb://demo
predicates:
- Path=/test
loadbalancer:
ribbon:
enabled: false
相关配置代码
@Configuration
@LoadBalancerClient(name = "demo", configuration = LbConfiguration.class)
public class LbConfiguration {
@Bean
@Primary
ServiceInstanceListSupplier serviceInstanceListSupplier() {
return new DemoServiceInstanceListSuppler("demo");
}
@LoadBalanced
@Bean
WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
}
/**
* loadbalancer 服务实例列表
*/
public class DemoServiceInstanceListSuppler implements ServiceInstanceListSupplier {
private final String serviceId;
DemoServiceInstanceListSuppler(String serviceId) {
this.serviceId = serviceId;
}
@Override
public String getServiceId() {
return serviceId;
}
@Override
public Flux<List<ServiceInstance>> get() {
return Flux.just(Arrays
.asList(new DefaultServiceInstance(serviceId + "1", serviceId, "localhost", 8081, false),
new DefaultServiceInstance(serviceId + "2", serviceId, "localhost", 8085, false)));
}
}
负载均衡结果