《Spring Cloud Netflix官方文件》10. 使用 Sidecar支援多語言
你有non-jvm語言也想使用Eureka,Ribbon和Config Server? Spring Cloud Netflix Sidecar靈感來自Netflix Prana,它包含一個簡單的http api去獲取給定服務的所有例項(主機和埠)。你也可以通過嵌入式Zuul代理代理服務呼叫,Zuul 代理從Eureka獲取全部路由資訊。可直接通過主機查詢或通過Zuul代理訪問Spring Cloud Config Server。non-jvm程式需要實現健康檢查,這樣Sidecar 才能向eureka 報告程式是否線上或宕機。
要把Sidecar 引入到你的專案中,只需加入依賴 group:org.springframework.cloudartifact ,artifact id :spring-cloud-netflix-sidecar。
要啟用Sidecar ,建立一個Spring Boot應用幷包含@EnableSidecar註釋。這個註釋由@EnableCircuitBreaker,@EnableDiscoveryClient和@EnableZuulProxy構成。把這個non-jvm應用在同一臺主機上跑起來。
配置Sidecar,在application.yml中增加sidecar.port和sidecar.health-uri。sidecar.port屬性是non-jvm應用的監聽埠。這樣Sidecar能夠註冊應用到Eureka中。sidecar.health-uri是一個non-jvm應用的可訪問uri模擬Spring Boot的健康檢查。uri應該返一個如下的json文件:
health-uri-document.
{ "status":"UP" }
這是一個Sidecar應用application.yml的例子:
application.yml.
server: port: 5678 spring: application: name: sidecar sidecar: port: 8000 health-uri: http://localhost:8000/health.json
呼叫DiscoveryClient.getInstances()方法的api是/hosts/{serviceId}。這個例子呼叫/hosts/customers並返回兩個不同主機的例項。這個api non-jvm應用也能訪問(如果是sidecar,埠是5678)地址是:
.
/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 } ]
Zuul代理為每個以/<serviceId>被eureka已知的服務原子的新增路由,so the customers service is available at
/customers。non-jvm應用能夠通過http://localhost:5678/customers訪問客戶服務(假設sidecar監聽5678埠)
如果Config Server註冊到Eureka,non-jvm應用能夠通過Zuul 代理訪問。如果ConfigServer的 serviceId是configserver並且Sidecar 監聽的埠是5678,那麼Config Server能通過http://localhost:5678/configserver 訪問。
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ password: password info: description: Spring Cloud Samples url: https://github.com/spring-cloud-samples