基于Mina的配置中心(一)
Mina 是 Apache 开源的一个 NIO 框架。还有一个 NIO 框架是 Netty。 像 Dubbo,RocketMQ,Nacos 都是基于Netty开发的,所以用 Mina 其实也可以开发。
因为之前想看 RocketMQ,Nacos 的源码,看了一下Netty,结果Netty看了一点,RocketMQ,Nacos 的源码也看了一点...尴尬啊。
好了,现在开始,开发一个基于 Mina 的配置中心。
感觉一篇写不完,所以就搞了一个系列,我也不知道最后会有多少。
技术架构
先说一下技术架构。
- SpringBoot @2.2.6.RELEASE
- Mybatis-Plus @3.0.4
- Mina @2.1.3
虽然主要是Mina,当中也包含了很多SpringBoot相关的知识。
整个配置中心包括三个部分:
- Server 服务端,包括用户登录,配置管理等等
- Client 客户端,主要是作为依赖给别人引入的
- Base 基础服务,服务端和客户端有大量重复的代码,比如消息体,编码协议,通用的工具类,抽取出来,客户端和服务端都可以使用。
接下来正式开始:
先编写Server端,创建一个Maven项目。

把红色的全删掉,这是一个多模块项目,最外面的只是一个空的父项目。

添加两个模块,一个Server,一个Base

这时选择 Maven ,GroupId就会默认填好,不需要再输入了。

最后结构是这样的(不过选择Maven不会自动创建包和启动类...)

接下来就是编辑 pom.xml 添加依赖了。
父项目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>
<packaging>pom</packaging>
<modules>
<module>mina-server</module>
<module>mina-base</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lww</groupId>
<artifactId>mina-config</artifactId>
<version>1.0.0</version>
<name>mina-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!--升级版本号只用修改这里-->
<config.version>1.0.0</config.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
<!-- mina核心依赖 https://mvnrepository.com/artifact/org.apache.mina/mina-core -->
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>
<version>2.1.3</version>
</dependency>
<!-- 为了进行socket通信,还需要加入另一个依赖: https://mvnrepository.com/artifact/org.apache.mina/mina-integration-beans -->
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-integration-beans</artifactId>
<version>2.1.3</version>
<exclusions>
<exclusion>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-integration-spring</artifactId>
<version>1.1.7</version>
<exclusions>
<exclusion>
<artifactId>mina-core</artifactId>
<groupId>org.apache.mina</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
<exclusions>
<exclusion>
<artifactId>swagger-annotations</artifactId>
<groupId>io.swagger</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--此时pom文件会报错Missing artifact org.apache.mina:mina-core:bundle:2.1.3 需要添加如下插件:-->
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
放在父项目的pom.xml中,全局都可以用,Mybatis-Plus和Swagger放在这里,是因为,消息体(Domain)要放在Base模块里,不放又不行,因为Client也要用这个类。
Server 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">
<parent>
<artifactId>mina-config</artifactId>
<groupId>com.lww</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mina-server</artifactId>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.13</version>
</dependency>
<dependency>
<groupId>com.lww</groupId>
<artifactId>mina-base</artifactId>
<version>${config.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
</project>
Base 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">
<parent>
<artifactId>mina-config</artifactId>
<groupId>com.lww</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mina-base</artifactId>
<version>${config.version}</version>
</project>
一些基本的配置类

SpringBeanFactoryUtils 这个类主要从ApplicationContext容器中获取对象,按理说,Client也会用到,
但是没有放到Base里,因为如果放到Base里,需要配置扫描路径,还要创建一个/META-INF/spring.factories来配置,否则是无法获取到容器的。
只为了一个工具类,有点太不划算了。
不过在Client中,会配置这些东西,而且把SpringBeanFactoryUtils 配置了一下,因为Client是作为第三方包被引入的,如果不配置,是无法生效的。
第一章先到这里。 准备工作已经完成了。第二章会创建消息表,然后整体设计配置中心,继续撸代码。敬请期待!
项目源码
欢迎大家关注我的公众号,共同学习,一起进步。

本文使用 mdnice 排版