一、什么是Ribbon?
Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现。通过Spring Cloud的封装,可以让我们轻松地将面向服务的REST模版请求自动转换成客户端负载均衡的服务调用。Spring Cloud Ribbon虽然只是一个工具类框架,它不像服务注册中心、配置中心、API网关那样需要独立部署,但是它几乎存在于每一个Spring Cloud构建的微服务和基础设施中。
二、环境准备
1、首先我们将服务提供者启动两个实例,模拟成一个小的集群。
idea中启动两个实例的方法。
1>更改启动配置
2>将single instance only 选中取消
3>启动后修改yml文件端口号,启动,这时就会看见注册中心中已经注册了两个服务。
三、创建服务消费者
1、创建一个maven工程eureka-ribbon;它和之前一样,pom文件依赖于父工程,并引入一下依赖
<?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">
<parent>
<artifactId>spring-Cloud</artifactId>
<groupId>com.demo.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.szz.spring</groupId>
<artifactId>eureka-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-ribbon</name>
<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>
2、在yml配置文件中指定注册中心地址,端口号,服务名称,如下所示
server:
port: 8764
spring:
application:
name: eureka-ribbon
eureka:
client:
service-url:
defalutZone: http://localhost:8761/eureka/
3、在启动类上加@EnableDiscoveryClient注解,注册到服务中心,如下
package com.demo.cloud.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @ClassName RibbonApplication
* @Description TODO
* @Author shanzz
* @Date 2019/5/28 11:03
* @Version 1.0
**/
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class,args);
}
}
4、这里我们需要新建一个java的配置类,配置注入RestTemplate的bean,并通过@LoadBalanced注解表明开启负载均衡功能。
package com.demo.cloud.ribbon.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @ClassName RestTemplateConfig
* @Description TODO
* @Author shanzz
* @Date 2019/5/28 11:33
* @Version 1.0
**/
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced//负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
5、书写测试类adminService,通过之前注入IOC容器的restTemplate来消费eureka-client服务的/hi接口。这里我们直接使用程序名代替具体的URL地址,ribbon会根据服务名来选择具体的服务实例,跟据服务实例在请求中会用具体的URL替换掉服务名。
package com.demo.cloud.ribbon.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* @ClassName AdminService
* @Description TODO
* @Author shanzz
* @Date 2019/5/28 13:50
* @Version 1.0
**/
@Service
public class AdminService {
@Autowired
private RestTemplate restTemplate;
public String sayHi(String message){
return restTemplate.getForObject("http://eureka-client/hi?message="+message,String.class);
}
}
6、编写controller,调用service方法
package com.demo.cloud.ribbon.controller;
import com.demo.cloud.ribbon.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName RibbonController
* @Description TODO
* @Author shanzz
* @Date 2019/5/28 13:47
* @Version 1.0
**/
@RestController
public class RibbonController {
@Autowired
private AdminService adminService;
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String sayHi(String message){
return adminService.sayHi(message);
}
}
7、访问http://localhost:8764/hi?message=helloRibon;多次访问可以发现浏览器交替显示
Hello the port is : 8763 , message is : helloRibon
Hello the port is : 8762 , message is : helloRibon
这时表示我们已经成功实现以负载均衡方式来访问不同端口的实例。
8、此时的整体架构
一个注册中心 Eureka-server port :8761
两个服务提供者eureka-client port:8762;8763,分别向服务中心注册
一个服务消费者eureka-ribbon port :8764,向服务中心注册
当服务消费者通过RestTemplate向eureka-client请求调用接口时,由于我们通过@LoadBalanced已经申明要去使用负载均衡,所以会轮流去调用8762和8763两个不同端口的服务。
【参考资料】
《Spring Cloud微服务实战》
https://www.funtl.com/zh/spring-cloud-netflix/Spring-Cloud-创建服务消费者(Ribbon).html
https://www.cnblogs.com/snake23/p/9490063.html