在上一篇文章《微服务实战spring cloud tencent系列(三)实战Spring Cloud Tencent Polaris Discover服务自动注册》我们介绍了服务自动注册到北极星(Polaris Mesh)上,这里我们在服务自动注册上衍生一下。
试想以下的场景:
某些时候,我们需要根据服务列表获取某个自定义的标签参数,然后在服务发现的时候,根据特定的标签来筛选调用的服务,这个怎么办呢?
上面这个场景其实就是常见的灰度发布的场景,所以对于服务自动注册来说,我们经常会涉及到向注册中心添加自定义的标签,方便后续的服务发现者来进行筛选,所以这里我们应该怎么办呢?
在Spring Cloud Tencent Polaris Discovery中,已经定义好了自定义添加标签参数的方法,有以下几种实现方式:
第一种:在配置文件中添加自定的换标签
在配置文件中添加自定义的标签的时候,遵循的格式如下:
spring: cloud: tencent: metadata: content: idc: chengdu-1 author: zhangsan env: prod
上面的案例是我们需要添加idc、author、env这三个自定义标签,他主要是在application.yml文件中添加的,示例如下:
此时我们实验下,在没添加自定义标签的场景下启动后能看到哪些标签:
添加自定义标签之后,我们能看到哪些标签:
可以看到自定义的标签都已经注册到北极星(Polaris Mesh)上了。
第二种实现自定义标签的方式是:在启动参数的时候添加自定义的标签,示例如下:
Java -jar -Dspring.cloud.tencent.metadata.content.env=prod -Dspring.cloud.tencent.metadata.content.idc=chengdu-1 -Dspring.cloud.tencent.metadata.content.author=zhangsan user-servie.jar
第三种实现自定义标签的方式是:添加环境变量,也就是在启动项目之前,我们使用如下的语句:
export SCT_METADATA_CONTENT_IDC=chengdu-1 export SCT_METADATA_CONTENT_ENV=prod export SCT_METADATA_CONTENT_AUTHOR=zhangsan java -jar user-service.jar
第四种是自定义实现SPI,这块SPI的话,个人使用较少,暂时不介绍了。
以上就是实现服务自动注册的时候,实现自定义标签的几种方式,最后附上方法1的示例代码,登录后即可下载。
还没有评论,来说两句吧...