Mina 是 Apache 開源的一個 NIO 框架。還有一個 NIO 框架是 Netty。 像 Dubbo,RocketMQ,Nacos 都是基於Netty開發的,因此用 Mina 其實也能夠開發。java
由於以前想看 RocketMQ,Nacos 的源碼,看了一下Netty,結果Netty看了一點,RocketMQ,Nacos 的源碼也看了一點...尷尬啊。mysql
好了,如今開始,開發一個基於 Mina 的配置中心。git
感受一篇寫不完,因此就搞了一個系列,我也不知道最後會有多少。github
先說一下技術架構。web
雖然主要是Mina,當中也包含了不少SpringBoot相關的知識。redis
整個配置中心包括三個部分:spring
先編寫Server端,建立一個Maven項目。sql
把紅色的全刪掉,這是一個多模塊項目,最外面的只是一個空的父項目。apache
添加兩個模塊,一個Server,一個Basejson
這時選擇 Maven ,GroupId就會默認填好,不須要再輸入了。
最後結構是這樣的(不過選擇Maven不會自動建立包和啓動類...)
接下來就是編輯 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也要用這個類。
<?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> 複製代碼
<?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 排版