001. Nacos动态配置

一、NACOS下载并启动

1. NACOS下载

NACOS是什么就不解释了,根据官网的Quick Star的步骤走,首先下载Release版本,目前最新的版本是1.1.4。虽然在网上看到有些帖子说下载会比较慢,但没想到如此之慢,而且我下载到80%居然还失败了?!所以我采用的方式是下载源码自己构建,方法可参考官网的步骤:

git clone https://github.com/alibaba/nacos.git
cd nacos/
mvn -Prelease-nacos clean install -U  
ls -al distribution/target/

// change the $version to your actual path
cd distribution/target/nacos-server-$version/nacos/bin
2. NACOS启动

这种方式还是比较快的,构建成功之后,在目录nacos-1.1.4/distribution/target/nacos-server-1.1.4/nacos/bin下有个脚本文件,执行这个脚本文件就可以启动了。

Linux/Unix/Mac
启动命令(standalone代表着单机模式运行,非集群模式):

sh startup.sh -m standalone

如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:

bash startup.sh -m standalone

Windows
启动命令:

cmd startup.cmd

或者双击startup.cmd运行文件。

启动之后,在浏览器中输入localhost:8848/nocas就可以看到NACOS的后台管理界面,这里需要一个登录验证,用户名和密码都是nacos.

3. NACOS停止

Linux/Unix/Mac
sh shutdown.sh

Windows
cmd shutdown.cmd

或者双击shutdown.cmd运行文件。

二、动态配置

我这里使用的SpringCloud,但在实验的过程中遇到了一个坑,在NACOS后台更改配置后,页面上的值就是不动。看了半天文档后发现是版本的问题,这里有一篇文档解释的很清楚,按照文档中的版本修改后,就没有问题了。

1.项目准备

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.chad</groupId>
    <artifactId>alibaba-oss</artifactId>
    <version>1.0</version>
    <name>alibaba-oss</name>
    <description>NACOS动态配置</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.0.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


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

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

bootstrap.properties

spring.application.name=nacos-config
spring.cloud.nacos.config.server-addr=localhost:8848
spring.cloud.nacos.config.file-extension=properties

controller
这里类上面要加@RefreshScope注解才能实现动态配置的效果

@RestController
@RequestMapping("/")
@RefreshScope
public class SampleController {

    @Value("${user.name}")
    String userName;

    @Value("${user.age}")
    int age;
    @GetMapping("/user")
    public String sayHi() {
        System.out.println(this.age);
        return "Hello Nacos Config! Hello, I'm " + userName + " and " + age + " years old!";
    }
}

之后就是在NACOS后台进行配置了,如下图:

  • Data ID: 就是bootstrap.properties 中的spring.application.name的值
  • Group: 组名(还不知道是干啥的)
  • 配置格式:选择 properties
  • 配置内容:就是controller中@Value注解标识的属性

发布之后,启动自己写的应用输入localhost:8080/user,就可以看到打印的属性值了,然后还是在NACOS后台找到刚才添加的配置,然后编辑属性值,点击发布,刷新localhost:8080/user,正常情况下就可以看到配置属性已经更新了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值