基于Mina的配置中心(一)

基于Mina的配置中心(一)

Mina 是 Apache 开源的一个 NIO 框架。还有一个 NIO 框架是 Netty。 像 Dubbo,RocketMQ,Nacos 都是基于Netty开发的,因此用 Mina 其实也能够开发。java

由于以前想看 RocketMQ,Nacos 的源码,看了一下Netty,结果Netty看了一点,RocketMQ,Nacos 的源码也看了一点...尴尬啊。mysql

好了,如今开始,开发一个基于 Mina 的配置中心。git

感受一篇写不完,因此就搞了一个系列,我也不知道最后会有多少。github

技术架构

先说一下技术架构。web

  1. SpringBoot @2.2.6.RELEASE
  2. Mybatis-Plus @3.0.4
  3. Mina @2.1.3

虽然主要是Mina,当中也包含了不少SpringBoot相关的知识。redis

整个配置中心包括三个部分:spring

  1. Server 服务端,包括用户登陆,配置管理等等
  2. Client 客户端,主要是做为依赖给别人引入的
  3. Base 基础服务,服务端和客户端有大量重复的代码,好比消息体,编码协议,通用的工具类,抽取出来,客户端和服务端均可以使用。

接下来正式开始:

先编写Server端,建立一个Maven项目。sql

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

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

这时选择 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 排版

相关文章
相关标签/搜索