SpringBoot使用Nacos配置中心

alibaba
本文介绍SpringBoot如何使用阿里巴巴Nacos做配置中心。

1.Nacos简介

Nacos是阿里巴巴集团开源的一个易于使用的平台,专为动态服务发现,配置和服务管理而设计。它可以帮助您轻松构建云本机应用程序和微服务平台。

Nacos基本上支持现在所有类型的服务,例如,Dubbo / gRPC服务,Spring Cloud RESTFul服务或Kubernetes服务。

尤其是使用Eureka注册中心的,并且担心Eureka闭源的开发者们,可以将注册中心修改为Nacos,本文主要介绍Nacos配置中心的使用。

Nacos官网如下图所示,官网地址 https://nacos.io/zh-cn/

2.Nacos安装

Nacos安装可以采用如下两种方式:

1.官网下载稳定版本解压使用。
2.下载源代码编译使用,目前最新的版本是1.1.3版本。
本文简单介绍一下第二种方式,到Nacos的稳定版本下载地址https://github.com/alibaba/nacos/releases,下载最新版,本文下的是tag.gz文件,下载后解压即安装完成,然后进入解压目录后的bin目录执行如下命令启动Nacos。

sh startup.sh -m standalone
启动可以看到控制台如图所示,端口号是8848(好像是因为珠穆朗玛峰的高度),版本0.8.0等等信息。

3.SpringBoot使用Nacos

接下来,创建项目,项目中加入使用Nacos配置中心的依赖nacos-config-spring-boot-starter,完整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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 
     <properties>
        <java.version>1.8</java.version>
    </properties>

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

     
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>0.2.3</version>
        </dependency>
    </dependencies>

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

配置文件中需要配置Nacos服务的地址,如下所示。

# nacos 配置
nacos:
  config:
    server-addr: 127.0.0.1:8848,127.0.0.2:8848
    # 分组
    group: DEFAULT_GROUP
    # 空间 public 空间不需要填写此值
    namespace: 06301150-8fbe-4f75-9351-3143b086a7d6

在启动类,加入@NacosPropertySource注解其中包含两个属性,如下:

  • dataId:这个属性是需要在Nacos中配置的Data Id。
  • autoRefreshed:为true的话开启自动更新。

在使用Nacos做配置中心后,需要使用@NacosValue注解获取配置,使用方式与@Value一样,完整启动类代码如下所示。

package com.xxxxx;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@NacosPropertySource(dataId = "springboot2-nacos-config.yaml", autoRefreshed = true)
@RestController
public class Springboot2NacosConfigApplication {

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

    @NacosValue(value = "${nacos.test.propertie:123}", autoRefreshed = true)
    private String testProperties;

    @GetMapping("/test")
    public String test(){
        return testProperties;
    }
}

由于本文只是简单示例使用Nacos做配置中心,所以将启动类加了一个MVC方法,作为输出配置信息进行测试,这个测试的配置给了一个默认值123,启动项目,访问http://localhost:8080/test,可以看到如下所示:

使用配置文件进行配置读取(注意必须配置默认值,否则启动会报错的)

这样就可以依赖注入进行使用了

package com.xxx.config;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: zhuwl
 * @Date: 2019/10/23 11:58
 * @Description:
 **/
@Component
@Data
public class NacosConfig {
    @NacosValue(value = "${nacos.test.propertie:123}", autoRefreshed = true)
    private String nacosTestProp;
}

4.使用Nacos修改配置

访问Nacos服务,http://localhost:8848/nacos/#/login,默认情况用户名密码都是nacos,登录进行修改测试。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值