美文网首页
spring boot微服务-第四部分:负载均衡 使用Ribbo

spring boot微服务-第四部分:负载均衡 使用Ribbo

作者: sstraybitd | 来源:发表于2020-03-10 16:32 被阅读0次

    在前两节,我们创建了汇率和货币兑换服务,并且这两个服务实现了通信。

    {
      id: 10002,
      from: "EUR",
      to: "INR",
      conversionMultiple: 75,
      quantity: 10000,
      totalCalculatedAmount: 750000,
      port: 8000,
    }
    
    image.png

    但是我么在CCS的组件CurrencyExchangeServiceProxy中使用了FS服务的硬编码url。

    @FeignClient(name="forex-service" url="localhost:8000")
    public interface CurrencyExchangeServiceProxy {
      @GetMapping("/currency-exchange/from/{from}/to/{to}")
      public CurrencyConversionBean retrieveExchangeValue
        (@PathVariable("from") String from, @PathVariable("to") String to);
    }
    

    如果这样,如果新的FS部署了,我们也无法实现对FS服务们的分布负载。
    在这一部分,使用Ribbon,使能客户端负载均衡

    使能Ribbon

    • 在pom文件中添加依赖
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
    
    • 在CurrencyExchangeServiceProxy中使能RibbonClient
    @FeignClient(name="forex-service")
    @RibbonClient(name="forex-service")
    public interface CurrencyExchangeServiceProxy {
    
    • 在application.properties中使配置实例
    forex-service.ribbon.listOfServers=localhost:8000,localhost:8001
    

    8001启动FS服务

    观察效果

    • 8100运行CCS
    • 8000和8001运行FS服务

    当两次请求CCS时分别看到如下效果:

    {
      id: 10002,
      from: "EUR",
      to: "INR",
      conversionMultiple: 75,
      quantity: 10000,
      totalCalculatedAmount: 750000,
      port: 8000,
    }
    
    {
      id: 10002,
      from: "EUR",
      to: "INR",
      conversionMultiple: 75,
      quantity: 10000,
      totalCalculatedAmount: 750000,
      port: 8001,
    }
    

    这样,我们用Ribbon实现了两个FS服务的负载
    但是,我们在CCS的配置文件中硬编码了FS的URL地址。这样引入了一个问题,每次在启动新的FS实例,我们需要更改CCS的配置文件。
    在下一部分,我们使用Eureka解决这个问题。

    相关文章

      网友评论

          本文标题:spring boot微服务-第四部分:负载均衡 使用Ribbo

          本文链接:https://www.haomeiwen.com/subject/iinwdhtx.html