Skip to content

Nacos无法自动注入 #4104

@baifachuan

Description

@baifachuan

我们鼓励使用英文,如果不能直接使用,可以使用翻译软件,您仍旧可以保留中文原文。另外请按照如下要求提交相关信息节省社区维护同学的理解成本,否则该讨论极有可能直接被忽视或关闭。
We recommend using English. If you are non-native English speaker, you can use the translation software. We recommend using English. If you are non-native English speaker, you can use the translation software. In addition, please submit relevant information according to the following requirements to save the understanding cost of community maintenances, otherwise the discussion is very likely to be ignored or closed directly.

我在nacos中定义的配置是:

spring:
  redis:
    database: 2
    host: 127.0.0.1
    port: 6379
    password:
    timeout: 10000
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://192.168.101.68:5432/data_api
    username: postgres
    password: postgres

我在 bootstrap.yml 中的配置是:

spring:
  mvc:
    servlet:
      load-on-startup: 1
  application:
    name: gateway
  profiles:
    active: dev
  cloud:
    nacos:
      username: nacos # 替换为您的Nacos用户名
      password: nacos # 替换为您的Nacos密码
      discovery:
        server-addr: ${nacos_host:127.0.0.1}:${nacos_port:8848}
        # 命名空间,默认:public
        namespace: ${nacos_namespace:}
        service: ${spring.application.name}
      config:
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
        namespace: ${spring.cloud.nacos.discovery.namespace}
        file-extension: yaml
        # 指定配置
        extension-configs:
          - data-id: datasource.yaml
            refresh: true

我自己写测试代码:

@PostConstruct
	public void getNacosConfig() throws NacosException {
		// Nacos配置参数
		Properties properties = new Properties();
		properties.put("serverAddr", "127.0.0.1:8848");
		properties.put("namespace", "public");
		properties.put("username", "nacos");
		properties.put("password", "nacos");
		// 创建ConfigService
		ConfigService configService = NacosFactory.createConfigService(properties);

		// 获取扩展配置(datasource.yaml)
		String extDataId = "datasource.yaml";
		String extConfig = configService.getConfig(extDataId, "DEFAULT_GROUP", 5000);
		System.out.println("\n===== Nacos扩展配置(" + extDataId + ") =====");
		System.out.println(extConfig);
	}

都能读取到值,但是就是没有自动注入,JDBC Datasource创建失败,这样的变量:

	@Value("${spring.datasource.driver-class-name}")
	private String driver;
	@Value("${spring.datasource.url}")
	private String url;
	@Value("${spring.datasource.username}")
	private String username;
	@Value("${spring.datasource.password}")
	private String password;
	@Value("${data.service.domain:localhost:8082}")
	private String dataServiceDomain;
	@Value("${seatunnel.server-url:http://localhost:5801}")
	private String seatunnelServerUrl;

注入也是失败的。

依赖是存在的:

<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

就是升级了版本后,感觉这两个版本不兼容:

<spring.cloud.version>2025.1.0</spring.cloud.version>
<spring.cloud.alibaba.version>2025.1.0.0-SNAPSHOT</spring.cloud.alibaba.version>

Which Component
eg. Nacos Discovery, Sentinel

Describe the bug
A clear and concise description of what the bug is.

Simplest demo
The URL of the simplest demo to reproduce the problem.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.
e.g. MacOS 、Java8 、 Version 0.2.1.RELEASE

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions