spring-cloud-netflix-sidecar接入案例
为什么想要了解spring-cloud-netflix-sidecar
这是第二次在工作中遇到夸语言的合作了,主体服务群是通过java语言基于spring-cloud实现的,新业务的需要,以及成员的技术栈问题,导致新业务用go语言、python语言来实现。需要实现内部管理,服务注册等功能,spring-cloud-netflix-sidecar边车服务的出现,不失为一种很好的解决方案。
一些关于边车的概念
因为这块不是本篇文章的重点,在这里就贴一些URL,做一个传送门:
https://time.geekbang.org/column/article/5909
https://zhuanlan.zhihu.com/p/61364161
关于spring-cloud-netflix-sidecar介绍的传送门
https://cloud.spring.io/spring-cloud-netflix/multi/multi__polyglot_support_with_sidecar.html
https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.2.2.RELEASE/reference/html/
话不多说,上代码
新建一个边车服务
pom.xml
注意spring-boot-starter-parent的版本和spring-cloud的版本,在实践过程中,更多的是在寻找版本上花了不少的时间。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>sidecar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sidecar</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-sidecar</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件
server:
port: 9965
spring:
application:
name: test-sidecar
sleuth:
feign:
enabled: true
sampler:
probability: 0.5
eureka:
client:
healthcheck:
enabled: true
registry-fetch-interval-seconds: 10
serviceUrl:
defaultZone: http://************/eureka #你的eureka地址
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
lease-expiration-duration-in-seconds: 90
lease-renewal-interval-in-seconds: 30
prefer-ip-address: true
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'
health:
mail:
enabled: false
sidecar:
port: **** #被边车的服务的端口
health-uri: http://localhost:8865/sidecar/health.json #被边车的服务的健康检测接口地址
启动类上添加@EnableSidecar注解
package com.example.sidecar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.sidecar.EnableSidecar;
@SpringBootApplication
@EnableSidecar
public class SidecarApplication {
public static void main(String[] args) {
SpringApplication.run(SidecarApplication.class, args);
}
}
被边车的其他语言的服务
添加一个健康检测的接口
接口要求:返回必须是
{
"status":"UP"
}
我这里用的还是java服务,因为本地没有其他服务
启动服务
1.先启动被边车的服务A
2.在启动边车服务B
可以在A服务中看到心跳检测到接口会定期被调用
服务B启动后可以在eureka中看见注册信息
然后直接在服务中集成TEST-SIDECAR的FeignClient
之后这个spring-cloud集群里的其他服务均可通过边车服务B的feignClient来调用请求服务A的接口功能。
例如,在服务中通过feignClient调用健康接口
这两个接口通过swagger调用均可得到返回
一点简单的总结
并没有将这个方案推荐给同事,主要原因有以下几点
1.自己本身对这个东西不太熟悉,也仅限于刚刚跑通
2.同事已经通过自己模拟请求的方式也将自己的go服务注册到了eureka上
3.基于原因2的情况下,我认为边车模式多加了一个边车服务进行转发,请求链路变长了,性能会有对应的下降,且每个go服务都多了一个配套的边车服务,会导致运维成本变高。
总之,虽然不用,但是也是一个了解吧~