码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Spring Cloud项目(二)——集成Nacos作为配置中心


    项目结构

    在这里插入图片描述

    代码编写

    1、pom文件

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-nacos-config</artifactId>
            </dependency>
        </dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2、application文件

    spring:
      profiles:
    # 表示你当前所使用的分支,dev开发分支,prod生产分支
    #    active: prod
        active: dev
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3、bootstrap文件

    文件说明:
    同application.yml文件一样,都是springboot的配置文件,但是springboot优先读取bootstrap.yml文件

    server:
      port: 8082
    
    spring:
      application:
        name: nacos-config
    
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848 # 注册中心路径
          config:
            server-addr: 127.0.0.1:8848 # 配置中心路径
            file-extension: yaml  # 后缀名
    #        group: DEV_GROUP  # 所在组
    #        namespace: DEV  # 命名空间
    # 读取的文件命名规则:{应用名}-{所处环境}.{文件后缀名},比如 nacos-config-dev.yaml
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    4、启动类

    package com.lx;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    /**
     * description TODO
     *
     * @author 流星
     * @date 2022/8/18 15:51
     */
    @SpringBootApplication
    @EnableDiscoveryClient
    public class ConfigApp {
        public static void main(String[] args) {
            SpringApplication.run(ConfigApp.class,args);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    5、controller

    package com.lx.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RefreshScope
    public class ConfigController {
    
        @Value("${config.info}")
        private String configInfo;
    
        @RequestMapping("/configInfo")
        public String getConfigInfo(){
            return configInfo;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    测试结果

    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述

    结语

    下面关于Spring Cloud学习系列的博客的连接有的是不能使用的,因为博主还在努力创作中,敬请期待

    附:Spring Cloud学习系列

    Spring Cloud项目(一)——集成Nacos作为注册中心
    Spring Cloud项目(二)——集成Nacos作为配置中心
    Spring Cloud项目(三)——实现Nacos数据信息持久化到MySQL
    Spring Cloud项目(四)——使用Ribbon作为负载均衡
    Spring Cloud项目(五)——使用openFeign作为服务调用
    Spring Cloud项目(六)——使用sentinel作为流量管理
    Spring Cloud项目(七)——使用sentinel作为限流和熔断
    Spring Cloud项目(八)——使用gateway作为服务网关

  • 相关阅读:
    计算机毕业设计Java远程教育系统(源码+系统+mysql数据库+Lw文档)
    基于JAVA在线辅导答疑系统计算机毕业设计源码+系统+数据库+lw文档+部署
    【Android】修改aar包里的jar包里的class文件内容
    网络协议学习:虚拟路由冗余协VRRP
    MATLAB未定义函数或变量‘polyadd‘的解决办法
    【云原生之kubernetes】在kubernetes集群下的映射外部服务—Eendpoint
    C语言之练习题
    C++中的双冒号(::) 使用小结
    Maxwell 概述、安装、数据同步【一篇搞定】!
    【20220623】【信号处理】深入理解Pearson相关系数和Matlab corr()、corrcoef()仿真
  • 原文地址:https://blog.csdn.net/weixin_45974176/article/details/126408940
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号