consul客户端配置微服务实例名称和ID
consul客户端必须配置微服务实例名称和ID,微服务启动的时候需要将名称和ID注册到注册中心,后续微服务之间调用也需要用到.
名称可以通过以下两种方式配置,优先级从高到低.两个都不配置则默认服务名称为application
spring.cloud.consul.discovery.service-name
spring.application.name
ID可以通过多个配置项配置,下面的五种配置都可以,优先级从高到低.
spring.cloud.consul.discovery.instance-id
vcap.application.instance_id
spring.application.name和spring.application.instance_id搭配,'-'分隔
spring.application.name和server.port搭配,'-'分隔
spring.application.name
spring.application.instance_id
如果不配置启动会报错,相关日志如下,从日志中也可以看出,配置要求必须以字母开始,字母或数字结尾.
Caused by: java.lang.IllegalArgumentException: Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen: 8081
at org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration.normalizeForDns(ConsulAutoRegistration.java:179) ~[spring-cloud-consul-discovery-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration.getInstanceId(ConsulAutoRegistration.java:170) ~[spring-cloud-consul-discovery-2.0.1.RELEASE.jar:2.0.1.RELEASE]
at org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration.registration(ConsulAutoRegistration.java:78) ~[spring-cloud-consul-discovery-2.0.1.RELEASE.jar:2.0.1.RELEASE]
代码详见org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration
public static ConsulAutoRegistration registration(AutoServiceRegistrationProperties autoServiceRegistrationProperties, ConsulDiscoveryProperties properties, ApplicationContext context, List<ConsulRegistrationCustomizer> registrationCustomizers, HeartbeatProperties heartbeatProperties) {
NewService service = new NewService();
//注册到consul服务端时显示的微服务名称,优先级从高到低:
//配置项spring.cloud.consul.discovery.service-name
//配置项spring.application.name
//默认值application
String appName = getAppName(properties, context.getEnvironment());
//注册到consul服务端时显示的微服务Id,优先级从高到低:
//配置项spring.cloud.consul.discovery.instance-id
//配置项vcap.application.instance_id
//配置项spring.application.name和spring.application.instance_id,用':'拼接
//配置项spring.application.name和server.port,用':'拼接
//配置项spring.application.name
//配置项spring.application.instance_id
//注意,内部的normalizeForDns方法,用于规范化应用名称、ID等,把非数字字符串转换成'-'连接符
//比如spring.application.name=sc--test,server.port=8080,拼成的id为sc--test:8080,经过normalizeForDns方法就转换成了sc-test-8080
service.setId(getInstanceId(properties, context));
if(!properties.isPreferAgentAddress()) {
service.setAddress(properties.getHostname());
}
//微服务应用名称也必须符合规范,字母开始,字母或数字结尾
service.setName(normalizeForDns(appName));
//设置标签,spring.cloud.consul.discovery.tags
service.setTags(createTags(properties));
if(properties.getPort() != null) {
service.setPort(properties.getPort());
setCheck(service, autoServiceRegistrationProperties, properties, context, heartbeatProperties);
}
<span class="token class-name">ConsulAutoRegistration</span> registration <span class="token operator">=</span> <span class="token keyword"><span class="hljs-keyword">new</span></span> <span class="token class-name">ConsulAutoRegistration</span><span class="token punctuation">(</span>service<span class="token punctuation">,</span> autoServiceRegistrationProperties<span class="token punctuation">,</span> properties<span class="token punctuation">,</span> context<span class="token punctuation">,</span> heartbeatProperties<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">customize</span><span class="token punctuation">(</span>registrationCustomizers<span class="token punctuation">,</span> registration<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword"><span class="hljs-keyword">return</span></span> registration<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
//字符串不能为空,首字符必须为字母,尾字符必须为字母或数字,所有非字母数字的字符统一转换成'-'连接符,同时多个连续连接符转换成一个'-'.
public static String normalizeForDns(String s) {
if(s != null && Character.isLetter(s.charAt(0)) && Character.isLetterOrDigit(s.charAt(s.length() - 1))) {
StringBuilder normalized = new StringBuilder();
Character prev = null;
char[] var3 = s.toCharArray();
int var4 = var3.length;
for(int var5 = 0; var5 < var4; ++var5) {
char curr = var3[var5];
Character toAppend = null;
if(Character.isLetterOrDigit(curr)) {
toAppend = Character.valueOf(curr);
}
//不为数字和字母的字符转换成'-'分隔符,连续多个非字母数字字符转换成一个'-'分隔符
//这里做了一层判断,只有在前一个字符为字母数字的时候,才把当前的字符转换成'-'; 如果前一个为'-'则这个字符忽略不拼接.
//其实这里的prev只有在第一次的时候为null但是第一次的时候走不到else if这个条件
else if(prev == null || prev.charValue() != 45) {
toAppend = Character.valueOf('-');
}
if(toAppend != null) {
normalized.append(toAppend);
prev = toAppend;
}
}
return normalized.toString();
} else {
throw new IllegalArgumentException("Consul service ids must not be empty, must start with a letter, end with a letter or digit, and have as interior characters only letters, digits, and hyphen: " + s);
}
}
consul客户端配置微服务实例名称和ID的更多相关文章
- dubbox微服务实例及引发的“血案”
Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成. 主要核心部件: Remoting: 网络通信框架 ...
- 微服务实战(二):使用API Gateway--转
原文地址:http://dockone.io/article/482 [编者的话]本系列的第一篇介绍了微服务架构模式.它讨论了采用微服务的优点和缺点,除了一些复杂的微服务,这种模式还是复杂应用的理想选 ...
- 微服务实战(二):使用API Gateway
微服务实战(一):微服务架构的优势与不足 微服务实战(二):使用API Gateway 微服务实战(三):深入微服务架构的进程间通信 微服务实战(四):服务发现的可行方案以及实践案例 微服务实践(五) ...
- 【SpringCloud微服务实战学习系列】服务治理Spring Cloud Eureka
Spring Cloud Eureka是Spring Cloud Netflix微服务中的一部分,它基于NetFlix Sureka做了二次封装,主要负责完成微服务架构中的服务治理功能. 一.服务治理 ...
- 微服务实战-使用API Gateway
当你决定将应用作为一组微服务时,需要决定应用客户端如何与微服务交互.在单体式程序中,通常只有一组冗余的或者负载均衡的服务提供点.在微服务架构中,每一个微服务暴露一组细粒度的服务提供点.在本篇文章中,我 ...
- SpringCloud微服务实战——第三章服务治理
Spring Cloud Eureka 服务治理 是微服务架构中最核心最基本的模块.用于实现各个微服务实例的自动化注册与发现. 服务注册: 在服务治理框架中,都会构建一个注册中心,每个服务单元向注册中 ...
- 微服务实战(二):使用API Gateway - DockOne.io
原文:微服务实战(二):使用API Gateway - DockOne.io [编者的话]本系列的第一篇介绍了微服务架构模式.它讨论了采用微服务的优点和缺点,除了一些复杂的微服务,这种模式还是复杂应用 ...
- 通过总线机制实现自动刷新客户端配置(Consul,Spring Cloud Config,Spring Cloud Bus)
通过总线机制实现自动刷新客户端配置 方案示意图 利用Git服务的webhook通知功能,在每次更新配置之后,Git服务器会用POST方式调用配置中心的/actuator/bus-refresh接口,配 ...
- SpringCloud微服务实战——搭建企业级开发框架(四十三):多租户可配置的电子邮件发送系统设计与实现
在日常生活中,邮件已经被聊天软件.短信等更便捷的信息传送方式代替.但在日常工作中,我们的重要的信息通知等非常有必要去归档追溯,那么邮件就是不可或缺的信息传送渠道.对于我们工作中经常用到的系统,里面 ...
随机推荐
- [转]BigDecimal使用(整理)
原文地址:https://www.jianshu.com/p/2947868d76eb 应用场景 大多数的商业计算中,一般采用java.math.BigDecimal类来进行精确计算.比如:货币 使用 ...
- Kotlin数据类型 Unit、Nothing与Nothing?、Any与Any?
Kotlin数据类型 Unit.Nothing与Nothing?.Any与Any? 本文链接:https://blog.csdn.net/ldxlz224/article/details/9440 ...
- 004-行为型-10-中介者模式(Mediator)
一.概述 在Mediator模式中,类之间的交互行为被统一放在Mediator的对象中,对象通过Mediator对象同其他对象交互,Mediator对象起着控制器的作用. 用一个中介对象来封装一系列的 ...
- bat批处理文件怎么将路径添加到path环境变量中
bat批处理文件怎么将路径添加到path环境变量中 摘自:https://zhidao.baidu.com/question/1887763143433391788.html 永久性的: @echo ...
- Qt编写气体安全管理系统17-记录清理
一.前言 记录清理功能,在数据量很小的情况下,用不上,如果数据量大了的话,长年累月存储的,那就显得极其重要了,好比视频监控中的NVR存储的视频一样,一般来说存储个60天,那超过60天怎办呢,擦除早期的 ...
- Linux记录-常用统计awk
#统计第一列ip的个数(uniq -c 打印重复行count计数) cat ip.txt | awk '{print $1}' | sort | uniq -c | sort -rn | head - ...
- .gitignore 模板
.gitignore 模板 HELP.md target/ !.mvn/wrapper/maven-wrapper.jar !**/src/main/** !**/src/test/** ### ST ...
- Flink 在IDEA执行时的webui
不过Flink IDEA中执行的webui 需要 flink-runtime-web 包的支持 pom 如下: <dependency> <groupId>org.apache ...
- JAVA协程 纤程 与Quasar 框架
ava使用的是系统级线程,也就是说,每次调用new Thread(....).run(),都会在系统层面建立一个新的线程,然鹅新建线程的开销是很大的(每个线程默认情况下会占用1MB的内存空间,当然你愿 ...
- ubuntu14.0.4安装kafka
1. 下载 zookeeper-3.4.12 zookeeper download 2 配置Zookeeper 进入 zookeeper 的 conf 目录下,找到 zoo_sample.cfg 文件 ...