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);
}
}