Ribbon:提供雲端負載均衡,有多種負載均衡策略可供選擇,可配合服務發現和斷路器使用。java
上一篇簡單講解了eureka的使用,這一篇文章基於上一篇的基礎上,講一下springcloud的另外一個重要的組件ribbon。git
這篇文章須要新建三個工程,service(8762),service(8763),service-ribbon(8764),三個服務分別向eureka-server註冊中心註冊,本文的服務使用的註冊中心都是本人建立的測試eureka--eureka.dalaoyang.cn。web
首先新建一個service(8762),括號內的8762是項目的端口號,加入eureka依賴,pom文件以下:spring
<?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.dalaoyang</groupId>
<artifactId>springcloud_service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud_service</name>
<description>springcloud_service</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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>
<spring-cloud.version>Edgware.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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>
複製代碼
配置文件apache
##端口號
server.port=8762
##服務名稱
spring.application.name=service
##註冊中心地址
eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/
複製代碼
啓動類使用註解@EnableEurekaClient向註冊中心註冊,代碼以下:springboot
package com.dalaoyang;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableEurekaClient
public class SpringcloudServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudServiceApplication.class, args);
}
@Value("${server.port}")
String port;
@Value("${spring.application.name}")
String serviceName;
@GetMapping("/")
public String index(){
return "serviceName=" + serviceName + "-------port=" + port;
}
}
複製代碼
到這裏service(8762)建立完成,而後複製一個項目,端口號改成8763做爲service(8763)。bash
而後新建項目service-ribbon(8764),pom文件加入ribbon依賴,pom完整代碼以下:app
<?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.dalaoyang</groupId>
<artifactId>springcloud_ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud_ribbon</name>
<description>springcloud_ribbon</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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>
<spring-cloud.version>Edgware.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</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>
複製代碼
配置文件以下:負載均衡
##端口號
server.port=8764
##服務名稱
spring.application.name=service_ribbon
##註冊中心地址
eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/
複製代碼
因爲只是一個簡單demo,全部我將java代碼都寫在了啓動類裏面,解釋一下,@LoadBalanced放在RestTemplate上面,代表RestTemplate開啓負載均衡。代碼以下:maven
package com.dalaoyang;
import org.springframework.beans.factory.annotation.Value;
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.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class SpringcloudRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
@Value("${server.port}")
String port;
@Value("${spring.application.name}")
String serviceName;
@GetMapping("/")
public String index(){
return restTemplate().getForObject("http://service/",String.class);
}
}
複製代碼
分別啓動service(8762),service(8763),service-ribbon(8764)三個工程。 先查看http://eureka.dalaoyang.cn/,能夠看到三個實例註冊成功。以下圖:
屢次訪問http://localhost:8764/,能夠看到頁面交替顯示 能夠看到負載均衡已經作到了。完整項目流程以下圖:
源碼下載 :大老楊碼雲
我的網站:dalaoyang.cn