春クラウドアリババサイドカー紹介
以来Spring Cloud Alibaba 2.1.1
リリース追加spring-cloud-alibaba-sidecar
の間接できるようにするサービスとしてプロキシとしてモジュールを他の言語を使用することができspring cloud alibaba
、およびその他の関連部品。マッピングおよびサービスへのアクセスを実現することができゲートウェイにルーティングすることにより、あなたはリボン間接呼び出しを使用することができます。
図のように、スプリングクラウドアプリケーション要求は、sidercar
異種サービスコードの利点はされ、次いで、他の言語のモジュールに転送される零侵入
から直接必要としないnacos
他のレジストリ登録API
はじめに
他の言語インタフェースサービスの構築
- シンプルなサービス・インターフェースを記述して行く基づき、
http://127.0.0.1:8089/sidecar
package main
import (
"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)
}复制代码
ビルのsidercar
アプリケーション
- 増加した
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: 8088
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
application:
name: go-provider
# 配置异构服务
sidecar:
ip: localhost
port: 8089
health-check-url: http://localhost:8089/health复制代码
ビルのnacos consumer
アプリケーション
application.yml
server:
port: 8087
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
application:
name: nacos-consumer复制代码
consumer
ロジック
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public 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);
}
}复制代码
使用テスト
- アクセス
spring cloud consumer 应用
curl http://localhost:8087/test 复制代码
- 出力
go-provider
アプリケーション
hello spring cloud alibaba sidecar复制代码