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

无注册中心版本!!

适用于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)));
    }
}

负载均衡结果

 

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud是一个用于构建分布式系统的开源框架,它基于Spring Boot提供了一套完整的微服务解决方案。下面是Spring Cloud的发展历程: 1. 2014年:Spring Cloud的前身是Netflix公司的一些开源项目,如Eureka、Hystrix、Ribbon等。这些项目提供了服务注册与发现、断路器、负载均衡等功能,为微服务架构提供了基础支持。 2. 2015年:Spring Cloud正式成为Spring官方项目的一部分,发布了第一个本。这个本主要包括了对Netflix开源项目的整合和封装,使得开发者可以更方便地使用这些功能。 3. 2016年:Spring Cloud发布了Dalston本,引入了更多的组件和功能。其中包括了Zuul网关、Config配置中心、Feign声明式HTTP客户端等。这些组件进一步提升了微服务架构的可扩展性和灵活性。 4. 2017年:Spring Cloud发布了Edgware本,引入了更多的功能和改进。其中包括了Sleuth分布式跟踪、Zipkin链路追踪、Spring Cloud Stream消息驱动等。这些功能使得微服务架构更加易于监控和调试。 5. 2018年:Spring Cloud发布了Finchley本,继续引入了新的功能和改进。其中包括了Spring Cloud Gateway网关、Spring Cloud Circuit Breaker断路器等。这些功能进一步提升了微服务架构的性能和可靠性。 6. 2019年:Spring Cloud发布了Greenwich本,继续完善了现有功能并引入了新的功能。其中包括了Spring Cloud LoadBalancer负载均衡、Spring Cloud Function函数式编程等。这些功能使得微服务架构更加灵活和高效。 7. 2020年:Spring Cloud发布了Hoxton本,继续改进了现有功能并引入了新的功能。其中包括了Spring Cloud Alibaba组件、Spring Cloud Kubernetes支持等。这些功能进一步扩展了Spring Cloud在云原生领域的应用范围。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值