基于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项目。
![9deaf0539dfb1451ce7e99eafcf42d82.png](https://i-blog.csdnimg.cn/blog_migrate/5c28baa52e65480dbe21e31151e3280f.jpeg)
把红色的全删掉,这是一个多模块项目,最外面的只是一个空的父项目。
![4c792074eb0f1406f64e979ffc5679e8.png](https://i-blog.csdnimg.cn/blog_migrate/f69fbf1b754f0d9aa7170326e056e937.jpeg)
添加两个模块,一个Server,一个Base
![a5e7d200ecf49bf9b325156884fb1bde.png](https://i-blog.csdnimg.cn/blog_migrate/35780201f8e59f71be910e44f1fc4794.jpeg)
这时选择 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.0modelVersion>
<packaging>pompackaging>
<modules>
<module>mina-servermodule>
<module>mina-basemodule>
modules>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.6.RELEASEversion>
<relativePath/>
parent>
<groupId>com.lwwgroupId>
<artifactId>mina-configartifactId>
<version>1.0.0version>
<name>mina-configname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<config.version>1.0.0config.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.0.4version>
dependency>
<dependency>
<groupId>org.apache.minagroupId>
<artifactId>mina-coreartifactId>
<version>2.1.3version>
dependency>
<dependency>
<groupId>org.apache.minagroupId>
<artifactId>mina-integration-beansartifactId>
<version>2.1.3version>
<exclusions>
<exclusion>
<groupId>org.apache.minagroupId>
<artifactId>mina-coreartifactId>
exclusion>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.minagroupId>
<artifactId>mina-integration-springartifactId>
<version>1.1.7version>
<exclusions>
<exclusion>
<artifactId>mina-coreartifactId>
<groupId>org.apache.minagroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.9.2version>
<exclusions>
<exclusion>
<groupId>io.swaggergroupId>
<artifactId>swagger-modelsartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>io.swaggergroupId>
<artifactId>swagger-modelsartifactId>
<version>1.5.21version>
<exclusions>
<exclusion>
<artifactId>swagger-annotationsartifactId>
<groupId>io.swaggergroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.9.2version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.62version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.2version>
<configuration>
<source>1.8source>
<target>1.8target>
<encoding>UTF-8encoding>
configuration>
plugin>
<plugin>
<groupId>org.apache.felixgroupId>
<artifactId>maven-bundle-pluginartifactId>
<extensions>trueextensions>
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-configartifactId>
<groupId>com.lwwgroupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>mina-serverartifactId>
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.18version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.13version>
dependency>
<dependency>
<groupId>com.lwwgroupId>
<artifactId>mina-baseartifactId>
<version>${config.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-aopartifactId>
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-configartifactId>
<groupId>com.lwwgroupId>
<version>1.0.0version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>mina-baseartifactId>
<version>${config.version}version>
project>
一些基本的配置类
![305722dad45421d06cb1c27a180957ba.png](https://i-blog.csdnimg.cn/blog_migrate/401fef264273588ddbb23b405e624ed3.jpeg)
SpringBeanFactoryUtils
这个类主要从ApplicationContext
容器中获取对象,按理说,Client也会用到, 但是没有放到Base里,因为如果放到Base里,需要配置扫描路径,还要创建一个/META-INF/spring.factories来配置,否则是无法获取到容器的。只为了一个工具类,有点太不划算了。
不过在Client中,会配置这些东西,而且把SpringBeanFactoryUtils
配置了一下,因为Client是作为第三方包被引入的,如果不配置,是无法生效的。
第一章先到这里。准备工作已经完成了。第二章会创建消息表,整体设计配置中心,然后继续撸代码。敬请期待!
项目源码
https://github.com/LerDer/mina-config
欢迎大家关注我的公众号,共同学习,一起进步。
![b7bd41d12946c4a58001908bc4741abc.png](https://i-blog.csdnimg.cn/blog_migrate/db85b481a7865b588ff3462782883816.jpeg)