SpringCloud Polyglot支持Sidecar

2023-11-23 14:22 更新

您是否要使用非JVM語言來利用Eureka,Ribbon和Config Server?Spring Cloud Netflix Sidecar的靈感來自Netflix Prana。 它包括一個(gè)HTTP API,用于獲取給定服務(wù)的所有實(shí)例(按主機(jī)和端口)。您也可以通過嵌入式Zuul代理來代理服務(wù)調(diào)用,該代理從Eureka獲取其路由條目。可以直接通過主機(jī)查找或通過Zuul代理訪問Spring Cloud Config服務(wù)器。非JVM應(yīng)用程序應(yīng)實(shí)施運(yùn)行狀況檢查,以便Sidecar可以向Eureka報(bào)告應(yīng)用程序是啟動(dòng)還是關(guān)閉。

要在項(xiàng)目中包含Sidecar,請(qǐng)使用組ID為org.springframework.cloud且工件ID為??spring-cloud-netflix-sidecar的依賴項(xiàng)。

要啟用Sidecar,請(qǐng)使用@EnableSidecar創(chuàng)建一個(gè)Spring Boot應(yīng)用程序。該注釋包括@EnableCircuitBreaker@EnableDiscoveryClient@EnableZuulProxy。 在與非JVM應(yīng)用程序相同的主機(jī)上運(yùn)行結(jié)果應(yīng)用程序。

要配置側(cè)車,請(qǐng)將sidecar.portsidecar.health-uri添加到application.ymlsidecar.port屬性是非JVM應(yīng)用程序偵聽的端口。 這樣Sidecar可以正確地向Eureka注冊(cè)應(yīng)用程序。sidecar.secure-port-enabled選項(xiàng)提供了一種啟用流量安全端口的方法。sidecar.health-uri是在非JVM應(yīng)用程序上可訪問的URI,它模仿Spring Boot運(yùn)行狀況指示器。 它應(yīng)該返回類似于以下內(nèi)容的JSON文檔:

health-uri-document。 

{
  "status":"UP"
}

以下application.yml示例顯示了Sidecar應(yīng)用程序的示例配置:

application.yml。 

server:
  port: 5678
spring:
  application:
    name: sidecar

sidecar:
  port: 8000
  health-uri: http://localhost:8000/health.json

DiscoveryClient.getInstances()方法的API為/hosts/{serviceId}。以下針對(duì)/hosts/customers的示例響應(yīng)在不同的主機(jī)上返回兩個(gè)實(shí)例:

/ hosts / customers。 

[
    {
        "host": "myhost",
        "port": 9000,
        "uri": "http://myhost:9000",
        "serviceId": "CUSTOMERS",
        "secure": false
    },
    {
        "host": "myhost2",
        "port": 9000,
        "uri": "http://myhost2:9000",
        "serviceId": "CUSTOMERS",
        "secure": false
    }
]

非JVM應(yīng)用程序(如果Sidecar位于端口5678上)可通過http://localhost:5678/hosts/{serviceId}訪問此API。

Zuul代理會(huì)自動(dòng)將Eureka中已知的每個(gè)服務(wù)的路由添加到/<serviceId>,因此可以在/customers中使用客戶服務(wù)。非JVM應(yīng)用程序可以在http://localhost:5678/customers上訪問客戶服務(wù)(假設(shè)Sidecar正在偵聽5678端口)。

如果Config Server已向Eureka注冊(cè),則非JVM應(yīng)用程序可以通過Zuul代理對(duì)其進(jìn)行訪問。如果ConfigServer的serviceIdconfigserver并且Sidecar在端口5678上,則可以在http:// localhost:5678 / configserver上對(duì)其進(jìn)行訪問。

非JVM應(yīng)用程序可以利用Config Server返回YAML文檔的功能。例如,調(diào)用https://sidecar.local.spring.io:5678/configserver/default-master.yml 可能會(huì)導(dǎo)致YAML文檔類似于以下內(nèi)容:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  password: password
info:
  description: Spring Cloud Samples
  url: https://github.com/spring-cloud-samples

要在使用HTTP時(shí)使運(yùn)行狀況檢查請(qǐng)求接受所有證書,請(qǐng)將sidecar.accept-all-ssl-certificates設(shè)置為`true。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)