maven netty 配置_基于Mina的配置中心(一)

基于Mina的配置中心(一)

Mina 是 Apache 开源的一个 NIO 框架。还有一个 NIO 框架是 Netty。像 Dubbo,RocketMQ,Nacos 都是基于Netty开发的,所以用 Mina 其实也可以开发。

因为之前想看 RocketMQ,Nacos 的源码,看了一下Netty,结果Netty看了一点,RocketMQ,Nacos 的源码也看了一点...尴尬啊。

好了,现在开始,开发一个基于 Mina 的配置中心。

感觉一篇写不完,所以就搞了一个系列,我也不知道最后会有多少。

技术架构

先说一下技术架构。

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

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

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

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

接下来正式开始:

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

9deaf0539dfb1451ce7e99eafcf42d82.png

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

4c792074eb0f1406f64e979ffc5679e8.png

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

a5e7d200ecf49bf9b325156884fb1bde.png

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

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

接下来就是编辑 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

SpringBeanFactoryUtils 这个类主要从ApplicationContext容器中获取对象,按理说,Client也会用到, 但是没有放到Base里,因为如果放到Base里,需要配置扫描路径,还要创建一个/META-INF/spring.factories来配置,否则是无法获取到容器的。只为了一个工具类,有点太不划算了。

不过在Client中,会配置这些东西,而且把SpringBeanFactoryUtils 配置了一下,因为Client是作为第三方包被引入的,如果不配置,是无法生效的。

第一章先到这里。准备工作已经完成了。第二章会创建消息表,整体设计配置中心,然后继续撸代码。敬请期待!
项目源码

https://github.com/LerDer/mina-config

欢迎大家关注我的公众号,共同学习,一起进步。

b7bd41d12946c4a58001908bc4741abc.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值