Ribbon是客戶端的負載均衡機制,它有幾種負載均衡機制。默認是輪詢,我們也可以自定義規則。通過合理的分配網絡請求來減小服務器的壓力。項目都是注冊到eureka服務器上。通過ribbon去調用其他服務。
項目搭建:
1. 導入依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> </parent> <!-- springcloud依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka客戶端依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies>
2. 寫配置文件
# 項目訪問路徑前綴
server:
context-path: /demo
# 設置服務名稱,服務會以這個名字注冊到eureka服務器上
spring:
application:
name: demo
# 設置eureka服務器的注冊地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3. 開發服務接口
import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @RequestMapping("/data") public String testData(HttpServletRequest req){ String url = req.getRequestURL().toString(); return "提供服務的是: "+url; } }
4. 啟動主函數,測試。
import java.util.Scanner; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication //申明eureka客戶端 @EnableEurekaClient public class Main { public static void main(String[] args) { //1. 啟動main方法,在控制台輸入端口號 Scanner scan = new Scanner(System.in); String port = scan.nextLine(); //項目以輸入的端口號啟動 new SpringApplicationBuilder(Main.class).properties("server.port="+port).run(args); //2. 啟動兩次main方法,分別以8080 8081啟動,模擬集群的環境 瀏覽器訪問接口測試下。 } }
二:搭建Ribbon項目
1. 導入依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> </parent> <!-- springcloud依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka客戶端依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- ribbon依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> </dependencies>
2. 配置application.yml文件
# 指定默認端口
server:
port: 8083
# 設置服務名稱,服務會以這個名字注冊到eureka服務器上
spring:
application:
name: ribbon
# 設置eureka服務器的注冊地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3. 編寫controller
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class Controller { @Autowired private RestTemplate restTpl; @RequestMapping("/testRibbon") public String testRibbon() { String data = restTpl.getForObject("http://demo/demo/data", String.class); //String data = restTpl.getForObject("http://127.0.0.1:8081/demo/data", String.class); //User user = restTpl.getForObject("http://demo/demo/user/{id}", User.class,id); return data; } }
4. 編寫主函數入口, 配置RestTemplate
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 //申明eureka客戶端 @EnableEurekaClient public class RibbonMain { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(RibbonMain.class, args); } }
測試:我們通過 ribbon來調用其他服務數據,可以看到它默認是走輪詢策略。