【Zookeeper学习八】——dubbo+zookeeper结合maven搭建分布式系统架构

前言

学习zookeeper的目的无外乎希望用在工程中,搭建一个简单的dubbo分布式系统对我们理解分布式有很大的帮助!

内容

1.创建idea搭建的ssm项目:

idea搭建ssm项目,也可以直接下载项目:idea搭建的ssm架构项目

2.安装zookeeper单机版或者集群,本小编的相关博客:

【Zookeeper学习四】——伪集群搭建和集群搭建教程【Zookeeper学习一】-单机版安装完整教程

3.拆分web到parent目录下,将service改成war包形式

改造后项目的目录结构:
这里写图片描述

  • 将web拖出manager项目,改变pom文件,改变parent为e3-parent,添加spring依赖,改变之后重新install:
 <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
  • 改变web.xml文件,减去spring的配置:
<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>e3-manager-web</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 解决post乱码 -->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!--springmvc前端控制器-->
 <servlet>
   <servlet-name>e3-manager</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:spring/springmvc.xml</param-value>
   </init-param>
 </servlet>
  <servlet-mapping>
    <servlet-name>e3-manager</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
  • 改变service文件打包方式为war包,添加tomcat的插件
   <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <artifactId>e3-manager-service</artifactId>
<!-- 配置tomcat插件 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <port>8084</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • war包是一种web工程模块,所以应该在service下添加webapp目录,并且添加web.xml,之后重新install
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>e3-manager</display-name>
  <!--spring容器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext-*.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

4.在pom中添加zkclient,dubbo依赖

e3-parent.xml

 <properties>    
        <dubbo.version>2.5.3</dubbo.version>
        <zookeeper.version>3.4.7</zookeeper.version>
        <zkclient.version>0.1</zkclient.version>
    </properties>
 <!-- dubbo相关 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.jboss.netty</groupId>
                        <artifactId>netty</artifactId>
                    </exclusion>
                </exclusions>
            <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zookeeper.version}</version>
            </dependency>
           <dependency>
               <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zkclient.version}</version>
            </dependency>

e3-web和e3-service的pom.xml中同样添加如下依赖:

<!-- dubbo相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jboss.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
       <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
       </dependency>

4.代码书写:

ItemController:

@Controller
public class ItemCatController {
    @Autowired
    private ItemCatService itemCatService;
    @RequestMapping("/item/cat/list")
    @ResponseBody
    public List<EasyUITreeNode> getItemCat(@RequestParam(name="id",defaultValue = "0") long parentId){
        List<EasyUITreeNode> nodeList=itemCatService.getItemCat(parentId);
        return nodeList;
    }
}

ItemService接口:

 public TbItem getItemById(long id);

ItemServiceImpl实现:

  @Autowired
     private TbItemMapper tbItemMapper;
    @Override
    public TbItem getItemById(long id) {
      TbItem item=tbItemMapper.selectByPrimaryKey(id);
      return  item;

    }

【注意】自动生成的实体一定要序列化,否则后期会报错:

public class TbItem implements Serializable{
    private Long id;
    。。。。。。。。。。。。。。。。。。

5.在配置文件中添加dubbo配置

springmvc.xml

 <!--添加dubbo的服务-->
    <dubbo:application name="e3-manager-web"></dubbo:application>
    <!--下面address代码zookeeper的地址例如:localhost:2181,如果是集群就在地址之间写逗号-->
   <dubbo:registry address="zookeeper地址" protocol="zookeeper"></dubbo:registry>
   <dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService"></dubbo:reference>

applicationContext.xml中:

<!-- 使用dubbo发布服务 -->
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="e3-manager" />
    <dubbo:registry protocol="zookeeper"
                    address="192.168.25.131:2181" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="cn.e3mall.service.ItemService" ref="itemServiceImpl" timeout="600000" />

6.配置启动项:

这里写图片描述
!因为现在是两个war,所以需要配置两个启动,分别如下:
这里写图片描述
这里写图片描述

7.启动:

在浏览器中输入localhost:8083/item/5663出现如下界面
这里写图片描述

总结

本程序地址内含数据库,只要修改zookeeper的地址和数据库地址就可以启动这个项目,地址:[idea工具结合maven+zookeeper+dubbo搭建简单分布式架构],(https://download.csdn.net/download/changyinling520/10533272),感谢浏览!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值