環境:
jdk1.8;spring boot2.0.3;spring cloud(Finchley.RELEASE版本);Maven3.3
摘要說明:
服務消費者:即調用注冊中心中的注冊服務暴露出的接口;同一服務提供者會啟動多個,故服務消費者需要起到負載均衡的作用
Spring Cloud Ribbon:Spring Cloud Ribbon是基于Netflix Ribbon實作的一套用戶端負載均衡的工具。它是一個基于HTTP和TCP的用戶端負載均衡器。它可以通過在用戶端中配置ribbonServerList來設定服務端清單去輪詢通路以達到均衡負載的作用。
步驟:
1.建立ribbon(ribbon的服務消費者)子項目
通過
SPRING INITIALIZR
工具選擇Cloud Routing:Ribbon子產品建構ribbon子項目引入依賴(pom.xnl):
<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>pers.cc</groupId>
<artifactId>springCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>ribbon</artifactId>
<name>ribbon</name>
<description>ribbon消費者</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</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-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
2.配置ribbon(ribbon的服務消費者)子項目
使用@EnableDiscoveryClient和@LoadBalanced注解開啟ribbon的服務消費者配置;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
配置application.properties
#配置服務及端口
spring.application.name=ribbon-consumer
server.port=3001
#注冊到注冊中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#從eureka伺服器系統資料庫中擷取注冊資訊的時間間隔(s),預設為30秒
eureka.client.registry-fetch-interval-seconds=5
3.開發服務提供者接口
在服務提供者eurekaDiscovery中開發部分接口給消費者調用
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
DiscoveryClient discoveryClient;
@GetMapping("/dc")
public String dc() {
String services = "Services: " + discoveryClient.getServices() + new Date().getTime();
System.out.println(services);
return services;
}
@GetMapping("/add")
public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
System.out.println(a + b);
return a + b;
}
@GetMapping("/stop")
public String stop() throws InterruptedException {
System.out.println("stop");
Thread.sleep(5000L);
return "stop";
}
}
4.開發ribbon調用提供者接口
在服務提供者eurekaDiscovery中開發部分接口給消費者調用
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;
import org.springframework.web.client.RestTemplate;
@RestController
public class TestCoontroller {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
return restTemplate.getForEntity("http://EUREKA-CLIENT/dc", String.class).getBody();
}
}
5.調用ribbon
先後啟動:
服務注冊中心(eurekaServer):eureka-server(1001)
服務提供者(eurekaDiscovery):eureka-client(2001)
服務提供者(eurekaDiscovery):eureka-client(2002)
服務消費者(ribbon):ribbon-consumer(3001)
多次輸入http://localhost:3001/test;檢視eurekaDiscovery兩個服務提供者都會出現調用達到負載均衡的作用;
6.源碼位址
github位址:https://github.com/cc6688211/springCloud.git