zoukankan      html  css  js  c++  java
  • spring cloud的消费服务ribbon(踩着坑往前爬)

    在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,

    一种是ribbon+restTemplate,另一种是feign

    Ribbon,主要提供客户侧的软件负载均衡算法。

    Ribbon客户端组件提供一系列完善的配置选项,比如连接超时、重试、重试算法等。Ribbon内置可插拔、可定制的负载均衡组件。下面是用到的一些负载均衡策略:
    - 简单轮询负载均衡
    - 加权响应时间负载均衡
    - 区域感知轮询负载均衡
    - 随机负载均衡

    Ribbon中还包括以下功能:
    - 易于与服务发现组件(比如Netflix的Eureka)集成
    - 使用Archaius完成运行时配置
    - 使用JMX暴露运维指标,使用Servo发布
    - 多种可插拔的序列化选择

    将service-hi的配置文件的端口改为8762,并启动,这时你会发现:service-hi在eureka-server注册了2个实例,这就相当于一个小的集群。访问localhost:8761如图所示:

    然而你会发现idea启动不了两次同一个main方法的类

    你只需要这样做就行了

    建一个服务消费者

    重新新建一个spring-boot工程,取名为:service-ribbon;

    <!--引入起步依赖spring-cloud-starter-eureka、spring-cloud-starter-ribbon、spring-boot-starter-web-->
    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>

    上面这个节点好像包括了spring-cloud-starter-eureka这个依赖,所以只要加上两个节点

    也许是命好吧

    在pom.xml加上两个节点

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

    在工程的配置文件指定服务的注册中心地址为http://localhost:8761/eureka/,程序名称为 service-ribbon,程序端口为8764。配置文件application.yml如下:

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

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

    package cn.zhiwei;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    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替换掉服务名,代码如下:

    package cn.zhiwei.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * Created by Administrator on 2018/4/6.
     */
    @Service//加上这个表示被spring容器管理了
    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 的方法,代码如下:

    package cn.zhiwei.controller;
    
    import cn.zhiwei.service.HelloService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * Created by Administrator on 2018/4/6.
     */
    @RestController
    public class HelloControler {
        @Autowired
        HelloService helloService;
        @RequestMapping(value = "/hi")
        public String hi(@RequestParam String name){
            return helloService.hiService(name);
        }
    }
    

      在浏览器上多次访问http://localhost:8764/hi?name=威哥,浏览器交替显示:

    hi 威哥,i am from port:8762
    hi 威哥,i am from port:8763

    项目结构图

     

  • 相关阅读:
    实用的 jquery 弹出窗口 插件winbox
    软考大纲
    那些年踩过三轮车的程序员
    今天是周几?
    本故事荣获2011年度最佳故事情节奖.
    命令行修改linux时间
    [置顶] 自考,认证相关资料
    金山软件面试题
    del
    【转】数据结构:位图法
  • 原文地址:https://www.cnblogs.com/liuzhiw/p/8732647.html
Copyright © 2011-2022 走看看