分布式配置nacos搭建踩坑指南(下)

时间:2023-02-11 22:05:28

  上一篇介绍了在配置nacos中的碰到的坑,这一篇介绍一下如何正确进行nacos的环境搭建和配置,所以本文分为两部分,第一部分为环境搭建,介绍如何安装和运行。第二部分为alibaba Sprint Boot配置,介绍如何正确配置。

  注意:本文基于nacos 2.2.0,alibaba Spring Boot 2.6.11,eclipse,操作系统为windows.

 

                                       一.环境搭建

 

  1.下载nacos

   在github上下载nacos的windows安装文件,网址为:https://github.com/alibaba/nacos/releases,这里选择2.2.0下载,选择.zip格式的即可,入下所示:

分布式配置nacos搭建踩坑指南(下)

 

2.运行nacos

  解压下载回来的zip文件至D盘,进入bin文件夹,运行:.\startup.cmd -m standalone 启动nacos,如果看到" Nacos started successfully in stand alone mode. use embedded storage",恭喜!运行成功!如下所示:

 分布式配置nacos搭建踩坑指南(下)

 

 

3.登录配置界面:

 在浏览器中访问:http://localhost:8848/nacos/,用户名和密码都是nacos,即可进入后台配置页面,如下所示:

分布式配置nacos搭建踩坑指南(下)

 

 

 

 

 第二部分 Spring Boot配置

1.下载alibaba Spring Boot脚手架

  访问https://start.aliyun.com/bootstrap.html,Group输入:com.alibaba.cloud,Artifact:nacos-config-sample,组件选择:Nacos Configuration,Spring Web,Spring Boot Actuator,同时需要引入Spring Cloud下的Cloud Bootstrap。这个在阿里巴巴出版的电子书<<Spring Cloud alibaba 从入门到实战>>中没有介绍。如果没有引入Cloud Bootstrap,在eclipse中运行后必报错,截图如下:

  分布式配置nacos搭建踩坑指南(下)

分布式配置nacos搭建踩坑指南(下)

 

 

 下面是完整的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.6.11</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>nacos-config-sample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacos-config-sample</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
        <spring-cloud.version>2021.0.4</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>

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

    <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>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

  

2.配置namespace

  访问http://localhost:8848/nacos/,在后台新建一个名为testnamespace的namespace,记住它的id:bd1caf11-e4f1-4dd0-9a4b-2c32041c64e2,待会在spring boot配置中要用到,如下所示:

分布式配置nacos搭建踩坑指南(下)

3.配置Properties

在配置管理中,我们选择刚才新建的testnamespace命名空间,在这个下面创建配置,新增一个data id为:nacos-config-sample.properties 的配置,配置格式选择properties.注意,data id的格式为{spring boot 应用名}.properties,两边必须要一致,否则报错!如下所示:

分布式配置nacos搭建踩坑指南(下)

分布式配置nacos搭建踩坑指南(下)

 

4.配置Spring Boot:

1)配置bootstrap.properties

spring.cloud.nacos.config.username=nacos
spring.cloud.nacos.config.password=nacos
spring.cloud.nacos.config.contextPath=/nacos
spring.cloud.nacos.config.server-addr=localhost:8848
spring.cloud.nacos.config.namespace=bd1caf11-e4f1-4dd0-9a4b-2c32041c64e2 
spring.cloud.nacos.config.extension-configs[0].data-id=nacos-config-sample.properties
spring.cloud.nacos.config.extension-configs[0].refresh=true

 

 注意spring.cloud.nacos.config.namespace=bd1caf11-e4f1-4dd0-9a4b-2c32041c64e2,这个namespace就是上面新建命名空间的id.

 

 2)配置application.properties

spring.application.name=nacos-config-sample
server.port=8080
management.server.port=8081
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

  

3)新建实体类

package com.alibaba.cloud.nacosconfigsample;

import com.alibaba.nacos.api.config.annotation.*;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.context.scope.*;


@RefreshScope
//@ConfigurationProperties(prefix="user")
public class User {
   
	private String name;
	private int age;
	
	public String getName() {
		return name;
		
	}
	
	public void setName(String name) {
		this.name=name;
		
	}
	
	public int getAge() {
		return this.age;
		
	}
	
	public void setAge(int age) {
		this.age=age;
		
	}
	
}

  

 4)Spring boot启动类 

package com.alibaba.cloud.nacosconfigsample;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.alibaba.nacos.api.config.annotation.*;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import javax.annotation.*;

@RestController
@SpringBootApplication
@RefreshScope
//@EnableConfigurationProperties(User.class)
public class NacosConfigSampleApplication {

	
	@Value("${user.name}") //import org.springframework.cloud.context.config.annotation.RefreshScope;
	private String userName;
	
	@Value("${user.age}")
	private int age;
	
	@PostConstruct
	public void init() {
		
		System.out.printf("[init] user age is: %s",userName);
	}
	
//	@Autowired
//	private User user;
	
	@RequestMapping("/user")
	public String user() {
		
		return  "user :"+userName;
	//return user.getName();
	}
	
	@PreDestroy
	public void destroy() {
		
		System.out.printf("[destroy] user age : %s ",userName);
	}
	
	
    public static void main(String[] args) {
        SpringApplication.run(NacosConfigSampleApplication.class, args);
    }

}

 

 

5)修改配置中的user.的name,在spring boot中观察是否更新