• SpringCloud接入nacos配置中心


    版本选择

    spring-cloud-Alibaba版本依赖关系

    在这里插入图片描述
    本文章,采用的 springboot 版本是 2.6.13,spring-cloud-alibaba 版本是 2021.0.5.0,Nacos 服务端的版本是 NACOS 2.0.4

    官方整合文档:Nacos 融合 Spring Cloud,成为注册配置中心

    注意:我这里只是整合了配置管理,没有整合服务发现!

    项目搭建

    pom.xml

    
    <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>
        <groupId>top.chenfugroupId>
        <artifactId>chenfu-miniappartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>chenfu-miniappname>
        <description>chenfu-miniappdescription>
        <properties>
            <java.version>1.8java.version>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
            <spring-boot.version>2.6.13spring-boot.version>
            <spring-cloud-alibaba.version>2021.0.5.0spring-cloud-alibaba.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druid-spring-boot-starterartifactId>
                <version>1.2.23version>
            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.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>2.2.2version>
            dependency>
            <dependency>
                <groupId>com.mysqlgroupId>
                <artifactId>mysql-connector-jartifactId>
                <scope>runtimescope>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-dependenciesartifactId>
                    <version>${spring-boot.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>
                    <version>${spring-boot.version}version>
                    <configuration>
                        <mainClass>top.chenfu.ChenfuMiniappApplicationmainClass>
                        <skip>trueskip>
                    configuration>
                    <executions>
                        <execution>
                            <id>repackageid>
                            <goals>
                                <goal>repackagegoal>
                            goals>
                        execution>
                    executions>
                plugin>
            plugins>
        build>
    
    project>
    
    

    本地的 application.yml

    注意:spring.config.import 属性上已经开启了刷新配置项!此处配置的加载的是 Nacos 中的 chenfu-miniapp-dev.yml 配置文件。也是在这个属性上指定的。

    spring:
      application:
        name: chenfu-miniapp
      profiles:
        active: dev
      cloud:
        nacos:
          config:
            file-extension: yml
            server-addr: localhost:8848
            namespace: chenfu_dev
      config:
        import: nacos:${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}?refresh=true
    

    chenfu-miniapp-dev.yml 中的配置项

    # 数据源配置
    spring:
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        druid:
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: root
          url: jdbc:mysql://localhost:3306/chenfu?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true
      resources:
        static-locations: classpath:/static/,classpath:/views/
      servlet:
        multipart:
          location: /home
    chenfu:
      config:
        v: sss
    

    接收配置的实体类

    @Data
    @SpringBootConfiguration
    @ConfigurationProperties(prefix = "chenfu.config")
    public class PropertiesConfig {
    
        private String v;
    
    }
    

    至此整合完毕,启动就可以使用 Nacos 来管理配置文件了

  • 相关阅读:
    GPT接入企微应用 - 让工作快乐起来
    阿里云几核服务器够用?内存多少合适?
    Jeewx-api 1.4.9版本发布—第三方APP开发SDK,支持微信、钉钉、企业微信、小程序等
    xss攻击与csrf攻击
    2022/11/7(cf·div2·CTR3)https://codeforces.com/contest/1750
    SpringBoot 01 HelloSpringBoot
    树莓派4b开机自启sh脚本
    js执行机制
    less 用法
    IC集成电路 测试与验证的区别?
  • 原文地址:https://blog.csdn.net/CHENFU_ZKK/article/details/143321273