美文网首页
nacos作为配置中心消费端最简单配置

nacos作为配置中心消费端最简单配置

作者: 梨捉阳笆 | 来源:发表于2021-04-16 10:58 被阅读0次

    pom配置

    <properties>
            <java.version>1.8</java.version>
            <nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version>
        </properties>
        <dependencies>
            <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>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>RELEASE</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    

    启动类配置

    @SpringBootApplication
    @NacosPropertySource(dataId = "demo",autoRefreshed = true)
    public class NacosConfigApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(NacosConfigApplication.class, args);
        }
    
    }
    

    controller配置

    @RestController
    @RequestMapping("config")
    public class ConfigController {
        @NacosValue(value = "${testFlag}",autoRefreshed = true)
        private boolean testFlag;
    
        @RequestMapping(value = "/flag",method = GET)
        public boolean fetchFlag() {
            return testFlag;
        }
    }
    

    相关文章

      网友评论

          本文标题:nacos作为配置中心消费端最简单配置

          本文链接:https://www.haomeiwen.com/subject/fctelltx.html