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

2020-03-10  本文已影响0人  sstraybitd

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

{
  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

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
@FeignClient(name="forex-service")
@RibbonClient(name="forex-service")
public interface CurrencyExchangeServiceProxy {
forex-service.ribbon.listOfServers=localhost:8000,localhost:8001

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解决这个问题。

上一篇下一篇

猜你喜欢

热点阅读