• SpringBoot 集成 Nacos


    目录

    Nacos安装详见:Spring Cloud 系列之 Nacos 配置中心

    一、前言

    上一篇已经讲解了怎样安装安装、启动、配置 Nacos,这篇我们讲解如何在项目中使用 Nacos

    还不了解 Nacos 的详见:Spring Cloud 系列之 Nacos 配置中心

    在集成 Nacos 之前,首先我们要先创建一个 Spring Boot 项目:IDEA 创建 SpringBoot 项目

    二、Nacos集成

    1、引入Nacos依赖

    
    	
    	
    		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
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    注:Spring Boot版本要低于2.4,否则启动应用会报错。
    在这里插入图片描述

    2、设置Nacos配置

    项目中默认配置文件是 application.propertiesNacos 配置加在此配置文件中的话,应用启动会报连接 Nacos 失败,我们需要创建 bootstrap.propertiesbootstrap.yml 配置文件(添加任意一个即可),下面我们以 bootstrap.properties 为例:

    spring.application.name=apm-mobile-android
    spring.cloud.nacos.username=nacos
    spring.cloud.nacos.password=nacos
    spring.cloud.nacos.server-addr=10.0.7.115:18117
    
    spring.cloud.nacos.discovery.namespace=PROD
    spring.cloud.nacos.config.namespace=PROD
    spring.cloud.nacos.config.timeout=3000
    spring.cloud.nacos.config.refresh-enabled=true
    
    spring.cloud.nacos.config.group=apm
    spring.cloud.nacos.config.prefix=${spring.application.name}
    spring.cloud.nacos.config.file-extension=properties
    
    spring.cloud.nacos.config.shared-configs[0].group=apm
    spring.cloud.nacos.config.shared-configs[0].data-id=apm-mobile-android.properties
    spring.cloud.nacos.config.shared-configs[0].refresh=true
    
    spring.liquibase.enabled=false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    3、加载Nacos配置中心配置项

    在初始化类中添加 @EnableDiscoveryClient 注解即可:

    package com.example.springbootdemo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @EnableDiscoveryClient
    @SpringBootApplication
    public class SpringbootdemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootdemoApplication.class, args);
            new BootstrapManager();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    4、Nacos集成验证

    Nacos配置如下:

    在这里插入图片描述

    启动应用,然后访问:http://localhost:8085/hello

    出现如下界面说明加载Nacos配置成功。

    在这里插入图片描述

    5、Nacos配置中心配置项动态生效

    需要在配置对象中添加 @RefreshScope 注解,然后重启应用。

    package com.example.springbootdemo.config;
    
    import lombok.Data;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.stereotype.Component;
    
    @Data
    @Component
    @Configuration
    @RefreshScope
    public class GlobalConfig {
    
        @Value("${data.domain:http://10.0.0.1:18080}")
        private String dataDomain;
    
        @Value("${log.level:DEBUG}")
        private String logLevel;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

    重启后,访问:http://localhost:8085/hello

    在这里插入图片描述

    Nacos 配置中的 log.level 修改为 DEBUG ,然后重新访问:http://localhost:8085/hello,出现如下界面说明 Nacos 配置动态生成成功。

    在这里插入图片描述

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    苹果Audio Classifier使用问题
    [安卓] 列表之ArrayAdapter适配
    鸿蒙Ability Kit(程序框架服务)【UIExtensionAbility】
    uniapp 页面滚动到指定位置的方法
    C语言中可变参数函数的实现——printf
    简单丝的tab切换栏(html/CSS)
    C++ Const
    HAproxy+keepalived+nginx实验部署
    1.3 Apache Hadoop的重要组成-hadoop-最全最完整的保姆级的java大数据学习资料
    2022年全球市场公路和碎石路自行车总体规模、主要生产商、主要地区、产品和应用细分研究报告
  • 原文地址:https://blog.csdn.net/web13985085406/article/details/126114317