Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: Set a default value for NacosConfigProperties#serverAddr e.g. 127.0.0.1:8848 #3314

Closed
ice2shell opened this issue May 16, 2023 · 5 comments
Labels
area/nacos spring cloud alibaba nacos kind/discussion Mark as discussion issues/pr

Comments

@ice2shell
Copy link

Spring Cloud Alibaba Version
2021.0.1.0

Which Component
Nacos Config

Describe what problem you have encountered
When using the following configuration,application failed to start due to an exception: com.alibaba.nacos.api.exception.NacosException: endpoint is blank.

Through debugging, I found that NacosConfigDataLocationResolver has loaded properties into NacosConfigProperties
before Spring instantiated the bean. Therefore, if spring.cloud.nacos is not configured, the property named serverAddr in NacosConfigProperties will be null.

So why not set a default value e.g.127.0.0.1:8848 for serverAddr as the method overrideFromEnv of NacosConfigProperties did. Is there any other consideration? I'll glad to make a pr for this if possible.

Here is the yaml configuration:

spring:
  application:
    name: demo1
  config:
    import:
      - optional:nacos:local-gateway-server.yaml
server:
  port: 8380

Here is the pom :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.6.8</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>sca-nacos-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sca-nacos-demo</name>
    <description>sca-nacos-demo</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- Nacos注册中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2021.0.4.0</version>
        </dependency>
        <!-- Nacos配置中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2021.0.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Here is the exception stack trace:

Connected to the target VM, address: '127.0.0.1:65440', transport: 'socket'
15:15:01.546 [main] DEBUG org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - Application failed to start due to an exception
com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureException: java.lang.reflect.InvocationTargetException
	at com.alibaba.cloud.nacos.NacosConfigManager.createConfigService(NacosConfigManager.java:62)
	at com.alibaba.cloud.nacos.NacosConfigManager.<init>(NacosConfigManager.java:43)
	at com.alibaba.cloud.nacos.configdata.NacosConfigDataLocationResolver.registerConfigManager(NacosConfigDataLocationResolver.java:189)
	at com.alibaba.cloud.nacos.configdata.NacosConfigDataLocationResolver.resolveProfileSpecific(NacosConfigDataLocationResolver.java:149)
	at org.springframework.boot.context.config.ConfigDataLocationResolvers.lambda$resolve$2(ConfigDataLocationResolvers.java:120)
	at org.springframework.boot.context.config.ConfigDataLocationResolvers.resolve(ConfigDataLocationResolvers.java:126)
	at org.springframework.boot.context.config.ConfigDataLocationResolvers.resolve(ConfigDataLocationResolvers.java:119)
	at org.springframework.boot.context.config.ConfigDataLocationResolvers.resolve(ConfigDataLocationResolvers.java:107)
	at org.springframework.boot.context.config.ConfigDataImporter.resolve(ConfigDataImporter.java:105)
	at org.springframework.boot.context.config.ConfigDataImporter.resolve(ConfigDataImporter.java:97)
	at org.springframework.boot.context.config.ConfigDataImporter.resolveAndLoad(ConfigDataImporter.java:85)
	at org.springframework.boot.context.config.ConfigDataEnvironmentContributors.withProcessedImports(ConfigDataEnvironmentContributors.java:116)
	at org.springframework.boot.context.config.ConfigDataEnvironment.processWithProfiles(ConfigDataEnvironment.java:311)
	at org.springframework.boot.context.config.ConfigDataEnvironment.processAndApply(ConfigDataEnvironment.java:232)
	at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:102)
	at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:94)
	at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:102)
	at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:87)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
	at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:85)
	at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)
	at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114)
	at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65)
	at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:339)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:297)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
	at com.example.scanacosdemo.ScaNacosDemoApplication.main(ScaNacosDemoApplication.java:10)
Caused by: com.alibaba.nacos.api.exception.NacosException: java.lang.reflect.InvocationTargetException
	at com.alibaba.nacos.api.config.ConfigFactory.createConfigService(ConfigFactory.java:46)
	at com.alibaba.nacos.api.NacosFactory.createConfigService(NacosFactory.java:44)
	at com.alibaba.cloud.nacos.NacosConfigManager.createConfigService(NacosConfigManager.java:55)
	... 32 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at com.alibaba.nacos.api.config.ConfigFactory.createConfigService(ConfigFactory.java:43)
	... 34 common frames omitted
Caused by: com.alibaba.nacos.api.exception.NacosException: endpoint is blank
	at com.alibaba.nacos.client.config.impl.ServerListManager.<init>(ServerListManager.java:183)
	at com.alibaba.nacos.client.config.http.ServerHttpAgent.<init>(ServerHttpAgent.java:274)
	at com.alibaba.nacos.client.config.NacosConfigService.<init>(NacosConfigService.java:88)
	... 39 common frames omitted
15:15:01.548 [main] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - 

***************************
APPLICATION FAILED TO START
***************************

Description:

Application failed to connect to Nacos server: "null"

Action:

Please check your Nacos server config

15:15:01.550 [Thread-1] WARN com.alibaba.nacos.common.http.HttpClientBeanHolder - [HttpClientBeanHolder] Start destroying common HttpClient
15:15:01.551 [Thread-1] WARN com.alibaba.nacos.common.http.HttpClientBeanHolder - [HttpClientBeanHolder] Destruction of the end
Disconnected from the target VM, address: '127.0.0.1:65440', transport: 'socket'

Process finished with exit code 1

Describe what information you have read
I have read the reference doc of Nacos

@ice2shell ice2shell changed the title Set a default value for NacosConfigProperties#serverAddr e.g. 127.0.0.1:8848 Discussion: Set a default value for NacosConfigProperties#serverAddr e.g. 127.0.0.1:8848 May 16, 2023
@123liuziming 123liuziming added kind/discussion Mark as discussion issues/pr area/nacos spring cloud alibaba nacos labels May 16, 2023
@steverao
Copy link
Collaborator

Good idea! I think we should set default value in com.alibaba.cloud.nacos.NacosConfigProperties#assembleConfigServiceProperties.

@ice2shell
Copy link
Author

Yes, it can be solved like this. But i'm curious if there will be any problems if setting the default value directly.

@985492783
Copy link

@i will solve it@

@chickenlj
Copy link
Collaborator

According to the official documentation, we should add default address support:

https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#config-data-import

@chickenlj
Copy link
Collaborator

According to the official documentation, we should add default address support:

https://docs.spring.io/spring-cloud-config/docs/current/reference/html/#config-data-import

application.properties

spring.config.import=optional:configserver:

This will connect to the Config Server at the default location of "http://localhost:8888".

chickenlj added a commit to chickenlj/spring-cloud-alibaba that referenced this issue Nov 27, 2023
ruansheng8 pushed a commit that referenced this issue Jan 3, 2024
…t` enabled. (#3508)

* fix #3314, set default nacos server address when config.import enabled.
chickenlj added a commit to chickenlj/spring-cloud-alibaba that referenced this issue Jan 22, 2024
ruansheng8 added a commit that referenced this issue Jan 23, 2024
Fix #3314, set default nacos server address when spring.config.import enabled.
ruansheng8 pushed a commit to ruansheng8/spring-cloud-alibaba that referenced this issue Feb 23, 2024
ruansheng8 pushed a commit to ruansheng8/spring-cloud-alibaba that referenced this issue Mar 7, 2024
…g.import` enabled. (alibaba#3508)

* fix alibaba#3314, set default nacos server address when config.import enabled.
yuluo-yx pushed a commit that referenced this issue Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/nacos spring cloud alibaba nacos kind/discussion Mark as discussion issues/pr
Projects
None yet
Development

No branches or pull requests

5 participants