目錄
一、通過RestTemplate調用微服務
二、通過Feign遠程調用
三、Dubbo?
分布式中的遠程調用大概分為兩種
RESTful接口?
REST,即Representational State Transfer的縮寫,如果一個架構符合REST原則,就稱它為RESTful架構。
- 每一個URI代表一種資源;
- 客戶端和服務器之間,傳遞這種資源的某種表現(xiàn)層;
- 客戶端通過四個HTTP動詞,對服務器端資源進行操作,實現(xiàn)"表現(xiàn)層狀態(tài)轉化"。
RPC協(xié)議
RPC( Remote Procedure Call )一種進程間通信方式。允許像調用本地服務一樣調用遠程服務。 RPC框架的主要目標就是讓遠程服務調用更簡單、透明。 RPC框架負責屏蔽底層的傳輸方式(TCP或者UDP)、序列化方式(XML/JSON/二進制)和通信細節(jié)。開發(fā)人員在使用的時候只需要了解誰在什么位置提供了什么樣的遠程服務接口即可,并不需要關心底層通信細節(jié)和調用過程。
區(qū)別與聯(lián)系?
比較項 |
RESTful |
RPC |
通訊協(xié)議 |
HTTP |
一般使用TCP |
性能 |
略低 |
較高 |
靈活度 |
高 |
低 |
應用 |
微服務架構 |
SOA架構 |
首先說下我這個案例的基本模型,通過訂單微服務與商品微服務,查詢商品實現(xiàn)下訂單。
前兩種方法都是用RESTful接口最后一個用的是RPC協(xié)議
一、通過RestTemplate調用微服務
配置RestTemplate交給spring管理
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
寫配置類或者啟動類中都行
通過restTemplate調用商品微服務
在訂單微服務中控制器層自動注入RestTemplate?
通過 restTemplate.getForObject 方法獲取商品對象
@Autowired
private RestTemplate restTemplate;
......
Product pr = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);
缺點:
- 代碼可讀性差,編程體驗不統(tǒng)一
- 參數(shù)復雜URL難以維護?
二、通過Feign遠程調用
首先加入導入坐標
<!--fegin組件-->
<dependency>
????????<groupId>org.springframework.cloud</groupId>
????????<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
在啟動類上添加Fegin的注解
@EnableFeignClients//開啟Fegin
在訂單微服務中創(chuàng)建一個service,并使用Fegin實現(xiàn)微服務調用
@FeignClient("service-product")//聲明調用的提供者的name
public interface ProductService {
//指定調用提供者的哪個方法
//@FeignClient+@GetMapping 就是一個完整的請求路徑 http://service- product/product/{pid}
@GetMapping(value = "/product/{pid}")
Product findById(@PathVariable("pid") int pid);
}
在訂單微服務中控制器層改寫方法 獲取商品對象
@Autowired
private ProductService productService;
......
Product pr = productService.findById(pid);
配置文件中
feign:
client:
config:
service-product: # 針對某個微服務的配置
# default: # 這里用default就是全局配置,如果是寫服務名稱,則是針對某個微服務的配置
loggerLevel: FULL # 日志級別
#需要把日志級別設置
logging:
level:
com.apesource: debug
而日志的級別分為四種:
- NONE:不記錄任何日志信息,這是默認值。
- BASIC:僅記錄請求的方法, URL以及響應狀態(tài)碼和執(zhí)行時間
- HEADERS:在BASIC的基礎上,額外記錄了請求和響應的頭信息
- FULL:記錄所有請求和響應的明細,包括頭信息、請求體、元數(shù)據(jù)。
Feign使用優(yōu)化?
Feign底層發(fā)起http請求,依賴于其它的框架。其底層客戶端實現(xiàn)包括:
- URLConnection:默認實現(xiàn),不支持連接池
- Apache HttpClient :支持連接池
- OKHttp:支持連接池
因此提高Feign的性能主要手段就是使用連接池代替默認的URLConnection
這里我們用Apache的HttpClient來演示
導入依賴
<!--httpClient的依賴 -->
<dependency>
????????<groupId>io.github.openfeign</groupId>
????????<artifactId>feign-httpclient</artifactId>
</dependency>
配置文件
feign:
client:
config:
default: # default全局的配置
loggerLevel: BASIC # 日志級別,BASIC就是基本的請求和響應信息
httpclient:
enabled: true # 開啟feign對HttpClient的支持
max-connections: 200 # 最大的連接數(shù)
max-connections-per-route: 50 # 每個路徑的最大連接數(shù)
總結:
- 日志級別盡量用basic
- 使用HttpClient或OKHttp代替URLConnection
三、Dubbo?
Dubbo是阿里巴巴開源的基于Java的高性能RPC(一種遠程調用) 分布式服務框架,致力于提供高性能和透明化的RPC遠程服務調用方案,以及SOA服務治理方案。
首先在common服務中添加一個IProductService接口
public interface IProductService {
Product findById(int pid);
}
在商品微服務中(服務提供者)
添加Dobbo依賴
<!--dubbo-->
<dependency>
????????<groupId>com.alibaba.cloud</groupId>
????????<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
添加dubbo配置
dubbo:
scan:
base-packages: com.cc.service.impl # 開啟包掃描
protocols:
dubbo:
name: dubbo # 服務協(xié)議
port: -1 # 服務端口 使用隨機端口
registry:
address: spring-cloud://localhost # 注冊中心
創(chuàng)建IProductServiceImpl實現(xiàn)類
//暴露服務:注意這里使用的是dubbo提供的注解@Service,而不是Spring的
@Service
public class IProductServiceImpl implements IProductService {
@Autowired
private ProductMapper productMapper;
@Override
public Product findById(int pid) {
return productMapper.selectById(pid);
}
}
在訂單微服務中(服務消費者)
導入Dobbo依賴
<!--dubbo-->
<dependency>
????????<groupId>com.alibaba.cloud</groupId>
????????<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
添加dobbo配置文章來源:http://www.zghlxwxcb.cn/news/detail-848007.html
dubbo:
registry:
address: spring-cloud://localhost # 注冊中心
cloud:
subscribed-services: service-product # 訂閱的提供者名稱
?在訂單微服務中控制器層改寫方法 獲取商品對象文章來源地址http://www.zghlxwxcb.cn/news/detail-848007.html
@Reference
private IProductService productService;
......
Product pr = productService.findById(pid);
到了這里,關于【spring Cloud】微服務通信的三種方式RestTemplate、Feign遠程調用與Dubbo的使用的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!