文章目錄
- 1.前言
- 2.Ribbon
- 3.OpenFeign
- 4.服務間調用元件的負載均衡
1.前言
經過上一篇部落格的Eureka學習後,接下來我們正式開始服務間調用元件的學習,目前常用的服務間調用元件有Ribbon和OpenFeign,這裡我們就重點來學習一下這兩個元件的使用。
2.Ribbon
Ribbon是一個基于 HTTP 和 TCP 用戶端的負載均衡器
Ribbon也算是Spring Cloud Netflix體系的正常元件,Ribbon提供了内部調用以及負載均衡的能力,此處我們先來看下内部調用。
其原理圖如下所示:
- 像之前一樣建立名為ribbon-client的子產品,同樣記得在選擇依賴時選擇Spring Web、Eureka Discovery、Ribbon,如圖所示:
- 修改配置檔案application.yml,代碼如下:
server:
port: 18765
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:18761/eureka/
spring:
application:
name: service-ribbon
-
修改啟動類,我們知道Ribbon之是以調用内部接口,依靠的完全是RestTemplate對象來完成,是以我們先注入RestTemplate到Spring管理。
代碼如下:
package com.springclouddemo.ribbonclient;
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.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonClientApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonClientApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
- 接下來,建立一個HiService類,表示要遠端調用的接口服務,注意這裡的uri名字要和調用的
相同,即上一篇部落格中application-name
中配置過的,代碼如下:eureka-client
package com.springclouddemo.ribbonclient.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* @author 莫息濤
* @Description: Ribbon的測試服務類
* @date 2020/2/24 11:54
*/
@Service
public class HiService {
@Autowired
RestTemplate restTemplate;
public String hiService() {
return restTemplate.getForObject("http://SERVICE-HI/hi",String.class); //注冊的服務名稱
}
}
- 建立一個HiController,調用HiService的方法,代碼如下:
package com.springclouddemo.ribbonclient.controller;
import com.springclouddemo.ribbonclient.service.HiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 莫息濤
* @Description: Ribbon的對外測試接口
* @date 2020/2/24 11:54
*/
@RestController
public class HiController {
@Autowired
HiService hiService;
@RequestMapping(value = "/hi")
public String hi(){
return hiService.hiService();
}
}
- 按照順序依次啟動
,eureka-server
,eureka-client
,通路注冊中心頁面,可以看到ribbon子產品已經啟動,如圖所示:ribbon-client
- 通路
,可以看到如下顯示結果,說明ribbon配置成功,如圖所示:http://localhost:18765/hi
3.OpenFeign
Feign(現在改名叫OpenFeign)是Spring Cloud的公共元件,不屬于netflix,它也是用來進行服務間調用的,那麼讀者們是不是有一個疑問?有了Ribbon為什麼還要有Feign。
有沒有考慮過,雖然Ribbon使用RestTemplate形式進行服務間調用,但是對我們來說,我們更想接近的是如同Dubbo或者普通方法調用那個直接來調用Demo-Service提供的服務,那麼這個時候Ribbon是不可以的,是以我們隻能使用Feign來完成。
- 像之前一樣建立名為openfeign-client的子產品,同樣記得在選擇依賴時選擇Spring Web、Eureka Discovery、OpenFeign,如圖所示:
- 修改配置檔案application.yml,代碼如下:
server:
port: 18764
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:18761/eureka/
spring:
application:
name: service-openfeign
- 修改啟動類,添加注解
,表示這是一個Feign的用戶端,代碼如下:@EnableFeignClients
package com.springclouddemo.openfeignclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OpenfeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(OpenfeignClientApplication.class, args);
}
}
- 定義一個遠端調用接口HiInterface ,添加注解
以調用相應服務,代碼如下:@FeignClient
package com.springclouddemo.openfeignclient.Interface;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author 莫息濤
* @Description: Feign的測試接口
* @date 2020/2/24 10:48
*/
@FeignClient(value = "service-hi")
public interface HiInterface {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String hi();
}
- 建立HiController ,用來外通路進而擷取遠端接口内容,代碼如下:
package com.springclouddemo.openfeignclient.controller;
import com.springclouddemo.openfeignclient.Interface.HiInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 莫息濤
* @Description: Feign的對外測試接口
* @date 2020/2/24 10:50
*/
@RestController
public class HiController {
@Autowired
HiInterface hiInterface;
@RequestMapping("hi")
public String hi(){
return hiInterface.hi();
}
}
- 按照順序依次啟動
,eureka-server
,eureka-client
,通路注冊中心頁面,可以看到openfeign子產品已經啟動,如圖所示:openfeign-client
- 通路
,可以看到如下顯示結果,說明openfeign配置成功,如圖所示:http://localhost:18764/hi
4.服務間調用元件的負載均衡
事實上,在以上兩個子產品編寫完成後,就已經基本展示了服務間調用元件的作用。然而,為了展現服務間調用元件的負載均衡特性,我們再添加一個與eureka-client一模一樣的子產品,取名為
eureka-client2
,唯一的差別是修改其端口号為18763。
這裡我們用OpenFeign作為例子,使用Ribbon也同理。
按照順序依次啟動
eureka-server
,
eureka-client
,
eureka-client2
,
openfeign-client
,通路注冊中心頁面,觀察注冊中心頁面,如圖所示:
可以看到這裡出現了兩個
SERVICE-HI
服務,而通路的端口不同。
我們通過OpenFeign不斷來通路這個服務,可以看到會出現兩種不同的結果,如圖所示: