nacos shuts down automatically. nacos 自动关闭

disbfnqx  于 2022-11-13  发布在  Nacos
关注(0)|答案(4)|浏览(312)

Describe the bug

Using nacos as the config center, nacos shutsdown automatically after successful getting the configuration file.

使用nacos 作为配置中心,成功获取配置文件后,nacos 自动关闭

Spring Boot Version: v2.7.5

jdk: openjdk 11

nacos version: 2.1.1

<dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>nacos-config-spring-boot-starter</artifactId>
        <version>0.2.12</version>
    </dependency>

log:

文件夹截图

application.yml

nacos:
  # Nacos 配置中心的配置项,对应 NacosConfigProperties 配置类
  config:
    server-addr: 127.0.0.1:8848?username=nacos&password=nacos # Nacos 服务器地址
    bootstrap:
#    这里,我们设置为 true,保证使用 @Value 和 @ConfigurationProperties 注解,可以读取到来自 Nacos 的配置项。
      enable: true # 是否开启 Nacos 配置预加载功能。默认为 false。
#    这里,我们设置为 true,保证 Spring Boot 应用的 Logger 能够使用来自 Nacos 的配置项。
      log-enable: true # 是否开启 Nacos 支持日志级别的加载时机。默认为 false。
    data-id: example # 使用的 Nacos 配置集的 dataId。
    type: YAML # 使用的 Nacos 配置集的配置格式。默认为 PROPERTIES。
    group: DEFAULT_GROUP # 使用的 Nacos 配置分组,默认为 DEFAULT_GROUP。
    namespace: # 使用的 Nacos 的命名空间,默认为 null。

Application,java

@SpringBootApplication
// @NacosPropertySource(dataId = "example", type = ConfigType.YAML)
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Component
    public class OrderPropertiesCommandLineRunner implements CommandLineRunner {

        private final Logger logger = LoggerFactory.getLogger(getClass());

        @Autowired
        private OrderProperties orderProperties;

        @Override
        public void run(String... args) {
            logger.info("payTimeoutSeconds:" + orderProperties.getPayTimeoutSeconds());
            logger.info("createFrequencySeconds:" + orderProperties.getCreateFrequencySeconds());
        }

    }

    @Component
    public class ValueCommandLineRunner implements CommandLineRunner {

        private final Logger logger = LoggerFactory.getLogger(getClass());

        //        @NacosValue(value = "${order.pay-timeout-seconds}")
        @Value(value = "${order.pay-timeout-seconds}")
        private Integer payTimeoutSeconds;

        //        @NacosValue(value = "${order.create-frequency-seconds}")
        @Value(value = "${order.create-frequency-seconds}")
        private Integer createFrequencySeconds;

        @Override
        public void run(String... args) {
            logger.info("payTimeoutSeconds:" + payTimeoutSeconds);
            logger.info("createFrequencySeconds:" + createFrequencySeconds);
        }
    }

}

OrderProperties.java

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
//@NacosConfigurationProperties(prefix = "order", dataId = "${nacos.config.data-id}", type = ConfigType.YAML)
@ConfigurationProperties(prefix = "order")
public class OrderProperties {

    /**
* 订单支付超时时长,单位:秒。
*/
    private Integer payTimeoutSeconds;

    /**
* 订单创建频率,单位:秒
*/
    private Integer createFrequencySeconds;

//    /**
//     * 配置描述
//     */
//    private String desc;

    public Integer getPayTimeoutSeconds() {
        return payTimeoutSeconds;
    }

    public OrderProperties setPayTimeoutSeconds(Integer payTimeoutSeconds) {
        this.payTimeoutSeconds = payTimeoutSeconds;
        return this;
    }

    public Integer getCreateFrequencySeconds() {
        return createFrequencySeconds;
    }

    public OrderProperties setCreateFrequencySeconds(Integer createFrequencySeconds) {
        this.createFrequencySeconds = createFrequencySeconds;
        return this;
    }

//    public String getDesc() {
//        return desc;
//    }
//
//    public OrderProperties setDesc(String desc) {
//        this.desc = desc;
//        return this;
//    }

}

nacos 配置文件截图

jhdbpxl9

jhdbpxl91#

这肯定是你应用自己的问题,关闭了应用。

3phpmpom

3phpmpom2#

这肯定是你应用自己的问题,关闭了应用。

大佬,那你能帮帮我吗?没几行代码

gg0vcinb

gg0vcinb3#

nacos-spring-boot 里有example, 先跑通example和理解example之后再试着修改到你的应用上。

uklbhaso

uklbhaso4#

nacos-spring-boot 里有example, 先跑通example和理解example之后再试着修改到你的应用上。

”nacos-spring-boot-example/nacos-spring-boot-config-example“ 里面的代码能跑是用了spring-boot-starter-web
如果不使用这个web包,替换成spring-boot-starter。也会出现了我一样的问题。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

log:

NacosConfigApplication.java

@SpringBootApplication
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class NacosConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApplication.class, args);
    }

    @Component
    public class ValueCommandLineRunner implements CommandLineRunner {
        private final Logger logger = LoggerFactory.getLogger(getClass());

        @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
        private boolean useLocalCache;

        @Override
        public void run(String... args) {
            logger.info("useLocalCache:" + useLocalCache);
        }

    }

}

pom.xml

<?xmlversion="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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>nacos-spring-boot-example</artifactId>
        <groupId>com.alibaba.nacos</groupId>
        <version>0.2.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nacos-spring-boot-config-example</artifactId>

    <properties>
        <nacos-config-spring-boot.version>0.2.12</nacos-config-spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>${nacos-config-spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-actuator</artifactId>
            <version>${nacos-config-spring-boot.version}</version>
        </dependency>
    </dependencies>
</project>

项目截图

相关问题