一、SpringCloud版本
本文介紹的Springboot版本爲2.1.1.RELEASE,SpringCloud版本爲Greenwich.RC1,JDK版本爲1.8,集成環境爲IntelliJ IDEA
二、Ribbon介紹
Ribbon是一個客戶端負載均衡器,它可以很好地控制HTTP和TCP客戶端的行爲。Feign已經使用Ribbon,所以如果您使用@FeignClient,則本節也適用。
Ribbon中的中心概念是指定客戶端的概念。每個負載平衡器是組合的組合的一部分,它們一起工作以根據需要聯繫遠程服務器,並且集合具有您將其作爲應用程序開發人員(例如使用@FeignClient註釋)的名稱。Spring Cloud使用RibbonClientConfiguration爲每個命名的客戶端根據需要創建一個新的合奏作爲ApplicationContext。這包含(除其他外)ILoadBalancer,RestClient和ServerListFilter。
要在項目中包含Ribbon,請使用組org.springframework.cloud和工件ID spring-cloud-starter-netflix-ribbon的起始器。
三、創建Ribbon服務
- 3.1創建
創建一個module,取名爲cloudribbon,選擇Cloud Routing,再勾選上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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jthao</groupId>
<artifactId>cloudribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloudribbon</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.jthao</groupId>
<artifactId>cloudser</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
在配置文件中設置端口爲8005,服務名爲cloudribbon,並指向服務註冊中心
eureka.client.service-url.defaultZone: http://localhost:8001/eureka/
server.port=8005
spring.application.name=cloudribbon
- 3.2啓動
啓動ribbon向服務中心註冊,並在程序注入一個Bean,通過@LoadBalanced註解來實現負載訪問功能
package com.jthao.cloudribbon;
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 CloudribbonApplication {
public static void main(String[] args) {
SpringApplication.run(CloudribbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
定義一個接口,通過注入的RestTemplate來訪問cloudclient的test方法
package com.jthao.cloudribbon.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class TestService {
@Autowired
RestTemplate restTemplate;
public String test(String name) {
return restTemplate.getForObject("http://cloudclient/test?name="+name,String.class);
}
}
創建一個controller,來調用TestService的方法
package com.jthao.cloudribbon.controller;
import com.jthao.cloudribbon.service.TestService;
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;
@RestController
public class TestController {
@Autowired
TestService testService;
@RequestMapping(value = "/test")
public String test(@RequestParam String name) {
return testService.test(name);
}
}
- 3.3訪問
通過瀏覽器多次訪問http://localhost:8005/test?name=huahua
我們可以看到如下展示
huahua===端口:8002被調用了===
huahua===端口:8003被調用了===