spring-cloud-loadbalancer 集成 feign、gateway---无注册中心版

无注册中心版本!!

适用于spring-cloud 版本  Hoxton.SR7,其他版本没有尝试

与feign集成

pom.xml


    org.springframework.cloud
    spring-cloud-starter-loadbalancer

    org.springframework.boot
    spring-boot-starter-webflux

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> 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


    org.springframework.cloud
    spring-cloud-starter-loadbalancer

    org.springframework.boot
    spring-boot-starter-webflux

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> get() {
        return Flux.just(Arrays
                .asList(new DefaultServiceInstance(serviceId + "1", serviceId, "localhost", 8081, false),
                        new DefaultServiceInstance(serviceId + "2", serviceId, "localhost", 8085, false)));
    }
}

负载均衡结果

spring-cloud-loadbalancer 集成 feign、gateway---无注册中心版_第1张图片

spring-cloud-loadbalancer 集成 feign、gateway---无注册中心版_第2张图片

 

 

你可能感兴趣的:(loadbalancer,feign,gateway)