一.需求

    每次当我们配置相关环境配置文件时,难免需要实时做好历史记录以及回滚工作,而人工去手动回滚配置需要大量时间,所有有了配置中心来统一管理服务的配置

二.工具

    Nacos:支持基于 DNS 和基于 RPC 的服务发现(可以作为springcloud的注册中心)、动态配置服务(可以做配置中心)、动态 DNS 服务。

三.部署nacos

   linux环境:

git clone https://github.com/alibaba/nacos.git

cd nacos/

mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U  

ls -al distribution/target/

// change the $version to your actual path
cd distribution/target/nacos-server-$version/nacos/bin

sh startup.sh -m standalone

   windows环境

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

     

     2. 解压

     3.进入nacos\nacos\distribution\target\nacos-server-1.2.1\nacos\bin目录,然后点击startup.cmd

       

      

   配置自己数据库

      1.打开目录nacos\nacos\distribution\target\nacos-server-1.2.1\nacos\conf,修改application.properties配置文件

      

           

         2.将该目录的shcema.sql文件导入本地数据库

         

     

    登录Nacos

        登录http://localhost:8848/地址(初始账号密码为 nacos/nacos)

         

         

四.springboot整合nacos

     相关依赖:

       springboot最好在2.0.0以上

<?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>com.maxnerva.cloud</groupId>
    <artifactId>maxsphere-nacos</artifactId>
    <version>1.0.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <test.project.version>10.01.01</test.project.version>
        <java.version>1.8</java.version>
        <nacos.config.vsersion>0.2.1.RELEASE</nacos.config.vsersion>
        <fastjson.version>1.1.28</fastjson.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
        <spring-cloud-alibaba.version>0.2.1.RELEASE</spring-cloud-alibaba.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--SpringCloud Alibaba-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

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

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

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

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- nacos配置依赖引入的是springcloud的nacos依赖所以上面要加入springcloud的依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>${nacos.config.vsersion}</version>
        </dependency>
        <!-- 服务发现依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${nacos.config.vsersion}</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
    </dependencies>

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

</project>

  配置文件

     1.application.yaml:

spring:
  application:
    name: maxsphere-nacos
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

     2.bootstrap.properties

#指定开发环境
spring.profiles.active=dev
#服务器地址
spring.cloud.nacos.config.server-addr=localhost:8848
#指定配置群组 --如果是DEFAULT_GROUP组 则可以省略群组配置
spring.cloud.nacos.config.group=DEFAULT_GROUP
#文件名 -- 如果没有配置则默认为 ${spring.appliction.name}
spring.cloud.nacos.config.prefix=nacos-admin
#指定文件后缀
spring.cloud.nacos.config.file-extension=properties

  业务处理

    1.启动类:(注意加@EnableDiscoveryClient注解)

@EnableDiscoveryClient
@SpringBootApplication
public class MaxsphereApplication {

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

    2.controller层实现

@RestController
@RequestMapping("/getConfig")
public class ConfigController {

    @Value("${test.name: false}")
    private String username;

    @GetMapping("/getName")
    public String getConfig(){
        return username;
    }
}

  Nacos添加配置

     

     

  访问springboot服务

       访问:http://localhost:10017/getConfig/getName

           

      

Logo

更多推荐