使用JAVA获取nacos配置信息出现null,获取不到

引入jar包

 <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>2.0.3</version>
  </dependency>

根据官方文档使用ConfigService获取配置信息


```java
import java.util.Properties;
import java.util.concurrent.Executor;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

public class TestN {

    public static void main(String[] args) throws NacosException, InterruptedException {
        String serverAddr = "139.9.5.70:8848";
        String dataId = "nacos_simpe";
        String group = "DEFAULT_GROUP";
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
        ConfigService configService = NacosFactory.createConfigService(properties);
        String content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);
        configService.addListener(dataId, group, new Listener() {
            @Override
            public void receiveConfigInfo(String configInfo) {
                System.out.println("recieve:" + configInfo);
            }

            @Override
            public Executor getExecutor() {
                return null;
            }
        });

        boolean isPublishOk = configService.publishConfig(dataId, group, "content");
        System.out.println(isPublishOk);

        Thread.sleep(3000);
        content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);

        boolean isRemoveOk = configService.removeConfig(dataId, group);
        System.out.println(isRemoveOk);
        Thread.sleep(3000);

        content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);
        Thread.sleep(300000);
    }
}


```bash
在这里插入代码片

调用出现null空的情况。

解决:

降低jia包!!!换成1.*

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
可以通过 Nacos 的动态配置功能来限制请求参数。以下是一个简单的示例: 1. 在 Nacos 控制台上创建一个配置: Data ID:`com.example.config` Group:`DEFAULT_GROUP` 配置内容: ```yaml # 允许的请求参数列表 allowed_params: - param1 - param2 ``` 2. 在应用程序中使用 Nacos 客户端将配置加载到应用程序中: ```java import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.listener.Listener; import java.util.Properties; public class NacosConfigExample { public static void main(String[] args) throws Exception { String serverAddr = "localhost:8848"; String dataId = "com.example.config"; String group = "DEFAULT_GROUP"; Properties properties = new Properties(); properties.put("serverAddr", serverAddr); ConfigService configService = NacosFactory.createConfigService(properties); String content = configService.getConfig(dataId, group, 5000); System.out.println(content); // 监听配置变更 configService.addListener(dataId, group, new Listener() { @Override public void receiveConfigInfo(String configInfo) { System.out.println(configInfo); } @Override public Executor getExecutor() { return null; } }); } } ``` 3. 在应用程序中使用配置: ```java import com.alibaba.nacos.api.config.ConfigFactory; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component @NacosConfigurationProperties(dataId = "com.example.config", groupId = "DEFAULT_GROUP", autoRefreshed = true) public class MyAppConfig { @Value("${allowed_params}") private List<String> allowedParams; public boolean isParamAllowed(String param) { return allowedParams.contains(param); } } ``` 在上面的示例中,我们使用了 `@NacosConfigurationProperties` 注解将配置文件中的 `allowed_params` 属性注入到应用程序中,然后我们就可以使用 `isParamAllowed()` 方法来检查请求参数是否被允许。如果参数不存在于 `allowed_params` 中,则该方法将返回 `false`,否则返回 `true`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值