Ribbon

    在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章首先讲解下基于ribbon+rest。

一、ribbon简介

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.

—–摘自官网

ribbon是一个负载均衡客户端,可以很好的控制http和tcp的一些行为。Feign默认集成了ribbon。

ribbon 已经默认实现了这些配置bean:

    IClientConfig ribbonClientConfig: DefaultClientConfigImpl
    IRule ribbonRule: ZoneAvoidanceRule
    IPing ribbonPing: NoOpPing
    ServerList ribbonServerList: ConfigurationBasedServerList
    ServerListFilter ribbonServerListFilter:
    ZonePreferenceServerListFilter
    ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

二、准备工作
启动eureka-server 工程;启动service-provider工程,它的端口为9091; 将service-provider的配置文件的端口改为9092,并启动,这时你会发现:service-provider在eureka-server注册了2个实例,这就相当于一个小的集群。访问localhost:9090如图所示:

集群01

三、建一个服务消费者**

重新新建一个spring-boot工程,取名为:service-ribbon; 在它的pom.xml文件分别引入起步依赖spring-cloud-starter-eureka、spring-cloud-starter-ribbon、spring-boot-starter-web,代码如下:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.19.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jeffrey</groupId>
    <artifactId>jc_service_ribbon</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jc_service_ribbon</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR5</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

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


server:
  port: 10004

spring:
  application:
    name: service-ribbon
eureka:
  client:
    service-url:
      defaultZone: http://localhost:10001/eureka/
package com.jeffrey;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient   //服务发现者(消费者)
public class JcServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(JcServiceRibbonApplication.class, args);
    }
//  @Bean
//  @LoadBalanced
//  public RestTemplate getRestTemplate(){
//      return  new RestTemplate();
//  }
    @Bean
    @LoadBalanced
    RestOperations restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }
}

业务类实现:

package com.jeffrey.service.impl;

import com.jeffrey.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

/**
 * Created by jeffrey on 2019/3/29.
 */
@Service
public class DeptServiceImpl  implements DeptService{
    @Autowired
    RestOperations restTemplate;
    public String fetchInfo(String id) {

        return restTemplate.getForObject("http://SERVICE-PROVIDER/dept?id="+id,String.class);
    }
}