使用Sidecar支持异构平台的微服务
生活随笔
收集整理的這篇文章主要介紹了
使用Sidecar支持异构平台的微服务
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Polyglot support with Sidecarhttps://cloud.spring.io/spring-cloud-netflix/reference/html/#_polyglot_support_with_sidecarSidecar異構(gòu)語言,我們知道在整個微服務(wù)系統(tǒng)里面,我們可能不只用JAVA這一種語言,我們可能有一部分微服務(wù)使用的是Node,有可能是PHP,那這個時候怎么辦呢,我可以使用SideCar異構(gòu)的微服務(wù),納入到SpringCloud的生態(tài)圈里面來,他的靈感來自Netflix Pranahttps://github.com/Netflix/PranaDo you have non-JVM languages with which you want to take advantage of Eureka, Ribbon, and Config Server? The Spring Cloud Netflix Sidecar was inspired by Netflix Prana. 異構(gòu)的APP實現(xiàn)健康指示器,然后返回的數(shù)據(jù)格式應(yīng)該是這個樣子的{"status":"UP"
}那我們先寫一個異構(gòu)的微服務(wù),我需要引入Node的哪些模塊,http模塊,url模塊,path模塊
microservice-sidecar應(yīng)該把sidecar翻譯成附加服務(wù),或者叫附加應(yīng)用,https://www.cnblogs.com/rjzheng/p/10390827.html@EnableSidecar這是一個組合注解/*** @author Spencer Gibb*/
@EnableCircuitBreaker
@EnableDiscoveryClient
@EnableZuulProxy
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(SidecarConfiguration.class)
public @interface EnableSidecar {}整合了斷路器,整合了服務(wù)發(fā)現(xiàn),整合了ZuulProxy,注冊到Eureka正常寫https://cloud.spring.io/spring-cloud-netflix/reference/html/#_polyglot_support_with_sidecar主要的是這一段sidecar:port: 8000health-uri: http://localhost:8000/health.jsonsidecar.port.health-uri=http://localhost:8000/health.json
sidecar.port=8000localhost:8010/sidecarsidecar他其實做了一個橋,他通過異構(gòu)微服務(wù)Node寫的health.json,就是健康檢查指示器,它會把狀態(tài)搞到sidecar狀態(tài)里面去localhost:8070/healthTo enable the Sidecar, create a Spring Boot application with @EnableSidecar. This annotation includes @EnableCircuitBreaker, @EnableDiscoveryClient, and @EnableZuulProxy. Run the resulting application on the same host as the non-JVM application.https://github.com/spring-cloud/spring-cloud-netflix/issues/1505How can I make a polyglot app high available with sidecar #1505sidecar的高可用I have a nodejs microservice called node-service , and now I want to deploy them with two nodes with sidecar. Here goes the IP address:node1: which hostname is node-service1, and port is 8888
node2: which hostname is node-service2, and port is 8888
How to deploy them with sidecar?Is it like this:create two sidecars, one isserver:port: 8060
spring:application:name: sidecar
eureka:client:service-url:defaultZone: http://localhost:8761/eureka/instance:prefer-ip-address: truehostname: node-service1
sidecar:port: 8888 health-uri: http://node-service1:8888/health.json
and the other sidecar isserver:port: 8060
spring:application:name: sidecar
eureka:client:service-url:defaultZone: http://localhost:8761/eureka/instance:prefer-ip-address: truehostname: node-service2
sidecar:port: 8888 health-uri: http://node-service2:8888/health.json
In this way, I can deploy the two nodejs service nodes.Am I right? If not, is there any better way?That seems fine to me, if there something that is not working as expected?https://my.oschina.net/eacdy/blog/3047330分享:個人是怎么學(xué)習(xí)新知識的/*** @author Spencer Gibb*/
public class LocalApplicationHealthIndicator extends AbstractHealthIndicator {@Autowiredprivate SidecarProperties properties;@SuppressWarnings("unchecked")@Overrideprotected void doHealthCheck(Health.Builder builder) throws Exception {URI uri = this.properties.getHealthUri();if (uri == null) {builder.up();return;}Map<String, Object> map = new RestTemplate().getForObject(uri, Map.class);Object status = map.get("status");if (status != null && status instanceof String) {builder.status(status.toString());}else if (status != null && status instanceof Map) {Map<String, Object> statusMap = (Map<String, Object>) status;Object code = statusMap.get("code");if (code != null) {builder.status(code.toString());}else {getWarning(builder);}}else {getWarning(builder);}}private Health.Builder getWarning(Health.Builder builder) {return builder.unknown().withDetail("warning", "no status field in response");}}/*** @author Spencer Gibb*/
@RestController
public class SidecarController {@Autowiredprivate DiscoveryClient discovery;@Value("${spring.application.name}")private String appName;@RequestMapping("/ping")public String ping() {return "OK";}@RequestMapping("/hosts/{appName}")public List<ServiceInstance> hosts(@PathVariable("appName") String appName) {return hosts2(appName);}@RequestMapping("/hosts")public List<ServiceInstance> hosts2(@RequestParam("appName") String appName) {List<ServiceInstance> instances = this.discovery.getInstances(appName);return instances;}@RequestMapping(value = "/", produces = "text/html")public String home() {return "<head><title>Sidecar</title></head><body>\n"+ "<a href='/ping'>ping</a><br/>\n"+ "<a href='/health'>health</a><br/>\n" + "<a href='/hosts/"+ this.appName + "'>hosts/" + this.appName + "</a><br/>\n" + "</body>";}}localhost:8070
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn.cloud</groupId><artifactId>microservice-sidecar</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zuul</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-netflix-sidecar</artifactId></dependency></dependencies><!-- 這個插件,可以將應(yīng)用打包成一個可執(zhí)行的jar包 --><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
server.port=8070
spring.application.name=microservice-sidecar
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eureka
eureka.instance.appname=8070
logging.level.com.learn=trace
logging.file=springboot.log
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd} ==== [%thread] %-5level ==== %logger{50} ==== %msg%n#management.security.enabled=falsesidecar.port.health-uri=http://localhost:8000/health.json
sidecar.port=8000
package com.learn.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.sidecar.EnableSidecar;@EnableSidecar
@SpringBootApplication
public class SidecarApplication {public static void main(String[] args) {SpringApplication.run(SidecarApplication.class, args);}
}
?
總結(jié)
以上是生活随笔為你收集整理的使用Sidecar支持异构平台的微服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Zuul的回退
- 下一篇: 禁用Zuul的过滤器