春クラウドアリババサイドカー多言語マイクロ異種サービス

春クラウドアリババサイドカー紹介

以来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复制代码

おすすめ商品:春の雲、RBAC権利管理システム歓迎の注目の春のセキュリティのOAuth2

おすすめ

転載: juejin.im/post/5dd241605188254c8e467b6e