spring-cloud-commons
spring-cloud-commons copied to clipboard
Instance ID should default to use 8080 instead of 0
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);
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?
spring boot container set server.port is 8080,not 0 。why take 0?
I am just wondering if the fact it is 0 is causing something to not work?
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.
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
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.
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:
- Because the spring cloud bus id is initialized in BusEnvironmentPostProcessor
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}}";
- 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

- 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
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?
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.
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
Can we listen for WebServerInitializedEvent to get the port once it is set?
@EventListener
public void onApplicationEvent(final WebServerInitializedEvent event) {
port = event.getWebServer().getPort();
}
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