1,需求
工程底座,包括软件工程目录结构和通用组件、工具类等。
所有微服务项目基于工程底座进行开发。
2,分析
3,环境准备
JDK >= 1.8 (推荐1.8版本)
Maven >= 3.0
Mysql >= 5.7.0 (推荐5.7版本)
Redis >= 3.0
rabbitMQ>=3.9.11
nacos >= 2.x.x版本
4,执行sql
4.1,执行注册中心sql
\tedu-charging-cloud\sql\tedu_config.sql
4.2,执行权限sql
\tedu-charging-cloud\sql\tedu_cloud.sql
5,创建主结构
5.1,创建maven项目 tedu-charging-cloud
<?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>com.tedu.charging</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
5.2,创建微服务组件文件夹tedu-component
5.3,创建maven项目 tedu-common
5.4,创建maven项目 tedu-charging-service
6搭建微服务组件
6.1,创建注册中心nacos
6.1.1,在tedu-component文件夹中创建文件夹tedu-register-server
6.1.2,拷贝nacos
6.1.3,修改\tedu-charging-cloud\tedu-component\tedu-register-server\nacos-server-2.2.3\conf\application.properties中的数据库ip地址,密码
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/tedu_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=root
6.1.4,dos下进入\tedu-charging-cloud\tedu-component\tedu-register-server\nacos-server-2.2.3\bin文件夹中,执行如下命令
startup.cmd -m standalone
显示以下表示nacos启动成功
"nacos is starting with standalone"
,--.
,--.'|
,--,: : | Nacos 2.2.3
,`--.'`| ' : ,---. Running in stand alone mode, All function modules
| : : | | ' ,'\ .--.--. Port: 8848
: | \ | : ,--.--. ,---. / / | / / ' Pid: 1348
| : ' '; | / \ / \. ; ,. :| : /`./ Console: http://192.168.66.102:8848/nacos/index.html
' ' ;. ;.--. .-. | / / '' | |: :| : ;_
| | | \ | \__\/: . .. ' / ' | .; : \ \ `. https://nacos.io
' : | ; .' ," .--.; |' ; :__| : | `----. \
| | '`--' / / ,. |' | '.'|\ \ / / /`--' /
' : | ; : .' \ : : `----' '--'. /
; |.' | , .-./\ \ / `--'---'
'---' `--`---' `----'
2023-05-31 09:34:00,733 INFO Tomcat initialized with port(s): 8848 (http)
2023-05-31 09:34:11,345 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'
6.1.5,在浏览器中输入http://localhost:8848/nacos,显示如下图
6.2,创建网关 tedu-gateway-server
6.2.1,创建module
6.2.2,修改 cloud的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tedu.charging</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>tedu-common</module>
<module>tedu-charging-service</module>
</modules>
<properties>
<teud.cloud.version>1.0-SNAPSHOT</teud.cloud.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-boot.version>2.7.7</spring-boot.version>
<spring-cloud.version>2021.0.5</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
<spring-boot-admin.version>2.7.10</spring-boot-admin.version>
<swagger.fox.version>3.0.0</swagger.fox.version>
<swagger.core.version>1.6.2</swagger.core.version>
<tobato.version>1.27.2</tobato.version>
<kaptcha.version>2.3.3</kaptcha.version>
<pagehelper.boot.version>1.4.6</pagehelper.boot.version>
<druid.version>1.2.16</druid.version>
<dynamic-ds.version>3.5.2</dynamic-ds.version>
<commons.io.version>2.11.0</commons.io.version>
<velocity.version>2.3</velocity.version>
<fastjson.version>2.0.25</fastjson.version>
<jjwt.version>0.9.1</jjwt.version>
<minio.version>8.2.2</minio.version>
<poi.version>4.1.2</poi.version>
<transmittable-thread-local.version>2.14.2</transmittable-thread-local.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>
<!-- SpringCloud Alibaba 微服务 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringBoot 依赖配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- FastDFS 分布式文件系统 -->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>${tobato.version}</version>
</dependency>
<!-- Swagger 依赖配置 -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>${swagger.core.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger.core.version}</version>
</dependency>
<!-- 验证码 -->
<dependency>
<groupId>pro.fessional</groupId>
<artifactId>kaptcha</artifactId>
<version>${kaptcha.version}</version>
</dependency>
<!-- pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.boot.version}</version>
</dependency>
<!-- io常用工具类 -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
</dependency>
<!-- excel工具 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<!-- 代码生成使用模板 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${velocity.version}</version>
</dependency>
<!-- JSON 解析器和生成器 -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- JWT -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jjwt.version}</version>
</dependency>
<!-- 线程传递值 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>${transmittable-thread-local.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- bootstrap 启动器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>public</id>
<name>aliyun nexus</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>aliyun nexus</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
6.2.3,修改 gateway的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.tedu.charging</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tedu.charging</groupId>
<artifactId>gatewayserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tedu-gateway-server</name>
<description>tedu-gateway-server</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<!-- SpringCloud Gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- SpringCloud Alibaba Sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- SpringCloud Alibaba Sentinel Gateway -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<!-- Sentinel Datasource Nacos -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!-- SpringBoot Actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- SpringCloud Loadbalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<!--验证码 -->
<dependency>
<groupId>pro.fessional</groupId>
<artifactId>kaptcha</artifactId>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.fox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.fox.version}</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
6.2.5,gateway中添加bootstrap.yml
# Tomcat
server:
port: 8080
# Spring
spring:
application:
# 应用名称
name: tedu-gateway
profiles:
# 环境配置
active: dev
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: 127.0.0.1:8848
config:
# 配置中心地址
server-addr: 127.0.0.1:8848
# 配置文件格式
file-extension: yml
# 共享配置
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
6.2.6,启动gateway后,nacos服务列表显示网关
6.3,搭建监控
6.3.1,创建module
6.3.2,修改monitor中的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.tedu.charging</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tedu.charging</groupId>
<artifactId>monitorserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tedu-monitor-server</name>
<description>tedu-monitor-server</description>
<dependencies>
<!-- SpringBoot Admin -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<!-- SpringCloud Alibaba Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- SpringCloud Alibaba Sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- SpringBoot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
6.3.3,monitor中添加bootstrap.yml
# Tomcat
server:
port: 9100
# Spring
spring:
application:
# 应用名称
name: tedu-monitor
profiles:
# 环境配置
active: dev
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: 127.0.0.1:8848
config:
# 配置中心地址
server-addr: 127.0.0.1:8848
# 配置文件格式
file-extension: yml
# 共享配置
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
6.3.4,启动类加@EnableAdminServer注解
@SpringBootApplication
@EnableAdminServer
public class TeduMonitorServerApplication {
public static void main(String[] args) {
SpringApplication.run(TeduMonitorServerApplication.class, args);
}
}
6.3.5,启动后,nacos服务列表中显示monitor
6.3.6,查看监控信息
http://localhost:9100/
用户名是tedu
密码是123456
登录成功后显示如下界面
7,创建微服务
7.1,创建module
7.2,修改tedu-charging-service中的pom.xml
<packaging>pom</packaging>
<modules>
<module>service-sample</module>
</modules>
7.3,修改service-sample中的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.tedu.charging</groupId>
<artifactId>tedu-charging-service</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.tedu.charging.service</groupId>
<artifactId>servicesample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-sample</name>
<description>service-sample</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- SpringBoot Admin -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>${spring-boot-admin.version}</version>
</dependency>
<!-- SpringCloud Alibaba Nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- SpringCloud Alibaba Sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- SpringBoot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
7.4,bootstrap.yml中内容如下
# Tomcat
server:
port: 10001
# Spring
spring:
application:
# 应用名称
name: servicesample
profiles:
# 环境配置
active: dev
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: 127.0.0.1:8848
config:
# 配置中心地址
server-addr: 127.0.0.1:8848
# 配置文件格式
file-extension: yml
# 共享配置
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
7.5,启动微服务后,在nacos中会出现微服务
8,创建公共代码包
8.1,创建common-core
8.1.1,创建module
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.tedu.charging</groupId>
<artifactId>tedu-common</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-core</artifactId>
<version>1.0-SNAPSHOT</version>
<name>tedu-common-core</name>
<description>tedu-common-core</description>
<dependencies>
<!-- SpringCloud Openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- SpringCloud Loadbalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!-- Spring Context Support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- Transmittable ThreadLocal -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
</dependency>
<!-- Pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
<!-- Hibernate Validator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- Alibaba Fastjson -->
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
</dependency>
<!-- Jwt -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
</dependency>
<!-- Jaxb -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<!-- Apache Lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- Commons Io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<!-- excel工具 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
<!-- Java Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
8.1.2,实现核心公共类
8.1.3,tedu-common下的pom.xml添加module
<modules>
<module>tedu-common-core</module>
</modules>
8.1.4,cloud中的pom.xml dependencyManager中添加依赖
<dependency>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-core</artifactId>
<version>${tedu.cloud.version}</version>
</dependency>
8.1.5,微服务中添加依赖并测试
service-sample pom.xml中添加依赖
<dependency>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-core</artifactId>
</dependency>
在service-sample中添加测试类
/**
* 测试common-core中的工具类
*/
public class CommonCoreTests {
@Test
public void testDateUtils(){
String time = DateUtils.getTime();
System.out.println(time);
}
}
8.2,创建common-redis
8.2.1,创建module
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.tedu.charging</groupId>
<artifactId>tedu-common</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-redis</artifactId>
<version>1.0-SNAPSHOT</version>
<name>tedu-common-redis</name>
<description>tedu-common-redis</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.5.1</version>
</dependency>
</dependencies>
</project>
8.2.2,实现核心公共类
8.2.3,tedu-common下的pom.xml添加module
<modules>
<module>tedu-common-core</module>
<module>tedu-common-redis</module>
</modules>
8.2.4,cloud中的pom.xml dependencyManager中添加依赖
<dependency>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-redis</artifactId>
<version>${tedu.cloud.version}</version>
</dependency>
8.2.5,微服务中添加依赖并测试
nacos配置中心中在servicesample-dev.yml中添加如下内容
spring:
redis:
host: 127.0.0.1
port: 6379
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:13306/tedu_cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: root
management:
endpoints:
web:
exposure:
include: '*'
微服务启动类中添加包扫描
@SpringBootApplication
@ComponentScan(basePackages = "com.tedu.charging.*")
public class ServiceSampleApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceSampleApplication.class, args);
}
}
测试代码如下
package com.tedu.charging.service.servicesample;
import org.junit.jupiter.api.Test;
import com.tedu.charging.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
/**
* 测试common-redis中的工具类
*/
@SpringBootTest
public class CommonRedisTests {
@Autowired
RedisService redisService;
@Test
public void saveTest() {
redisService.setCacheObject("test3", "ok3");
}
@Test
public void getTest() {
Object test3 = redisService.getCacheObject("test3");
System.out.println("test3="+test3);
}
}
8.3,创建tedu-common-rabbitmq
8.3.1,创建module
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.tedu.charging</groupId>
<artifactId>tedu-common</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-redis</artifactId>
<version>1.0-SNAPSHOT</version>
<name>tedu-common-redis</name>
<description>tedu-common-redis</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.5.1</version>
</dependency>
</dependencies>
</project>
8.3.2,实现核心公共类
8.3.3,tedu-common下的pom.xml添加module
<modules>
<module>tedu-common-core</module>
<module>tedu-common-redis</module>
<module>tedu-common-mq</module>
</modules>
8.3.4,cloud中的pom.xml dependencyManager中添加依赖
<dependency>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-mq</artifactId>
<version>${tedu.cloud.version}</version>
</dependency>
8.3.5,微服务中添加依赖并测试
配置中心中添加rabbitmq配置信息
spring:
rabbitmq:
host: 192.168.64.140
port: 5672
username: guest
password: guest
virtual-host: /
connection-timeout: 15000
添加依赖
<dependency>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-mq</artifactId>
</dependency>
在src/main/java中添加配置类
@Configuration
public class RabbitMQConfig {
public static final String EXCHANGE_NAME="myExchange3";
public static final String QUEUE_NAME="myQueue3";
public static final String ROUTING_KEY="myRoutingKey3";
@Bean
public Queue queue() {
return new Queue(QUEUE_NAME);
}
@Bean
public DirectExchange exchange() {
return new DirectExchange(EXCHANGE_NAME);
}
@Bean
public Binding binding(Queue queue, DirectExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
}
}
在src/test中添加测试类
/**
* 测试common-mq中的工具类
*/
@SpringBootTest
public class CommonMQTests {
@Autowired
RabbitmqProducerAdapter rabbitmqProducerAdapter;
@Test
public void producerTest() {
rabbitmqProducerAdapter.sendMessage(RabbitMQConfig.EXCHANGE_NAME,RabbitMQConfig.ROUTING_KEY,"testmq");
}
}
在微服务中添加消息消费者
import com.tedu.charging.common.mq.rabbitmq.RabbitmqConsumerAdapter;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class ConsumerTests extends RabbitmqConsumerAdapter {
@Override
@RabbitListener(queues = RabbitMQConfig.QUEUE_NAME,concurrency = "1")
public void consumeMessage(Object data) {
System.out.println("------消息消费者------");
System.out.println(data);
}
}
消费者日志出现在servicesample窗口中
8.4,创建tedu-common-log
8.4.1,创建module
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.tedu.charging</groupId>
<artifactId>tedu-common</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-log</artifactId>
<version>1.0-SNAPSHOT</version>
<name>tedu-common-log</name>
<description>tedu-common-log</description>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
8.4.2,实现核心公共类
8.4.3,tedu-common下的pom.xml添加module
<modules>
<module>tedu-common-core</module>
<module>tedu-common-redis</module>
<module>tedu-common-mq</module>
<module>tedu-common-log</module>
</modules>
8.4.4,cloud中的pom.xml dependencyManager中添加依赖
<dependency>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-log</artifactId>
<version>${tedu.cloud.version}</version>
</dependency>
8.4.5,微服务中添加依赖并测试
<dependency>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-log</artifactId>
</dependency>
创建测试代码
package com.tedu.charging.service.servicesample.controller;
import com.tedu.charging.common.core.web.domain.AjaxResult;
import com.tedu.charging.common.log.annotation.Log;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LogTestController {
@GetMapping("/test")
@Log()
public AjaxResult test() {
return AjaxResult.success("test");
}
@GetMapping("/test2")
public AjaxResult test2() {
return AjaxResult.success("test2");
}
}
8.5,创建 tedu-common-swagger
8.5.1,创建module
<?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.tedu.charging</groupId>
<artifactId>tedu-common</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-swagger</artifactId>
<version>1.0-SNAPSHOT</version>
<name>tedu-common-swagger</name>
<description>tedu-common-swagger</description>
<dependencies>
<!-- SpringBoot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.fox.version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-micro-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.tedu.charging.common.swagger.TeduCommonSwaggerApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
8.5.2,实现核心公共类
8.5.3,tedu-common下的pom.xml添加module
<modules>
<module>tedu-common-core</module>
<module>tedu-common-redis</module>
<module>tedu-common-mq</module>
<module>tedu-common-log</module>
<module>tedu-common-swagger</module>
</modules>
8.5.4,cloud中的pom.xml dependencyManager中添加依赖
<dependency>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-swagger</artifactId>
<version>${tedu.cloud.version}</version>
</dependency>
8.5.5,微服务中添加依赖并测试
<dependency>
<groupId>com.tedu.charging.common</groupId>
<artifactId>tedu-common-swagger</artifactId>
</dependency>