詳解Spring Cloud Alibaba Sidecar多語言微服務(wù)異構(gòu)
自 Spring Cloud Alibaba 2.1.1
版本后增加了 spring-cloud-alibaba-sidecar
模塊作為作為一個(gè)代理的服務(wù)來間接性的讓其他語言可以使用spring cloud alibaba
等相關(guān)組件。通過與網(wǎng)關(guān)的來進(jìn)行路由的映射,從而可以做到服務(wù)的獲取,然后可以使用Ribbon間接性調(diào)用。
如上圖, Spring Cloud 應(yīng)用 請(qǐng)求 sidercar
然后轉(zhuǎn)發(fā)給其他語言的模塊,優(yōu)勢(shì)是對(duì)于異構(gòu)服務(wù)代碼 零侵入
,不需要直接根據(jù) nacos
或其他注冊(cè)中心 api 注冊(cè)等
使用入門
構(gòu)建其他語言接口服務(wù)
基于go 寫個(gè)簡單的服務(wù)接口
http://127.0.0.1:8089/sidecar
package mainimport ( "encoding/json" "fmt" "log" "net/http")func main() { http.HandleFunc("/sidecar", sidecar) http.HandleFunc("/heath", health) log.Fatal(http.ListenAndServe(":8089", nil)) }func sidecar(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprintf(w, "hello spring cloud alibaba sidecar") } func health(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") actuator := make(map[string]string) actuator["status"] = "UP" _ = json.NewEncoder(w).Encode(actuator) }
構(gòu)建 sidercar 應(yīng)用
增加 sidecar
依賴
<dependency> <groupid>com.alibaba.cloud</groupid> <artifactid>spring-cloud-starter-alibaba-sidecar</artifactid> <version>2.1.1.RELEASE</version></dependency>
配置 application.yml
server: port: 8088spring: cloud: nacos: discovery: server-addr: localhost:8848 application: name: go-provider# 配置異構(gòu)服務(wù)sidecar: ip: localhost port: 8089 health-check-url: http://localhost:8089/health
構(gòu)建 nacos consumer應(yīng)用
application.yml
server: port: 8087spring: cloud: nacos: discovery: server-addr: localhost:8848 application: name: nacos-consumer
consumer
邏輯
@RestController@EnableDiscoveryClient@SpringBootApplicationpublic class NacosConsumerApplication { public static void main(String[] args) { SpringApplication.run(NacosConsumerApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } @Autowired private RestTemplate restTemplate; @GetMapping("/test") public String test() { return restTemplate.getForObject("http://go-provider/sidecar", String.class); } }
測(cè)試使用
訪問spring cloud consumer 應(yīng)用
curl http://localhost:8087/test
輸出 go-provider
應(yīng)用
hello spring cloud alibaba sidecar
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot各種參數(shù)校驗(yàn)的實(shí)例教程
經(jīng)常需要提供接口與用戶交互(獲取數(shù)據(jù)、上傳數(shù)據(jù)等),由于這個(gè)過程需要用戶進(jìn)行相關(guān)的操作,為了避免出現(xiàn)一些錯(cuò)誤的數(shù)據(jù)等,一般需要對(duì)數(shù)據(jù)進(jìn)行校驗(yàn),下面這篇文章主要給大家介紹了關(guān)于SpringBoot各種參數(shù)校驗(yàn)的相關(guān)資料,需要的朋友可以參考下2022-03-03Java實(shí)現(xiàn)將word轉(zhuǎn)換為html的方法示例【doc與docx格式】
這篇文章主要介紹了Java實(shí)現(xiàn)將word轉(zhuǎn)換為html的方法,結(jié)合實(shí)例形式分析了java針對(duì)doc與docx格式文件的相關(guān)轉(zhuǎn)換操作技巧,需要的朋友可以參考下2019-03-03SpringBoot中HttpSessionListener的簡單使用方式
這篇文章主要介紹了SpringBoot中HttpSessionListener的簡單使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03MyBatis Example And與Or混合使用的實(shí)例
這篇文章主要介紹了MyBatis Example And與Or混合使用的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12防止未登錄用戶操作—基于struts2攔截器的簡單實(shí)現(xiàn)
下面小編就為大家?guī)硪黄乐刮吹卿浻脩舨僮鳌趕truts2攔截器的簡單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10