zoukankan      html  css  js  c++  java
  • springCloud学习-服务消费者(rest+ribbon)

    1、ribbon简介 

      spring cloud的Netflix中提供了两个组件实现软负载均衡调用:ribbon和feign。

    Ribbon 是一个基于 HTTP 和 TCP 客户端的负载均衡器

    它可以在客户端配置 ribbonServerList(服务端列表),然后轮询请求以实现均衡负载

    它在联合 Eureka 使用时

    ribbonServerList 会被 DiscoveryEnabledNIWSServerList 重写,扩展成从 Eureka 注册中心获取服务端列表

    同时它也会用 NIWSDiscoveryPing 来取代 IPing,它将职责委托给 Eureka 来确定服务端是否已经启动

    2、启动多个client实例

      停止eureka-client服务,打开Run/Debug Configurations


    把图中勾选取消,然后保存,启动,此时启动端口为8762,打开配置文件,将端口修改为8763,再次启动,可以看到在注册中心启动了两个服务

    3、创建服务消费者service-ribbon

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.study</groupId>
        <artifactId>service-ribbon</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>service-ribbon</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>com.study</groupId>
            <artifactId>cloud</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </dependency>
    
        </dependencies>
    
    
    </project>

    配置文件

    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
    server:
      port: 8764
    spring:
      application:
        name: service-ribbon

    在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

    @EnableEurekaClient
    @EnableDiscoveryClient
    @SpringBootApplication
    public class ServiceRibbonApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ServiceRibbonApplication.class, args);
        }
    
        @Bean
        @LoadBalanced
        RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }

    写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的程序名替代了具体的url地址,

    在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:

    @Service
    public class HelloService {
    
        @Autowired
        RestTemplate restTemplate;
    
        public String hiService(String name) {
            return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
        }
    }

    写一个controller,在controller中用调用HelloService 的方法,代码如下:

    @RestController
    public class HelloControler {
    
        @Autowired
        HelloService helloService;
    
        @GetMapping(value = "/hi")
        public String hi(@RequestParam String name) {
            return helloService.hiService( name );
        }
    }

    启动工程,在浏览器中访问http://localhost:8764/hi?name=study 浏览器交替显示:

    hi study ,i am from port:8762
    hi study ,i am from port:8763

    这说明当我们通过调用restTemplate.getForObject(“http://SERVICE-HI/hi?name=“+name,String.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。

    4、此时的架构

    5、架构分析

      5.1、看实线部分,service-hi 8762、service-hi 8763 和 service-ribbon 8764都作为服务消费者分别向服务注册中心eureka-server 8761中注册。

      5.2、看虚线部分,当sercvice-ribbon通过restTemplate调用service-hi的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hi:8762和8763 两个端口的hi接口。

  • 相关阅读:
    2018-2-13-win10-uwp-如何让WebView标识win10手机
    2018-2-13-win10-uwp-如何让WebView标识win10手机
    2018-3-22-win10-uwp-设置-HttpClient-浏览器标识
    2018-3-22-win10-uwp-设置-HttpClient-浏览器标识
    2018-2-13-WPF-获得触笔悬停元素上
    2018-2-13-WPF-获得触笔悬停元素上
    ACM-ICPC 2019 山东省省赛总结
    ACM-ICPC 2019 山东省省赛总结
    ACM-ICPC 2019 山东省省赛D Game on a Graph
    ACM-ICPC 2019 山东省省赛D Game on a Graph
  • 原文地址:https://www.cnblogs.com/shun-gege/p/9323553.html
Copyright © 2011-2022 走看看