spring-boot 配置中心 nacos 简易部署+ 手把手教


最近搞一个配置中心,看了很多大牛的讲解愣是没有学会,于是终于学废了~.~

再后来,使用了一个简单的方法,版本啥的也没管,然后就成了~,特此记录一下(炫耀一下。。。)

1、项目创建

  1)idea中创建项目 选择创建springboot的项目:

                                           

  2)选择java8,填写项目名demo后点击next

                                           

  3)选择web的,就这一个就行,因为是服务,需要一直启动。然后点击next再点击finish结束。

  4)配置pom(依赖就这四个就行)

<?xml version="1.0" encoding="UTF-8"?>

    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.4.RELEASE   
         
    
    com.example
    centers
    0.0.1-SNAPSHOT
    centers
    Demo project for Spring Boot
    
        1.8
    

    
    
    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-nacos-config
        2.2.1.RELEASE
    
    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-nacos-discovery
        2.2.1.RELEASE
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


  如果你没有仓库可以下载这些依赖,那配置一下alibaba的库,一个就够用了。

  5)创建boostrap.yml

spring:
  application:
    name: nacos-config
    ## 当前环境,这个和dataId有关-> ${prefix}-${spring.profiles.active}.${file-extension}
   ## profiles: active: dev cloud: nacos: config: ## nacos的地址,作为配置中心 server-addr: 127.0.0.1:8848 ## 配置内容的数据格式,目前只支持 properties 和 yaml 类型,这个和dataId有关-> ${prefix}-${spring.profiles.active}.${file-extension} file-extension: properties management: endpoints: web: exposure: ## yml文件中存在特殊字符,必须用单引号包含,否则启动报错 include: '*'

上面三个有颜色的文字后面有用(nacos中的配置文件名:nacos-config-dev.properties)

  6)创建测试类

package com.example.centers;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//refreshscope这个注解用于动态加载配置,即你远程改了配置后立即生效,不加的话,每次改都要重启项目 @RestController @RefreshScope @RequestMapping("/nacos") public class NacosController { @Value("${config.version}") private String version; @GetMapping("/test/{id}") public String test(@PathVariable("id")Integer id){ return "accept one msg id="+id+"----- version="+version; } }

  7)项目最终结构:   

                                                 

 2、nacos下载、启动、配置项

   1)下载地址:https://github.com/alibaba/nacos/releases 使劲向下划,找到zip,这个是windows上启动的,上面gz的那个是linux上用的。

   2)启动:

    解压后进入bin目录,双击startup.cmd ,等一会弹出运行框。

      出现  2022-04-15 21:29:36,967 INFO Completed initialization in 6 ms  就是启动完成了。

   3)配置:

      启动完成后,进入http://localhost:8848/nacos

      1)点击新增的加号

       2)填写信息,照着填写,然后点击发布,就好了

3、测试

    1)启动项目

    2)浏览器输入:http://localhost:8080/nacos2/test/1

      页面显示:accept one msg id=1----- version=1314

      大功告成!!

    3)修改一下nacos的nacos-config-deb.properties配置里的值,随便设置一个值,如:

      
config.version=110

    4)刷新浏览器的页面发现,页面显示:accept one msg id=1----- version=110

      完美~!

      完美~!

      完美~!

nacos还可以链接mysql、还可以作为注册中心、还可以配置分布式的等等,要学习的地方还有很多~!