spring-cloud-commons icon indicating copy to clipboard operation
spring-cloud-commons copied to clipboard

Instance ID should default to use 8080 instead of 0

Open EightMonth opened this issue 3 years ago • 12 comments

Describe the bug If server.port is not written in application.yml, bus id = applicationName:0:random, this code should take the default value of server.port 8080. If you write server.port in application.yaml, bus id = applicationName:port:random

bug code position: org.springframework.cloud.bus.ServiceMatcherAutoConfiguration#serviceMatcher ServiceMatcher serviceMatcher = new ServiceMatcher(pathMatcher, properties.getId(), configNames);

EightMonth avatar Sep 15 '22 09:09 EightMonth

If server.port is not written in application.yml, bus id = applicationName:0:random, this code should take the default value of server.port 8080.

Why?

ryanjbaxter avatar Sep 15 '22 12:09 ryanjbaxter

spring boot container set server.port is 8080,not 0 。why take 0?

EightMonth avatar Sep 16 '22 03:09 EightMonth

I am just wondering if the fact it is 0 is causing something to not work?

ryanjbaxter avatar Sep 16 '22 14:09 ryanjbaxter

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues avatar Sep 23 '22 14:09 spring-cloud-issues

org.springframework.cloud.bus.BusAutoConfiguration
@EventListener( classes = {RemoteApplicationEvent.class} ) public void acceptLocal(RemoteApplicationEvent event) { if (this.serviceMatcher.isFromSelf(event) && !(event instanceof AckRemoteApplicationEvent)) { if (log.isDebugEnabled()) { log.debug("Sending remote event on bus: " + event); }

        this.cloudBusOutboundChannel.send(MessageBuilder.withPayload(event).build());
    }

}

when application.yml without server.port as the code。the event object‘s bus id is application:8080:random,but the serviceMatcher's bus id is application:0:random

when application.yml config server.port is 8080 the event object's bus id is application:8080:random,the serviceMatcher's bus id is application:8080:random

EightMonth avatar Sep 29 '22 02:09 EightMonth

Could you describe the problem that manifests as a result of this? Or better yet provide a sample that reproduces the problem? This code has been like this for a long time so I am hesitant to change it.

ryanjbaxter avatar Sep 29 '22 12:09 ryanjbaxter

I'm sorry for not describing the problem clearly because of my poor English.Next, I will describe the problem with the help of Google Translate。(Source language: Chinese) Step 1: If there is no server in application.yml Port, then the bus id will be application: 0: random

Step 2: The spring container does not have application.yml and does not configure server.port without specifying the listening port. It will listen to port 8080 by default

Step 3: When I use spring cloud bus (the spring container is initialized), I will get the server.port 8080 from the spring container

Step 4, because the ID of spring cloud bus is obtained during initialization The port is empty, so the default is 0. Because the two IDs are inconsistent, when sending a bus event, it is found that the event was not sent by itself, resulting in the event cannot be sent through the spring cloud stream.

The causes of the bug are as follows:

  1. Because the spring cloud bus id is initialized in BusEnvironmentPostProcessor

image image public static final String DEFAULT_SERVICE_ID_STRING = "${vcap.application.name:${spring.application.name:application}}:${vcap.application.instance_index:${spring.application.index:${local.server.port:${server.port:0}}}}:${vcap.application.instance_id:${cachedrandom.${vcap.application.name:${spring.application.name:application}}.value}}";

  1. When EnvironmentPostProcessor is called by the Spring container, server.port has not been assigned the default value of 8080 by spring. Therefore, the spring cloud bus id will be application: 0: random

image

  1. If server.port is configured in application.yml, there will be server only when BusEnvironmentPostProcessor is executed If the port is not empty, the spring cloud bus id will be application: server. port: random

4.I don't want to submit the code, because it is too troublesome, so I bother you

EightMonth avatar Sep 30 '22 02:09 EightMonth

Thank you for that explanation.

If I am understanding what you said above, you are proposing we change ${server.port:0} in DEFAULT_SERVICE_ID_STRING to be ${server.port:8080}. Is that right?

ryanjbaxter avatar Sep 30 '22 13:09 ryanjbaxter

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues avatar Oct 07 '22 13:10 spring-cloud-issues

I want to explain that when you get the ${server. port} configuration item, it does not match the spring life cycle. It should not be obtained at EnvironmentPostProcessor, because it cannot be obtained at this time

EightMonth avatar Oct 08 '22 02:10 EightMonth

Can we listen for WebServerInitializedEvent to get the port once it is set?

    @EventListener
    public void onApplicationEvent(final WebServerInitializedEvent event) {
        port = event.getWebServer().getPort();
    }

ryanjbaxter avatar Oct 12 '22 21:10 ryanjbaxter

Boot sets the default 8080 here https://github.com/spring-projects/spring-boot/blob/f2294babaf50710a90af1c02a630202afecc94d2/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/AbstractConfigurableWebServerFactory.java#L44

spencergibb avatar Oct 13 '22 16:10 spencergibb