当前位置: 首页 > news >正文

spring-boot nacos

spring-boot nacos
 


nacos-config-spring-boot-starter
nacos-discovery-spring-boot-starter

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>dr.cqr</groupId>
    <artifactId>demor</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <java.version>1.8</java.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.11</version>
        </dependency>
        <!-- 代码生成器 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.11</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.34</version>
        </dependency>

        <!-- 分页 -->
        <!-- jdk 8+ 引入可选模块 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-jsqlparser-4.9</artifactId>
        </dependency>

        <!-- 多数据源 -->
        <!-- https://mvnrepository.com/artifact/com.baomidou/dynamic-datasource-spring-boot-starter -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>4.3.1</version>
        </dependency>

        <!-- oracle -->
        <!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 -->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>23.7.0.25.01</version>
            <scope>runtime</scope>
        </dependency>

        <!-- oracle字符集支持 -->
        <!-- Additional library required to support Internationalization -->
        <dependency>
            <groupId>com.oracle.database.nls</groupId>
            <artifactId>orai18n</artifactId>
            <version>21.5.0.0</version>
            <scope>provided</scope>
        </dependency>


        <!-- 版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。-->
        <!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>0.2.12</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-discovery-spring-boot-starter</artifactId>
            <version>0.2.12</version>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-bom</artifactId>
                <version>3.5.11</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 nacos配置

nacos:
  # 配置中心
  config:
    server-addr: localhost:8848
    # 权限认证,nacos.core.auth.enabled=true 时需要添加
    username: nacos
    password: nacos
    file-extension: yaml
    # 设置配置文件所属命令空间
    namespace: e9232680-860c-4217-b312-75de46e374e9
    #服务注册
  discovery:
    username: nacos
    password: nacos
    server-addr: localhost:8848
    # 服务注册命令空间
    namespace: e9232680-860c-4217-b312-75de46e374e9
    file-extension: yaml
    # 设置配置文件所属组
    group: DEFAULT_GROUP

 

 启动配置

@SpringBootApplication
@RestController
@NacosPropertySource(dataId = "demor.yml", autoRefreshed = true)
public class APPApplication {
    public static void main(String[] args) {
        SpringApplication.run(APPApplication.class, args);
        System.out.println("################### 启动成功 ##################");
    }
}

获取服务列表和配置文件 

    //获取服务列表
    @NacosInjected
    private NamingService namingService;
    //获取配置文件
    @NacosValue(value = "${ff.fg}", autoRefreshed = true)
    private String fg;
    @GetMapping("/nacos")
    public String nacos () throws IOException, NacosException {

        System.out.println(fg);
        System.out.println(namingService.getAllInstances("demor"));


        return "ok";
    }

相关文章:

  • deepin使用autokey添加微信快捷键一键显隐ctrl+alt+w
  • CExercise_12_单链表面试题_1求链表中间结点的值,判断单链表是否有环
  • 代码随想录训练营第31天 || 56. 合并区间 738. 单调递增的数字
  • gitee基本使用
  • Shell编程之循环语句
  • 【前端样式】使用Flexbox实现经典导航栏:自适应间距与移动端折叠实战
  • MATLAB基本数据类型
  • 如何一键自动提取CAD图中的中心线(如墙体、道路、巷道中心线等)
  • Android常见界面控件、程序活动单元Activity练习
  • LeetCode算法题(Go语言实现)_46
  • 3.2.2.3 Spring Boot配置拦截器
  • C++学习之数据库操作
  • AI日报 - 2025年4月15日
  • 华为OD机试真题——阿里巴巴找黄金宝箱 IV(2025A卷:200分)Java/python/JavaScript/C++/C语言/GO六种最佳实现
  • 子串-滑动窗口的最大值
  • 科研软件分享
  • AI agents系列之全从零开始构建
  • 批处理(Batch Processing)的详解、流程及框架/工具的详细对比
  • 前端工程化之自动化构建
  • .NET MCP 文档
  • 女乘客遭顺风车甩客、深夜丢高速服务区,滴滴霸道回应:赔五百元
  • 上海明天起进入“升温通道”,五一假期冲刺33℃
  • 马上评丨发钱奖励结婚,支持婚育就该系统性发力
  • 伊朗最大港口爆炸:26公里外都能听到,超七百人受伤,原因指向化学品储存
  • 精准滴灌“种企业”,苏南强县常熟新的进阶密码
  • 迎接神十九乘组回家,东风着陆场各项工作已准备就绪