开发者社区 > 云原生 > 正文

Nacos配置示例启动失败

rt

@RestController @RefreshScope public class EchoController {

@Value("${useLocalCache:cc}")
private String useLocalCache;

@RequestMapping("/get")
@ResponseBody
public String get() {
    return useLocalCache;
}

public void setUseLocalCache(String useLocalCache) {
    this.useLocalCache = useLocalCache;
}

}

@SpringBootApplication @Slf4j public class NacosApplication { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(NacosApplication.class); // while (true) { // //当动态配置刷新时,会更新到 Enviroment中,因此这里每隔一秒中从Enviroment中获取配置 // String userName = applicationContext.getEnvironment().getProperty("name"); // String userAge = applicationContext.getEnvironment().getProperty("gg"); // log.info("配置信息,name:{},gg:{}", userName, userAge); // try { // TimeUnit.SECONDS.sleep(1); // } catch (InterruptedException e) { // e.printStackTrace(); // } // } } }

spring.application.name=nacos spring.cloud.nacos.config.server-addr=127.0.0.1:8848

spring.application.name=nacos spring.cloud.nacos.config.server-addr=127.0.0.1:8848

cloud top.huzhurong.alibaba 1.0-SNAPSHOT

4.0.0

nacos-producer

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version>

org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

org.springframework.boot spring-boot-maven-plugin ${spring-boot.version} repackage top.huzhurong.cloud.nacos.discovery.NacosApplication

parent#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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>top.huzhurong.alibaba</groupId>
    <artifactId>cloud</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>nacos-producer</module>
        <module>nacos-consumer</module>
    </modules>


    <properties>
        <spring-boot.version>2.0.4.RELEASE</spring-boot.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>

        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <target>8</target>
                    <source>8</source>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

无法刷新配置:)@mercyblitz

原提问者GitHub用户chenshun00

展开
收起
白夜行fighting 2023-06-11 10:32:42 96 0
1 条回答
写回答
取消 提交回答
  • 参考https://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/Nacos-config,请确保bootstrap.yml具有正确的配置。

    原回答者GitHub用户fangjian0423

    2023-06-11 11:27:25
    赞同 展开评论 打赏

阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。

相关电子书

更多
Nacos架构&原理 立即下载
workshop专场-微服务专场-开发者动手实践营-微服务-使用Nacos进行服务的动态发现和流量调度 立即下载
Nacos 启航,发布第一个版本, 云原生时代助力用户微服务平台建设 立即下载