1-SOA分布式架构搭建(dubbo)

余生长醉 提交于 2020-03-23 00:08:00

3 月,跳不动了?>>>

1-SOA分布式架构搭建(dubbo)

一、分布式架构的演进

1.单一应用架构

  • 优点:适用于小型网站,小型管理系统,将所有功能都部署到一个功能里,简单易用。
  • 缺点: 1.性能扩展比较难  2.协同开发问题  3.不利于升级维护

2.垂直应用架构

  • 优点:通过切分业务来实现单个模块单独独立部署,降低了维护和部署的难度,团队各司其职比较容易,性能扩展也更方便,更有针对性。
  • 缺点:公用模块无法重复利用,开发性能浪费

3.分布式应用架构

  • 优点:将各个应用通过分层独立出来,可以利用rpc实现web与service、service与service的互相调用,提高了代码的复用性。
  • 缺点:每个调用的模块都要存储一份完整的被调用模块的位置和状态,一旦位置和状态发生变化,就要更新所有涉及的配置

4.面向服务的分布式架构

  **架构概述:**随着架构不断增大,服务节点也越来越多,服务之间的调用和依赖关系也越来越负责,需要一个统一的中心来调度、路由、管理所有的服务基于这个中心构建的这个新型架构就是目前主流的SOA分布式架构。

二、分布式工程的模块搭建

1、分布式模块文件图

  由于查询web应用需要调用爬虫service接口,那么必须在订单的工程中也包含一份service接口。

  如果拷贝一个接口道查询工程中,那么如果以后有更多的模块都调用这个接口呢?每个接口都拷贝一份嘛?

  这种情况就可以利用maven的依赖把这些接口作为公共的包管理起来。同时接口类中的方法也引用了很多的实体bean,那么同样的实体bean类也可以统一管理起来。

2、分布式依赖关系图

3、分布式某块模块搭建

①创建parent模块

  • 新建maven模块

  • 定义模块名、包名

  • parent模块中的 pom.xml

 由于依赖包分布于多个模块中,最好有一个地方能够把所有依赖的版本管理起来。这里便可以使用maven中的<parent>概念。可以让所有的模块都继承这个parent模块,由这个parent模块来管理版本。

<!--?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>

    <parent>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-parent</artifactid>
        <version>1.5.21.RELEASE</version>
        <relativepath /> <!-- lookup parent from repository -->
    </parent>
    <groupid>com.couragehe.souzhi</groupid>
    <artifactid>souzhi-parent</artifactid>
    <version>0.0.1-SNAPSHOT</version>
    <name>souzhi-parent</name>
    <packaging>pom</packaging>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceencoding>UTF-8</project.build.sourceencoding>
        <project.reporting.outputencoding>UTF-8</project.reporting.outputencoding>
        <java.version>1.8</java.version>

        <fastjson.version>1.2.46</fastjson.version>
        <dubbo-starter.version>1.0.10</dubbo-starter.version>
        <dubbo.version>2.6.0</dubbo.version>
        <zkclient.version>0.10</zkclient.version>
        <mybatis.version>1.3.1</mybatis.version>
        <nekohtml.version>1.9.20</nekohtml.version>
        <xml-apis.version>1.4.01</xml-apis.version>
        <batik-ext.version>1.9.1</batik-ext.version>
        <jsoup.version>1.11.2</jsoup.version>
        <httpclient.version>4.5.5</httpclient.version>
        <commons-lang3.version>3.7</commons-lang3.version>
        <mapper-starter.version>1.2.3</mapper-starter.version>
        <jedis.version>2.9.0</jedis.version>
        <jest.version>5.3.3</jest.version>
        <jna.version>4.5.1</jna.version>
        <beanutils.version>1.9.3</beanutils.version>
    </properties>

    <dependencymanagement>
        <dependencies>
            <dependency>
                <groupid>com.alibaba</groupid>
                <artifactid>fastjson</artifactid>
                <version>${fastjson.version}</version>
            </dependency>

            <dependency>
                <groupid>com.alibaba</groupid>
                <artifactid>dubbo</artifactid>
                <version>${dubbo.version}</version>
            </dependency>

            <dependency>
                <groupid>com.101tec</groupid>
                <artifactid>zkclient</artifactid>
                <version>${zkclient.version}</version>
            </dependency>

            <dependency>
                <groupid>com.gitee.reger</groupid>
                <artifactid>spring-boot-starter-dubbo</artifactid>
                <version>${dubbo-starter.version}</version>
            </dependency>

            <dependency>
                <groupid>org.mybatis.spring.boot</groupid>
                <artifactid>mybatis-spring-boot-starter</artifactid>
                <version>${mybatis.version}</version>
            </dependency>


            <dependency>
                <groupid>net.sourceforge.nekohtml</groupid>
                <artifactid>nekohtml</artifactid>
                <version>${nekohtml.version}</version>
            </dependency>

            <dependency>
                <groupid>xml-apis</groupid>
                <artifactid>xml-apis</artifactid>
                <version>${xml-apis.version}</version>
            </dependency>

            <dependency>
                <groupid>org.apache.xmlgraphics</groupid>
                <artifactid>batik-ext</artifactid>
                <version>${batik-ext.version}</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
            <dependency>
                <groupid>org.jsoup</groupid>
                <artifactid>jsoup</artifactid>
                <version>${jsoup.version}</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
            <dependency>
                <groupid>org.apache.httpcomponents</groupid>
                <artifactid>httpclient</artifactid>
                <version>${httpclient.version}</version>
            </dependency>

            <dependency>
                <groupid>org.apache.commons</groupid>
                <artifactid>commons-lang3</artifactid>
                <version>${commons-lang3.version}</version>
            </dependency>


            <dependency>
                <groupid>tk.mybatis</groupid>
                <artifactid>mapper-spring-boot-starter</artifactid>
                <version>${mapper-starter.version}</version>
            </dependency>

            <dependency>
                <groupid>redis.clients</groupid>
                <artifactid>jedis</artifactid>
                <version>${jedis.version}</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
            <dependency>
                <groupid>io.searchbox</groupid>
                <artifactid>jest</artifactid>
                <version>${jest.version}</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
            <dependency>
                <groupid>net.java.dev.jna</groupid>
                <artifactid>jna</artifactid>
                <version>${jna.version}</version>
            </dependency>

            <dependency>
                <groupid>commons-beanutils</groupid>
                <artifactid>commons-beanutils</artifactid>
                <version>${beanUtils.version}</version>
            </dependency>


        </dependencies>
    </dependencymanagement>

    <build>
        <plugins>
            <plugin>
                <groupid>org.springframwork.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>
</project>

  • 在idea右边菜单执行安装

②搭建Util模块

  • Util模块种类
  1. web业务模块用到的第三方包,比如页面渲染工具、操作cookie的工具类等等。
  2. service业务模块用到的第三方包,比如jdbc、mybatis、jedis、activemq工具包等等。
  3. 通用型的第三方包,比如fastjson、httpclient、apache工具包等等。
  4. 只有本模块用到的 es
  • Util模块依赖关系图

  • 创建三个Util模块

  • Util模块中的 pom.xml
  1. souzhi-common-util
spring-boot-starter-test 测试(springboot有默认版本号)
spring-boot-starter-web 内含tomcat容器、HttpSevrletRequest等 (springboot有默认版本号)
fastjson json工具
commons-lang3 方便好用的apache工具库
commons-beanutils 方便好用的apache处理实体bean工具库
commons-codec 方便好用的apache解码工具库
httpclient restful调用客户端
<!--?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>
    <parent>
        <groupid>com.couragehe.souzhi</groupid>
        <artifactid>souzhi-parent</artifactid>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupid>com.couragehe.souzhi</groupid>
    <artifactid>souzhi-common-util</artifactid>
    <version>0.0.1-SNAPSHOT</version>
    <name>souzhi-common-util</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <!-- SpringBoot 热部署插件       -->
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-devtools</artifactid>
            <optional>true</optional>         <!--这个设置为true热部署才会生效-->
        </dependency>

        <dependency>
            <groupid>com.alibaba</groupid>
            <artifactid>fastjson</artifactid>
        </dependency>


        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
        </dependency>


        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
        </dependency>


        <dependency>
            <groupid>org.apache.httpcomponents</groupid>
            <artifactid>httpclient</artifactid>
        </dependency>

        <dependency>
            <groupid>org.apache.commons</groupid>
            <artifactid>commons-lang3</artifactid>
        </dependency>

        <dependency>
            <groupid>commons-beanutils</groupid>
            <artifactid>commons-beanutils</artifactid>
        </dependency>

        <dependency>
            <groupid>commons-codec</groupid>
            <artifactid>commons-codec</artifactid>
        </dependency>


        <dependency>
            <groupid>com.alibaba</groupid>
            <artifactid>dubbo</artifactid>
        </dependency>

        <dependency>
            <groupid>com.101tec</groupid>
            <artifactid>zkclient</artifactid>
            <exclusions>
                <exclusion>
                    <groupid>org.slf4j</groupid>
                    <artifactid>slf4j-log4j12</artifactid>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupid>com.gitee.reger</groupid>
            <artifactid>spring-boot-starter-dubbo</artifactid>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>

</project>

  1. souzhi-web-util
Thymeleaf/jsp/freemarker springboot自带页面渲染工具(springboot有默认版本号)
<!--?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>
    <parent>
        <groupid>com.couragehe.souzhi</groupid>
        <artifactid>souzhi-parent</artifactid>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupid>com.couragehe.souzhi</groupid>
    <artifactid>souzhi-web-util</artifactid>
    <version>0.0.1-SNAPSHOT</version>
    <name>souzhi-web-util</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupid>com.couragehe.souzhi</groupid>
            <artifactid>souzhi-common-util</artifactid>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- thymeleaf是 springBoot 的原生渲染模板 -->
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-thymeleaf</artifactid>
        </dependency>
        <!--    thymelead支持包、可以设施送校验、热部署 -->
        <dependency>
            <groupid>net.sourceforge.nekohtml</groupid>
            <artifactid>nekohtml</artifactid>
        </dependency>

        <dependency>
            <groupid>xml-apis</groupid>
            <artifactid>xml-apis</artifactid>
        </dependency>

        <dependency>
            <groupid>org.apache.xmlgraphics</groupid>
            <artifactid>batik-ext</artifactid>
        </dependency>
        <dependency>
            <groupid>io.jsonwebtoken</groupid>
            <artifactid>jjwt</artifactid>
            <version>0.9.0</version>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-test</artifactid>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>

</project>

  1. souzhi-service-util
spring-boot-starter-jdbc 数据库驱动(springboot有默认版本号)
mysql-connector-java 数据库连接器(springboot有默认版本号)
mybatis-spring-boot-starter mybatis
<!--?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>
    <parent>
        <groupid>com.couragehe.souzhi</groupid>
        <artifactid>souzhi-parent</artifactid>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupid>com.couragehe.souzhi</groupid>
    <artifactid>souzhi-service-util</artifactid>
    <version>0.0.1-SNAPSHOT</version>
    <name>souzhi-service-util</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupid>com.couragehe.souzhi</groupid>
            <artifactid>souzhi-common-util</artifactid>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-jdbc</artifactid>
        </dependency>

        <dependency>
            <groupid>org.mybatis.spring.boot</groupid>
            <artifactid>mybatis-spring-boot-starter</artifactid>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupid>mysql</groupid>
            <artifactid>mysql-connector-java</artifactid>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupid>redis.clients</groupid>
            <artifactid>jedis</artifactid>
        </dependency>
        <dependency>
            <groupid>org.redisson</groupid>
            <artifactid>redisson</artifactid>
            <version>3.10.5</version>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-test</artifactid>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>

</project>

③搭建api模块

  将interface模块与bean模块整合一起则为api模块

  利用maven的依赖把这些接口作为公共的包管理起来。同时接口类中的方法也引用了很多的实体bean,那么同样的实体bean类也可以统一管理起来。

  • api模块依赖关系图

  bean模块其中引用了通用mapper,所以我们把通用mapper的依赖提取出来放到bean模块后面,变成如下结构。

  • api模块中的 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>

    <groupid>com.couragehe.souzhi</groupid>
    <artifactid>souzhi-api</artifactid>
    <version>0.0.1-SNAPSHOT</version>
    <name>souzhi-api</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <!-- 通用mapper 实现对单表的简单增上改查       -->
        <dependency>
            <groupid>tk.mybatis</groupid>
            <artifactid>mapper-spring-boot-starter</artifactid>
            <version>1.2.3</version>
            <exclusions>
                <exclusion>
                    <groupid>org.springframework.boot</groupid>
                    <artifactid>spring-boot-starter-jdbc</artifactid>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>


    
    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>
</project>

④搭建应用模块(souzhi-search-web)

  • search-web模块依赖关系图

  • search-web模块中的 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>
    <parent>
        <groupid>com.couragehe.souzhi</groupid>
        <artifactid>souzhi-parent</artifactid>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupid>com.couragehe.souzhi</groupid>
    <artifactid>souzhi-search-web</artifactid>
    <version>0.0.1-SNAPSHOT</version>
    <name>souzhi-search-web</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupid>com.couragehe.souzhi</groupid>
            <artifactid>souzhi-api</artifactid>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupid>com.couragehe.souzhi</groupid>
            <artifactid>souzhi-web-util</artifactid>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-autoconfigure</artifactid>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-test</artifactid>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>

</project>

  • Controller测试代码
@Controller
public class SearchController {
    @Reference//import com.alibaba.dubbo.config.annotation.Reference;
    SearchService searchService;

    @RequestMapping("index")
    @ResponseBody
    public String index(){
        String msg = searchService.index();
        return msg;
    }
}
  • 配置文件
server.port=8011
#日志级别
logging.level.root=debug

##dubbo的配置
#dubbo中的服务名称
spring.dubbo.application=search-web
#dubbo中的通讯协议名称
spring.duubbo.protocol.name=dubbo
#zookeeper的注册中心地址
spring.dubbo.registry.address=192.168.10.100:2181
#zookeeper的通讯协议的名称
spring.dubbo.registry.protocol=zookeeper
#dubbo的服务的扫描路径
spring.dubbo.base-package=com.couragehe.souzhi
#设置超时时间
spring.dubbo.consumer.timeout=600000
#设置是否检测服务存在
spring.dubbo.consumer.check=false

④搭建应用模块(souzhi-search-service)

  • search-service模块依赖关系图

  • search-web模块中的 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>
    <parent>
        <groupid>com.couragehe.souzhi</groupid>
        <artifactid>souzhi-parent</artifactid>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupid>com.couragehe.souzhi</groupid>
    <artifactid>souzhi-search-service</artifactid>
    <version>0.0.1-SNAPSHOT</version>
    <name>souzhi-search-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupid>com.couragehe.souzhi</groupid>
            <artifactid>souzhi-api</artifactid>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupid>com.couragehe.souzhi</groupid>
            <artifactid>souzhi-service-util</artifactid>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-autoconfigure</artifactid>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-test</artifactid>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>

</project>

  • Service测试代码
@Service//import com.alibaba.dubbo.config.annotation.Service;
public class SearchServiceImpl implements SearchService {
    @Override
    public String index() {
        return "index";
    }
}
  • 配置文件
server.port=8000
#日志级别
logging.level.root=debug

#jdbc
spring.datasource.password=123456
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://localhost:3306/hms?characterEncoding=UTF-8

##dubbo的配置
#dubbo中的服务名称
spring.dubbo.application=search-service
#dubbo中的通讯协议名称
spring.duubbo.protocol.name=dubbo
#zookeeper的注册中心地址
spring.dubbo.registry.address=192.168.10.100:2181
#zookeeper的通讯协议的名称
spring.dubbo.registry.protocol=zookeeper
#dubbo的服务的扫描路径
spring.dubbo.base-package=com.couragehe.souzhi
#设置超时时间。
spring.dubbo.consumer.timeout=60000
#设置是否检测服务存在
spring.dubbo.consumer.check=false

</parent>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!