consul micro-client configuration service instance name and ID

consul client must be configured to serve instance name and ID micro, micro service starts when the need to register the name and ID to the registry, follow-up call between micro-services also need to use.

Name can be configured in two ways, highest to lowest priority. Neither configure the default service name for the application

spring.cloud.consul.discovery.service-name 
spring.application.name

ID can be a plurality of configuration items configuration, the following configuration can be five, highest to lowest priority.

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  

If no start being given, the following relevant log, the log can also be seen, the configuration requirements must start with a letter, an alphanumeric or end.

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]

See Codeorg.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);
        }

        ConsulAutoRegistration registration = new ConsulAutoRegistration(service, autoServiceRegistrationProperties, properties, context, heartbeatProperties);
        customize(registrationCustomizers, registration);
        return registration;
    }
    //字符串不能为空,首字符必须为字母,尾字符必须为字母或数字,所有非字母数字的字符统一转换成'-'连接符,同时多个连续连接符转换成一个'-'.  
    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);
        }
    }
Original Address: https: //www.jianshu.com/p/83d3a8105620

Guess you like

Origin www.cnblogs.com/jpfss/p/11928378.html