一、什麼是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