一、前言
前幾篇博客,對springcloud的註冊中心Eureka做了說明,並且提供者可以註冊到註冊中心上,客戶端可以面向服務的調用Restful接口。有的時候我們需要對提供者做負載均衡,SpringCloudRibbon就爲我們提供了負載均衡調用。下面我們就來見識一下Ribbon。
二 、Ribbon是什麼?
爲負載均衡而生
Spring Cloud Ribbon 是基於HTTP和TCP調用的負載均衡工具,是基於Netflix Ribbon實現的。
我們可以通過Spring Cloud Ribbon把直接從提供者調用的面向服務的調用替換成客戶端負載均衡調用。
使用Ribbon也非常簡單,就三步:
1.引入Ribbon的依賴 spring-cloud-starter-ribbon
2.消費者通過用@LoadBalanced修飾RestTemplete來調用進行面向服務接口REST調用
3.服務端建立多個實例
三、代碼
3.1 架構
架構中,提供者建立兩個,名字都是client1,端口分別是9001和9003.
消費者通過RestTempelet來訪問,Ribbon通過負載均衡來指導請求去訪問那個提供者
3.2 準備工作
註冊中心 Eureka
兩臺client,端口分別爲9001和9003
負載均衡 Ribbon
3.3 創建註冊中心
這個創建方法,跟前兩篇博客一樣,就不進行代碼展示。
3.4 創建提供者
client:
配置文件:
server:
port: 9001
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: client1
controller方法:
package com.wl.client.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Ares on 2018/4/11.
*/
@RestController
public class User {
@GetMapping("/user/findById")
public String findById(@RequestParam("id")String id){
return "這個是springcloud的客戶端1----"+id;
}
}
clientc:
配置文件:
server:
port: 9003
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: client1
controller:
package com.wl.client.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Ares on 2018/4/11.
*/
@RestController
public class User {
@GetMapping("/user/findById")
public String findById(@RequestParam("id")String id){
return "這個是springcloud的客戶端3----"+id;
}
}
兩個的配置文件application.name是相同的,端口號不同。爲了在顯示的時候區分那個是那個服務,所以在controller上的/user/findById顯示的不一樣,1爲client,3爲clientc。
3.5 創建負載均衡ribbon
pom文件:
添加spring-cloud-starter-eureka
和spring-cloud-starter-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.wl</groupId>
<artifactId>ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ribbon</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<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>
<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>Dalston.RC1</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
RibbonApplication:
在工程的啓動類中,通過@EnableEurekaClient向服務中心註冊;並且向程序的ioc注入一個bean: restTemplate;並通過@LoadBalanced註解表明這個restRemplate開啓負載均衡的功能。
package com.wl.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
HelloController:
寫一個controller用於用戶訪問,這裏注入了resttempelet,通過resttempelet的方法進行相關的服務調用。這裏url中CLIENT1是服務的名字,因爲在前文中的提供者的application.name爲CLIENT1,所以我們這裏要寫這個。
package com.wl.ribbon.controller;
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;
import org.springframework.web.client.RestTemplate;
/**
* Created by Ares on 2018/4/18.
*/
@RestController
public class HelloController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/hi")
public String hi(@RequestParam("id") String id){
return restTemplate.getForObject("http://CLIENT1/user/findById?id="+id,String.class);
}
}
application.yml:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
server:
port: 7000
spring:
application:
name: servie-ribbon
3.6 啓動
依次啓動Eureka ,client,clientc,ribbon。
在Eureka的監控界面可以看到:兩個提供者和ribbon都註冊上來了。
通過http://localhost:7000/hi?id=wl
多次訪問,會看到依次出現
這個是springcloud的客戶端3wl
這個是springcloud的客戶端1wl
多次訪問,多次訪問不同的提供者,這樣就實現了負載均衡。
四、小結
通過這次操練,可以很好的熟悉springcloud中的這個核心組件ribbon。下面會向大家帶來,ribbon的負載均衡策略。