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


      上一篇介绍了在配置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格式的即可,入下所示:

     

    2.运行nacos

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

     

     

     

    3.登录配置界面:

     在浏览器中访问:http://localhost:8848/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。这个在阿里巴巴出版的电子书<>中没有介绍。如果没有引入Cloud Bootstrap,在eclipse中运行后必报错,截图如下:

      

     

     

     下面是完整的pom.xml文件:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    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.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.6.11version>
            <relativePath/>
        parent>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>nacos-config-sampleartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>nacos-config-samplename>
        <description>Demo project for Spring Bootdescription>
     
        <properties>
            <java.version>1.8java.version>
            <spring-cloud-alibaba.version>2021.0.4.0spring-cloud-alibaba.version>
            <spring-cloud.version>2021.0.4spring-cloud.version>
        properties>
     
        <dependencies>
        <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-bootstrapartifactId>
    dependency>
     
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-actuatorartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starterartifactId>
            dependency>
     
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
     
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloudgroupId>
                    <artifactId>spring-cloud-dependenciesartifactId>
                    <version>${spring-cloud.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
                <dependency>
                    <groupId>com.alibaba.cloudgroupId>
                    <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                    <version>${spring-cloud-alibaba.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
            dependencies>
        dependencyManagement>
     
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>3.8.1version>
                    <configuration>
                        <source>1.8source>
                        <target>1.8target>
                        <encoding>UTF-8encoding>
                    configuration>
                plugin>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
     
    project>

      

    2.配置namespace

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

    3.配置Properties

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

     

    4.配置Spring Boot:

    1)配置bootstrap.properties

    1
    2
    3
    4
    5
    6
    7
    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

    1
    2
    3
    4
    5
    6
    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)新建实体类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    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启动类 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    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中观察是否更新

     

       

        

  • 相关阅读:
    【电脑录制屏】如何使用bandicam录游戏 设置图文教程
    2023后端面试题(持续性更新)
    【2023集创赛】加速科技杯作品:高光响应的二硫化铼光电探测器
    迅为i.MX8MM开发板Coatrx-M4内核开发给IAR安装8MM补丁
    Java实现递归查询树结构
    this用法总结
    Unity VFX Output 模块详解
    网易企业邮箱免费版管理员密码忘记的三大找回方式
    Python部分异常日志缺失
    2024五一杯数学建模B题思路分析
  • 原文地址:https://www.cnblogs.com/wangsanfeng/p/17111262.html