前言
项目基于springcloud alibaba+springboot+springcloud目前市场上较流行的框架组合,三类框架需要做版本适配,不然存在调试起来会出现诸多不兼容问题。(版本问题请参考版本说明 · alibaba/spring-cloud-alibaba Wiki · GitHub)我这里选用的版本如下:
SpringBoot:2.5.14
SpringCloud:2020.0.5
SpringCloud Alibaba:2021.1
Java:11.0.12
IDE: Intelli IDEA
创建公共依赖eggs-boot-dependencies
该项目实现对常用依赖版本的统一管理,是其他项目的公共父依赖。
项目目录结构:
文件内容:
<?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.5.14</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xunqiu</groupId>
<artifactId>eggs-boot-dependencies</artifactId>
<version>23.10.26-SNAPSHOT</version>
<name>eggs-boot-dependencies</name>
<description>eggs-boot-dependencies</description>
<packaging>pom</packaging>
<properties>
<java.version>11</java.version>
<spring-cloud-dependencies.version>2020.0.5</spring-cloud-dependencies.version>
<com.alibaba.cloud.version>2021.1</com.alibaba.cloud.version>
<xunqiu.lombok>1.18.26</xunqiu.lombok>
<xunqiu.commons-lang3>3.11</xunqiu.commons-lang3>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${com.alibaba.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--lombok⼯具-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${xunqiu.lombok}</version>
<scope>provided</scope>
</dependency>
<!-- StringUtils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${xunqiu.commons-lang3}</version>
</dependency>
<!--测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
</project>
将该项目安装到本地marven仓库:
mvn clean install
创建SpringCloud项目egg-service
该项目可以实现需求中比较专业的,细化的需求。给项目是父子模块架构,parent主要提供公共依赖版本配置,api子模块定义对外服务接口统一规范,也是微服务调用统计规范。module子模块是所有api接口实现模块,承载所有主要业务逻辑开发。jar模块主要包含微服的入口函数,也是承担服务打包任务的模块。(注意各子模块的包名要统一,不然会有很多坑让你应接不暇)
代码目录如下,下面一一列举个细节配置与代码
父模块POM配置内容
<?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.xunqiu</groupId>
<artifactId>eggs-boot-dependencies</artifactId>
<version>23.10.26-SNAPSHOT</version>
</parent>
<groupId>com.xunqiu</groupId>
<artifactId>eggs-service-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<profiles>
<profile>
<properties>
<os.detected.classifier>linux-x86_64</os.detected.classifier>
</properties>
<id>prod</id>
<modules>
<module>eggs-service-api</module>
<module>eggs-service-jar</module>
<module>eggs-service-module</module>
</modules>
</profile>
<profile>
<properties>
<os.detected.classifier>osx-x86_64</os.detected.classifier>
</properties>
<id>test</id>
<modules>
<module>eggs-service-api</module>
<module>eggs-service-jar</module>
<module>eggs-service-module</module>
</modules>
<!-- 默认激活fat jar -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
<build>
<plugins>
<!--编译插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<!--编译跳过测试文件检查的生命周期-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
子模块eggs-service-api中POM配置
<?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.xunqiu</groupId>
<artifactId>eggs-service-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.xunqiu</groupId>
<artifactId>eggs-service-api</artifactId>
<properties>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
子模块eggs-service-module中POM配置
<?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.xunqiu</groupId>
<artifactId>eggs-service-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.xunqiu</groupId>
<artifactId>eggs-service-module</artifactId>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>com.xunqiu</groupId>
<artifactId>eggs-service-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- StringUtils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
</dependencies>
</project>
子模块eggs-service-jar中POM配置
<?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.xunqiu</groupId>
<artifactId>eggs-service-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>eggs-service-jar</artifactId>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>com.xunqiu</groupId>
<artifactId>eggs-service-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>xunqiu-eggs-service</finalName>
</build>
</project>
eggs-service-api模块定义接口规范DubboServiceApi
package com.xunqiu.eggs.api; /** * @Author: XiaoMing.Hu * Date: 2023-10-27 18:31 * Description: */ public interface DubboServiceApi { /** * Test dubbo api * @return */ String hello(); }
代码目录结构:
eggs-service-module实现DubboServiceApi
package com.xunqiu.eggs.controller; import com.xunqiu.eggs.api.DubboServiceApi; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; /** * @Author: XiaoMing.Hu * Date: 2023-10-27 18:34 * Description: */ @RequestMapping(path = "/open/v1/dubboService") public class DubboServiceImp implements DubboServiceApi { @Override @GetMapping("/hello") public String hello() { String say = "Hello word!"; System.out.println(say); return say; } }
入口函数中的XunQiuEggsConfiguration.class定义在module模块下
package com.xunqiu.eggs.config; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Configuration; /** * @Author: XiaoMing.Hu * Date: 2023-10-24 11:32 * Description: */ @Configuration @EnableDiscoveryClient public class XunQiuEggsConfiguration { }
resources文件夹下文件application.yaml内容
server: port: 8083
目录结构为:
eggs-service-jar模块是入口main代码
package com.xunqiu.eggs; import com.xunqiu.eggs.config.XunQiuEggsConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Import; /** * @author xiaominghu */ @SpringBootApplication @Import({XunQiuEggsConfiguration.class}) public class EggsJarApplication { public static void main(String[] args) { SpringApplication.run(EggsJarApplication.class, args); System.out.println("EggsJarApplication =====> started success"); } }
目录结构为:
模块之间引用关系api---->module---->jar(一次前者被后者引用),纤细信息请看某块中的具体内容。在这里我说下我自己遇到过的一个坑:开始我把打包插件
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
放在父POM文件中导致所有子模块继承改插件,全部打可执行jar包会覆盖普通jar包。这样会出现一个问题--依赖找不到。所建议将这个插件只放在jar模块的POM文件内即可,因为只有jar模块有入口函数需要打出可执行的jar包。
总结
这里都是验证后的配置,后面的系列笔记都是基于这个模版进行记录,希望能给起步实操SpringCloud的同学一点帮助。