【SpringCloud Greenwich版本】第四章:服務消費者(ribbon)
一、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被呼叫了===