使用 Sidecar 整合非 JVM 微服务

使用 Sidecar 整合非 JVM 微服务

假设有个微服务运行在 ip:123.123.123.123 port: 8080 上,我们现在需要将其注册在 Eureka Server 上

首先我们需要在该服务上提供一个 REST 接口,让该接口返回服务的状态,即

{
    "status":"UP" // status 有几种取值,分别是: UP、DOWN、OUT_OF_SERVICE、UNKNOWN 等
}

编写 Sidecar 来注册该微服务

1、创建工程,添加下列依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-netflix-sidecar</artifactId>
</dependency>

2、在启动类上添加 @EnableSidecar 注解,声明这是一个 Sidecar。

@EnableSidecar 是一个组合注解,它整合了 @EnableCircuitBreaker、@EnableDiscoveryClient、@EnableZuulProxy。

3、配置 applicaiton.yml

server:
	port:8070
spring:
	application:
		name: microservice-sidecar-node-service
	eureka:
		instance: 
			hostname: hostname #非 JVM 微服务的 hostname 
		client:
			service-url:
				defaultZone: http://localhost: 8761/eureka/
			instance:
				prefer-ip-address: true
	sidecar:
		port: 8080
		health-uri: http://localhost:8080/health.json

或者

server:
	port:8070
spring:
	application:
		name: microservice-sidecar-node-service
	eureka:
		client:
			service-url:
				defaultZone: http://localhost: 8761/eureka/
			instance:
				prefer-ip-address: true
	sidecar:
		port: 8080
		health-uri: http://hostname:8080/health.json
		hostname: hostname # 非 JVM 微服务的 hostname
		ip-address: IP # 非 JVM 微服务的 IP 地址

4、Sidecar 的端点

  • 1)/

    该端点返回一个测试页面,展示 Sidecar 的常用端点

  • 2)/hosts/{serviceId}

    该端点返回 DiscoveryClient.getInstances(serviceId),即指定微服务在 Eureka 上的实例列表。

  • 3)/ping

    返回 OK

  • 4)/{serviceId}

    由于 Sidecar 整合了 Zuul ,因此可以使用该端点将请求转发到 serviceId。

posted @ 2020-12-03 16:13  zolmk  阅读(195)  评论(0编辑  收藏  举报