欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringCloud 集成 nacos-config

程序员文章站 2022-07-12 23:22:28
...

1.下载安装Nacos

注:本文是Windows环境 ,版本是nacos-server-1.4.0

下载链接:  https://github.com/alibaba/nacos/releases

SpringCloud 集成 nacos-config

解压缩之后得到文件夹,在该目录下单机模式启动,启动命令 startup.cmd  -m standalone

SpringCloud 集成 nacos-config

本地启动成功后访问http://127.0.0.1:8848/nacos,用户名:nacos   密码: nacos

SpringCloud 集成 nacos-config

 

2.准备代码环境

     2.1引入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.3.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>nacos-config</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <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-discovery</artifactId>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

    2.2配置文件(由于本文是集成的nacos-cofig,注意是bootstrap.yml,不是application.yml)

server:
  port: 8040
spring:
  application:
    name: nacos-config-demo
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        prefix: nacos-config
      profiles:
        active: dev

  2.3 nacos管理界面新建配置

  SpringCloud 集成 nacos-config

注(DataId格式为: ${prefix}-${spring.profiles.active}.${file-extension} )

SpringCloud 集成 nacos-config

 

 2.4 JAVA代码使用

package com.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RefreshScope
public class MainApplication {

    @Value("${nacos.config:test-local}")
    private String config;

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

    @RequestMapping("/test")
    public String test(){
        return config;
    }

}

 请求测试

SpringCloud 集成 nacos-config

相关标签: 注册中心