• 服务消费者


    Ribbon

    Ribbon是一个基于HTTP和TCP客户端的负载均衡器。Feign中也使用Ribbon.

    Ribbon可以在通过客户端中配置的ribbonServerList服务端列表去轮询访问以达到均衡负载的作用。

    当Ribbon与Eureka联合使用时,ribbonServerList会被DiscoveryEnabledNIWSServerList重写,扩展成从Eureka注册中心中获取服务端列表。同时它也会用NIWSDiscoveryPing来取代IPing,它将职责委托给Eureka来确定服务端是否已经启动。

     

    pom.xml依赖

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>

     

    在应用主类中,通过@EnableDiscoveryClient注解来添加发现服务能力。创建RestTemplate实例,并通过@LoadBalanced注解开启均衡负载能力。

    @SpringBootApplication
    @EnableDiscoveryClient  //发现服务的能力
    public class RibbonApplication {
        
        //rest 请求对象,具有负载均衡的能力
        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
        public static void main(String[] args) {
            SpringApplication.run(RibbonApplication.class, args);
        }
    }

    创建ConsumerController来消费COMPUTE-SERVICE的add服务。通过直接RestTemplate来调用服务,计算10 + 20的值。

    @RestController
    public class ConsumerController {
        @Autowired
        RestTemplate restTemplate;
        @RequestMapping(value = "/add", method = RequestMethod.GET)
        public String add() {
         //调用服务
    return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody(); } }

    application.properties中配置eureka服务注册中心。

    Ribbon总结:

    • 第一步先选择 Eureka Server, 它优先选择在同一个Zone且负载较少的Server;
    • 第二步再根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址。其中Ribbon提供了三种策略:轮询、断路器和根据响应时间加权。

                 

    Feign

    Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

    在应用主类中通过@EnableFeignClients注解开启Feign功能

    @SpringBootApplication
    @EnableDiscoveryClient 
    @EnableFeignClients  //开启feign
    public class FeignApplication {
        public static void main(String[] args) {
            SpringApplication.run(FeignApplication.class, args);
        }
    }

    定义compute-service服务的接口

    @FeignClient("compute-service")
    public interface ComputeClient {
        @RequestMapping(method = RequestMethod.GET, value = "/add")
        Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
    }
    • 使用@FeignClient("compute-service")注解来绑定该接口对应compute-service服务
    • 通过Spring MVC的注解来配置compute-service服务下的具体实现,注意:定义的url 必须与提供服务的url一致,包括请求方式、参数名

    在web层中调用上面定义的ComputeClient 

    @RestController
    public class ConsumerController {
    
    @Autowired ComputeClient computeClient;//feign 的代理类
    @RequestMapping(value
    = "/add", method = RequestMethod.GET) public Integer add() {
    //调用远程服务方法
    return computeClient.add(10, 20); } }

     application.properties中配置eureka服务注册中心。

    Feign总结:

    • 我们使用Feign提供的注解编写HTTP接口的客户端代码非常简单, 只需要声明一个Java接口加上少量注解即可完成。
    • Feign会帮我们处理好一切. 根据我们的接口声明, Feign会在Spring容器启动之后, 将生成的代理类注入, 所以我们不需要写HTTP调用的实现代码就能完成REST接口的调用.
    • Feign服务客户端 定义的请求url必须与服务提供者url一致。
    • Feign服务客户端中的接口名、返回对象可以任意定义。但对象中的属性类型和属性名必须一致,与两个对象中的属性顺序和数量无关
    • 启动 Eureka注册中心、服务提供者、Feign服务客户端,然后 Eureka注册中心挂掉时,Feign服务客户端消费服务是不受影响的。
  • 相关阅读:
    R语言介绍
    mysql存储过程和函数的操作
    在SSRS自动化报表中创建共享数据源
    在python中实现数据库下group by功能
    MySQL中创建表及导入文件
    python下各种包的安装
    windows下python2.7.11的安装
    面向对象(异常)
    面向对象(内部类)
    面向对象(Object类)
  • 原文地址:https://www.cnblogs.com/zhangjianbin/p/6262077.html
Copyright © 2020-2023  润新知