5.Spring Cloud (Hoxton.SR8) 学习笔记—IDEA 创建 Spring Cloud、配置文件样例

一、IDEA 创建 Spring Cloud 基本步骤

创建父项目 (Project)

点击查看: Spring Cloud实战教程

父项目
在这里插入图片描述

父项目只需要 保留 这些文件, 其他的 删除:
在这里插入图片描述


创建子模块 (Module)

子模块 (方式一[Maven]: 推荐)
在这里插入图片描述

子模块 (方式二[Spring Initializr])
在这里插入图片描述


创建 Common 模块

Common 模块 中包含的是 请求封装类响应封装类分页封装类 等, pom.xml 文件中包含一些子模块 通用的依赖。(父项目 中包含的依赖多为各种依赖的 启动器)


创建 Generator 模块

Generator 模块 使用 Mybatis-Plus-Generator 依赖, 根据数据库表生成: 实体类MapperServiceServiceImplController 代码。


Spring Cloud 中的依赖版本对应关系?

点击查看: Spring Cloud 和 Spring Boot 版本对应

推荐版本:

  • Spring Cloud: Hoxton.SR8 (最常用的版本)
  • Spring Cloud Alibaba: 2.2.5.RELEASE
  • Spring Boot: 2.3.2.RELEASE

所有版本关系:

<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    <cloud-alibabba.version>2.2.5.RELEASE</cloud-alibabba.version>
    <mysql.version>8.0.26</mysql.version>
    <mybatis-plus.version>3.5.2</mybatis-plus.version>
    <druid.version>1.2.14</druid.version>
    <kaptcha.version>1.2.1</kaptcha.version>
    <fastjson.version>1.2.71</fastjson.version>
    <httpclientutil.version>1.0.4</httpclientutil.version>
    <commons-lang.version>2.6</commons-lang.version>
    <commons-collections.version>3.2.2</commons-collections.version>
    <commons-io.version>2.6</commons-io.version>
</properties>

Spring Cloud实现模块间相互调用(引入模块)?

Springcloud-实现跨项目相互调用 (简易版: 不使用Feign版本)


Maven项目命名规范(groupID、artifactid)

点击查看: Maven项目命名规范 (groupID、artifactid)

<groupId>: 定义当前 Maven项目 隶属的 实际项目,例如 com.compang.project

  • 前半部分com.compang: 代表此项目隶属的 公司
  • 后半部分project: 代表 项目的名称
  • 例如: 腾讯(tencent)微信(wechat)项目: com.tencent.wechat

<artifactId>: 构件ID, 该元素定义实际项目中的一个 Maven项目 或者是 子模块

  • 构建名 称必须 小写字母,没有其他的 特殊字符
  • 父项目: 【公司名-项目名】,例如:tencent-wechat
  • 子模块: 【公司名-项目名-模块名】,例如:tencent-wechat-usertencent-wechat-file
    > - 子模块方案二: 【项目名-模块名】,例如:wechat-userwechat-file

父模块

<groupId>com.tencent.wechat</groupId>
<artifactId>tencent-wechat</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

子模块

<parent>
    <groupId>com.tencent.wechat</groupId>
    <artifactId>tencent-wechat</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>tencent-wechat-user</artifactId>

Spring Cloud中 父模块 和 子模块 的依赖关系?

点击查看: Spring Cloud 中 dependencyManagement、type、scope在 父模块 和 子模块 的作用?

  • 父项目 中使用 dependencyManagement,继承该 父项目子项目 默认不会直接引入 dependencyManagement 管理的 jar包
  • 子项目 要继承 父项目依赖 时, 需要 显式的声明 需要用的 依赖,并且不指定 version,才会从 父项目 中继承该 依赖,这时 versionscope 都读取自 父pom
<dependencyManagement>
    <dependencies>
    
        <!-- springCloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

		<!-- 更多依赖... -->
	
    </dependencies>
</dependencyManagement>

二、配置文件样例

父项目文件

pom.xml

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

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

    <groupId>com.tencent.wechat</groupId>
    <artifactId>tencent-wechat</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>tencent-wechat-user</module>
        <module>tencent-wechat-blog</module>

		<!-- 公有模块 -->
        <module>tencent-wechat-common</module>
    </modules>

    <!-- 版本锁定 -->
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
        <cloud-alibabba.version>2.2.5.RELEASE</cloud-alibabba.version>
        <mysql.version>8.0.26</mysql.version>
        <mybatis-plus.version>3.5.2</mybatis-plus.version>
        <druid.version>1.2.14</druid.version>
        <kaptcha.version>1.2.1</kaptcha.version>
        <fastjson.version>1.2.71</fastjson.version>
        <httpclientutil.version>1.0.4</httpclientutil.version>
        <commons-lang.version>2.6</commons-lang.version>
        <commons-collections.version>3.2.2</commons-collections.version>
        <commons-io.version>2.6</commons-io.version>
    </properties>

    <dependencyManagement>
        <dependencies>

            <!-- springCloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--nacos的管理依赖-->
<!--            <dependency>-->
<!--                <groupId>com.alibaba.cloud</groupId>-->
<!--                <artifactId>spring-cloud-alibaba-dependencies</artifactId>-->
<!--                <version>${cloud-alibabba.version}</version>-->
<!--                <type>pom</type>-->
<!--                <scope>import</scope>-->
<!--            </dependency>-->

            <!-- mysql驱动 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>

            <!--mybatis-plus-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus</artifactId>
                <version>${mybatis-plus.version}</version>
            </dependency>

            <!--链接池-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>${druid.version}</version>
            </dependency>

            <!--swagger-->
<!--            <dependency>-->
<!--                <groupId>com.spring4all</groupId>-->
<!--                <artifactId>swagger-spring-boot-starter</artifactId>-->
<!--                <version>2.0.2.RELEASE</version>-->
<!--            </dependency>-->

            <!--图片验证码-->
            <dependency>
                <groupId>com.github.penggle</groupId>
                <artifactId>kaptcha</artifactId>
                <version>${kaptcha.version}</version>
            </dependency>

            <!--存储头像-->
            <dependency>
                <groupId>com.aliyun.oss</groupId>
                <artifactId>aliyun-sdk-oss</artifactId>
                <version>3.8.0</version>
            </dependency>

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <!--http请求工具-->
            <dependency>
                <groupId>com.arronlong</groupId>
                <artifactId>httpclientutil</artifactId>
                <version>${httpclientutil.version}</version>
            </dependency>

            <!--工具类依赖-->
            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>${commons-lang.version}</version>
            </dependency>
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>${commons-collections.version}</version>
            </dependency>
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons-io.version}</version>
            </dependency>

            <!--简化bean代码的工具包-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
                <version>1.18.20</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.2</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-jdk14</artifactId>
                <version>1.5.6</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

</project>

业务子模块文件

pom.xml

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

    <parent>
        <groupId>com.tencent.wechat</groupId>
        <artifactId>tencent-wechat</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>tencent-wechat-user</artifactId>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- 引入公共模块 -->
        <dependency>
            <groupId>com.tencent.wechat</groupId>
            <artifactId>tencent-wechat-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!-- nacos 客户端 作为 注册与发现-->
<!--        <dependency>-->
<!--            <groupId>com.alibaba.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>-->
<!--        </dependency>-->

        <!-- nacos 配置中心 -->
<!--        <dependency>-->
<!--            <groupId>com.alibaba.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>-->
<!--        </dependency>-->

        <!-- Spring Security、OAuth2 和JWT等-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-oauth2</artifactId>-->
<!--        </dependency>-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--热部署 ctrl+f9-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-core</artifactId>
            <version>3.5.2</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- swagger-->
<!--        <dependency>-->
<!--            <groupId>com.spring4all</groupId>-->
<!--            <artifactId>swagger-spring-boot-starter</artifactId>-->
<!--            <version>1.9.1.RELEASE</version>-->
<!--        </dependency>-->

        <dependency>
            <groupId>com.github.yulichang</groupId>
            <artifactId>mybatis-plus-join</artifactId>
            <version>1.3.8</version>
        </dependency>

    </dependencies>

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

</project>

application.yml

server:
  port: 8190
  servlet:
    context-path: /user #上下文请求路径,请求前缀 ip:port/user
spring:
  application:
    name: user-server #应用名
    #数据库配置
  datasource:
    url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&autoReconnect=true&nullCatalogMeansCurrent=true&allowMultiQueries=true
    username: xxx
    password: xxx
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
  type-aliases-package: com.tencent.wechat.user.entities
  mapper-locations: classpath*:com/tencent/wechat/user/mapper/**/*.xml
  configuration:
    map-underscore-to-camel-case: false
logging:
  level:
    com.pkpmgl.pt.deptuser.mapper: debug

mybatis-plus 的 generator 模块

CodeGenerator.java

public class CodeGenerator {
    //工程
    private static String PROJECT_NNAME ="tencent-wechat-user";
//    private static String PROJECT_NNAME ="tencent-wechat-blog";

    //子包名
    private static String MODULE_NAME = "user";
//    private static String MODULE_NAME = "blog";

    //数据库名称
    private static String DATABASE_NAME ="xxx";

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + ":");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw  new MybatisPlusException("请输入正确的" + tip + "!");
    }

    public static void main(String[] args) {
        //代码生成器
        AutoGenerator mpg = new AutoGenerator();
        //数据库配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://xxx.xxx.xxx.68:xxx/"+DATABASE_NAME+"?useUnicode=ture&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("xxx");
        dsc.setPassword("xxx");
        mpg.setDataSource(dsc);
        //全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir")+"/";
        gc.setOutputDir(projectPath + PROJECT_NNAME +"/src/main/java");
        gc.setIdType(IdType.ASSIGN_ID); //分布式id
        gc.setAuthor("xqzhao");
        gc.setFileOverride(true);
        gc.setOpen(false);
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true);
        mpg.setGlobalConfig(gc);

        //包全局
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.tencent.wechat");
        pc.setController(MODULE_NAME+".controller");
        pc.setService(MODULE_NAME+".service");
        pc.setServiceImpl(MODULE_NAME+".service.impl");
        pc.setMapper(MODULE_NAME+".mapper");
        pc.setXml(MODULE_NAME+".mapper.xml");
        pc.setEntity(MODULE_NAME+".entities");
        mpg.setPackageInfo(pc);

        //策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setEntitySerialVersionUID(true);
        strategy.setRestControllerStyle(true);
        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        //strategy.setTablePrefix("T_E_App_");//去掉前缀
        mpg.setStrategy(strategy);

        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
    
}

common 公用模块

公用模块包含的 依赖: (一般引入 依赖的启动器)

  • spring-boot-starter-web
  • mysql-connector-java
  • mybatis-plus-boot-starter
  • druid-spring-boot-starter
  • mybatis-plus-join
  • mybatis-plus-join-core

pom.xml

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

    <parent>
        <groupId>com.tencent.wechat</groupId>
        <artifactId>tencent-wechat</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>tencent-wechat-common</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

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

        <!-- feign 调用服务接口 -->
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-openfeign</artifactId>-->
<!--        </dependency>-->

        <!-- Spring Security、OAuth2 和JWT等-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-oauth2</artifactId>-->
<!--        </dependency>-->

        <!--redis-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-data-redis</artifactId>-->
<!--        </dependency>-->

        <!--    springboot 2.3.x版本以上将validation单独抽取出来了,要我们自己引入-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <!--mybatis-plus启动器-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>

        <!--Druid连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- 配置处理器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <!--lombok setter, getter-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- swagger-->
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.9.1.RELEASE</version>
        </dependency>

        <!-- aliyun -->
        <!-- aliyun oss-->
<!--        <dependency>-->
<!--            <groupId>com.aliyun.oss</groupId>-->
<!--            <artifactId>aliyun-sdk-oss</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>

        <!--http请求工具-->
        <dependency>
            <groupId>com.arronlong</groupId>
            <artifactId>httpclientutil</artifactId>
        </dependency>

        <!-- 工具类依赖 -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.yulichang</groupId>
            <artifactId>mybatis-plus-join-core</artifactId>
            <version>1.3.8</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>com.github.yulichang</groupId>
            <artifactId>mybatis-plus-join</artifactId>
            <version>1.3.8</version>
        </dependency>

    </dependencies>

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

</project>

三、项目源码
点击前往 Gitee 查看项目源码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

页川叶川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值