【Spring Cloud总结】10.Ribbon脱离Eureka使用

最近忙工作入职的事(有些麻烦),所以这两周的更新干货和篇幅少了一些,等工作稳定下来就会正常更新~爱你们~

接上篇《9.使用配置文件自定义Ribbon Client》  Spring Cloud版本为Finchley.SR2版

上一篇我们介绍了如何使用配置文件来自定义Ribbon Client,可以看到我们之前都是在连接了eureka server注册中心进行Ribbon的使用的,但是有些时候单机环境没有连接eureka,此时我们需要用另外一种方式使用Ribbon。
本部分官方文档:https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html#netflix-ribbon-starter

在官方文档的“How to Use Ribbon Without Eureka”章节,对在没有连接eureka的时候,如何使用Ribbon进行了说明:

16.6 Example: How to Use Ribbon Without Eureka
Eureka is a convenient way to abstract the discovery of remote servers so that you do not have to hard code their URLs in clients. However, if you prefer not to use Eureka, Ribbon and Feign also work. Suppose you have declared a @RibbonClient for "stores", and Eureka is not in use (and not even on the classpath). The Ribbon client defaults to a configured server list. You can supply the configuration as follows:

application.yml. 

stores:
  ribbon:
    listOfServers: example.com,google.com

译文:

Eureka提供了一种抽象的发现远程服务的便捷的方式,这样你就不必在客户端代码中硬编码服务端的URL,但是如果你不用它,那么Ribbon和Feign也是经得起考验的。假设,你没有使用Eureka,并且你用@FeignClient声明了一个"stores"服务,这个时候Ribbon Client默认生成一个配置的服务列表,当然你也可以提供下面这样的配置:

application.yml. 

stores:
  ribbon:
    listOfServers: example.com,google.com

我们下面在之前编写的实例上进行测试,但是由于我们原来的实例工程上都使用了eureka,这里我们在测试的时候,不需要将eureka的依赖以及配置去除来测试这个功能,官方文档中提供了Ribbon禁用eureka的配置(在application.yml中):

ribbon:
  eureka:
   enabled: false

打开编译器,找到microserver-consumer-movie工程(即服务消费者),我们在其配置文件application.yml中添加Ribbon禁用eureka的配置:

ribbon:
  eureka:
   enabled: false

然后我们配置请求user微服务的时候,只请求7900端口的那个服务(开7900和7902两个user服务):

microserver-provider-user:
  ribbon:
    listOfServers: localhost:7900

然后我们分别启动eureka和7900以及7902端口的user服务,和movie本身的服务:
【Spring Cloud总结】10.Ribbon脱离Eureka使用_第1张图片
启动后观察eureka中的服务列表,可以看到服务消费者和服务提供者都已经启动成功:
【Spring Cloud总结】10.Ribbon脱离Eureka使用_第2张图片
因为我们在movie服务中添加了ribbon禁用eureka的配置,那么当使用ribbon访问服务时,不再从eureka中获取服务列表,而是从application.yml中配置的listOfServers获取服务列表。我们访问之前编写的“/testRibbon”服务:

@GetMapping("/testRibbon")
public String testRibbon(){
    ServiceInstance serviceInstance  = this.loadBalanceClient.choose("microserver-provider-user");
    System.out.println(serviceInstance.getServiceId()+":"+serviceInstance.getHost()+":"+serviceInstance.getPort());
    return "test success! o(* ̄︶ ̄*)o";
}

我们一口气连续访问8次,在浏览器中看到访问成功:
【Spring Cloud总结】10.Ribbon脱离Eureka使用_第3张图片
在编译器的控制台上,我们可以看到,ribbon的访问结果就是我们配置的serverList中仅有的7900节点的服务:
【Spring Cloud总结】10.Ribbon脱离Eureka使用_第4张图片
至此,我们脱离eureka的ribbon使用配置介绍完毕。

有时候我们需要直接获取Ribbon负载均衡后的结果,用于服务信息的获取等操作,这里官方提供了直接使用Ribbon的API获取负载均衡结果的功能,样例:

public class MyClass {
    @Autowired
    private LoadBalancerClient loadBalancer;

    public void doStuff() {
        ServiceInstance instance = loadBalancer.choose("stores");
        URI storesUri = URI.create(String.format("http://%s:%s", instance.getHost(), instance.getPort()));
        // ... 可编写针对URI对象的任何操作
    }
}

至此,我们有关Ribbon的操作就全部介绍完毕了,下一篇我们来介绍如何使用Feign来实现一个声明式Web服务客户端,简化我们的接口调用。

参考:《51CTO学院Spring Cloud高级视频》
https://www.cnblogs.com/cjsblog/p/7986628.html

转载请注明出处:https://blog.csdn.net/acmman/article/details/97764130

你可能感兴趣的:(Spring,Cloud,Spring,Cloud全面入门学习)