比尔云BierYun--阿里云最新优惠活动
阿里云优惠码丨阿里云代金券

springmvc整合dubbo分布式系统的搭建demo

springmvc整合dubbo分布式系统的搭建demohttp://www.bieryun.com/2022.html

四个步骤

1 搭建zookeeper消息中心

2 搭建dubbo服务端代码

3 写dubbo客户端代码

4 dubbo提供的dubbo-admin-2.5.4.war 管理控制台

一,搭建zookeeper

1 下载zookeeper安装文件
zookeeper-3.3.6.tar.gz
这里提供一个百度网盘的下载地址http://pan.baidu.com/s/1gexxfvP

2  执行命令 tar zxvf zookeeper-3.4.6.tar.gz 解压,

3 到 zookeeper-3.3.6/conf 目录下

把zoo_sample.cfg 复制一份为zoo.cfg,作为默认配置文件

4 配置说明

tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。

我的配置: 端口2181是默认配置

 

 

5 在zookper-3.3.6/bin目录下执行

./zkServer.sh start 启动zookeeper

 

6 验证zookeeper是否启动

使用netstat -lpn | grep 2181
如果有端口号, 说明已经启动

 

 

二 DubboServer 工程

1,pom.xml

Xml代码  收藏代码
  1. <project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  2.   xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”>
  3.   <modelVersion>4.0.0</modelVersion>
  4.   <groupId>com.zhang.dubboServer</groupId>
  5.   <artifactId>dubboServer</artifactId>
  6.   <packaging>war</packaging>
  7.   <version>0.0.1-SNAPSHOT</version>
  8.   <name>dubboServer Maven Webapp</name>
  9.   <url>http://maven.apache.org</url>
  10.   <dependencies>
  11.     <dependency>
  12.       <groupId>junit</groupId>
  13.       <artifactId>junit</artifactId>
  14.       <version>3.8.1</version>
  15.       <scope>test</scope>
  16.     </dependency>
  17.     <dependency>
  18.         <groupId>javax.servlet</groupId>
  19.         <artifactId>servlet-api</artifactId>
  20.         <version>2.4</version>
  21.       </dependency>
  22.       <dependency>
  23.         <groupId>javax.servlet.jsp</groupId>
  24.         <artifactId>jsp-api</artifactId>
  25.         <version>2.1</version>
  26.       </dependency>
  27.       <dependency>
  28.         <groupId>org.springframework</groupId>
  29.         <artifactId>spring-context</artifactId>
  30.          <version>4.1.6.RELEASE</version>
  31.      </dependency>
  32.       <dependency>
  33.         <groupId>org.springframework</groupId>
  34.         <artifactId>spring-context</artifactId>
  35.         <version>4.1.6.RELEASE</version>
  36.       </dependency>
  37.       <dependency>
  38.         <groupId>org.springframework</groupId>
  39.         <artifactId>spring-webmvc</artifactId>
  40.         <version>4.1.6.RELEASE</version>
  41.       </dependency>
  42.       <dependency>
  43.         <groupId>jstl</groupId>
  44.         <artifactId>jstl</artifactId>
  45.         <version>1.2</version>
  46.     </dependency>
  47.     <dependency>
  48.         <groupId>dubbo</groupId>
  49.         <artifactId>dubbo</artifactId>
  50.         <version>2.5.3</version>
  51.     </dependency>
  52.     <dependency>
  53.       <groupId>javassist</groupId>
  54.       <artifactId>javassist</artifactId>
  55.       <version>3.15.0-GA</version>
  56.       <classifier>GA</classifier>
  57.     </dependency>
  58.     <dependency>
  59.         <groupId>netty</groupId>
  60.         <artifactId>netty</artifactId>
  61.         <version>3.2.5.Final</version>
  62.     </dependency>
  63.     <dependency>
  64.         <groupId>log4j</groupId>
  65.         <artifactId>log4j</artifactId>
  66.         <version>1.2.17</version>
  67.     </dependency>
  68.     <dependency>
  69.         <groupId>zkclient</groupId>
  70.         <artifactId>zkclient</artifactId>
  71.         <version>0.4</version>
  72.     </dependency>
  73.     <dependency>
  74.         <groupId>zookeeper</groupId>
  75.         <artifactId>zookeeper</artifactId>
  76.         <version>3.3.1</version>
  77.     </dependency>
  78.   </dependencies>
  79.   <build>
  80.     <finalName>dubboServer</finalName>
  81.   </build>
  82. </project>

 

2.web.xml

Xml代码  收藏代码
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <web-app
  3.         xmlns=“http://java.sun.com/xml/ns/javaee”
  4.         xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  5.         xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd”
  6.         version=“3.0”
  7.         metadata-complete=“false”>
  8.     <context-param>
  9.         <param-name>contextConfigLocation</param-name>
  10.         <param-value>/WEB-INF/spring/springmvc-config.xml;classpath*:dubbo/*.xml</param-value>
  11.     </context-param>
  12.     <listener>
  13.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  14.     </listener>
  15.     <servlet>
  16.         <servlet-name>DispatcherServlet</servlet-name>
  17.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  18.         <init-param>
  19.             <param-name>contextConfigLocation</param-name>
  20.             <param-value>/WEB-INF/spring/springmvc-config.xml</param-value>
  21.         </init-param>
  22.         <load-on-startup>1</load-on-startup>
  23.         <async-supported>true</async-supported>
  24.     </servlet>
  25.     <servlet-mapping>
  26.         <servlet-name>DispatcherServlet</servlet-name>
  27.         <url-pattern>/</url-pattern>
  28.     </servlet-mapping>
  29.     <filter>
  30.          <filter-name>CharacterEncodingFilter</filter-name>
  31.           <filter-class>
  32.             org.springframework.web.filter.CharacterEncodingFilter
  33.          </filter-class>
  34.          <async-supported>true</async-supported>
  35.          <init-param>
  36.                 <param-name>encoding</param-name>
  37.                 <param-value>UTF-8</param-value>
  38.          </init-param>
  39.          <init-param>
  40.               <param-name>forceEncoding</param-name>
  41.               <param-value>true</param-value>
  42.          </init-param>
  43.     </filter>
  44.     <filter-mapping>
  45.          <filter-name>CharacterEncodingFilter</filter-name>
  46.          <url-pattern>/*</url-pattern>
  47.     </filter-mapping>
  48. </web-app>

 3,spring和dubbo配置文件

Xml代码  收藏代码
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans:beans xmlns=“http://www.springframework.org/schema/mvc”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xmlns:beans=“http://www.springframework.org/schema/beans”
  5.     xmlns:context=“http://www.springframework.org/schema/context”
  6.     xsi:schemaLocation=”http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
  7.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd”>
  9.     <!– Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory –>
  10.     <resources mapping=“/resources/**” location=“/resources/” />
  11.     <!– Enables the Spring MVC @Controller programming model –>
  12.     <annotation-driven ></annotation-driven>
  13.     <!–spring 自动扫描com.zhang下边的所有类  –>
  14.     <context:component-scan base-package=“com.zhang” />
  15.     <context:property-placeholder location=“classpath:config.properties” />
  16.     <!– 自定义配置Service –>
  17.     <beans:bean id=“sayHelloService” class=“com.zhang.service.demoImpl”/>
  18. </beans:beans>

 

Xml代码  收藏代码
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:dubbo=“http://code.alibabatech.com/schema/dubbo”
  4.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd
  6.         http://code.alibabatech.com/schema/dubbo
  7.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  8.         “>
  9.     <!– 系统项目名 –>
  10.     <dubbo:application name=“DubboDemo” />
  11.     <!– 注册中心 –>
  12.     <dubbo:registry protocol=“zookeeper” address=“${able_zookeeper}”  />
  13.     <!– 是否纳入调用统计报表(可选) –>
  14.     <dubbo:monitor protocol=“registry”/>
  15.     <!– 协议 –>
  16.     <dubbo:protocol name=“dubbo” port=“31581” />
  17.     <!– 服务者与消费者的默认配置 –>
  18.     <!– 延迟到Spring初始化完成后,再暴露服务,服务调用超时设置为6秒,超时不重试   –>
  19.     <dubbo:provider delay=“-1” timeout=“6000” retries=“0”/>
  20.     <dubbo:consumer timeout=“6000” retries=“0”/>
  21. </beans>

 

Xml代码  收藏代码
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:dubbo=“http://code.alibabatech.com/schema/dubbo”
  4.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd
  6.         http://code.alibabatech.com/schema/dubbo
  7.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  8.         “>
  9.     <dubbo:service interface=“com.zhang.service.demo” ref=“sayHelloService”  timeout=“3000” protocol=“dubbo”/>
  10. </beans>

config.properties

Java代码  收藏代码
  1. #able_zookeeper
  2. able_zookeeper=192.168.50.42:2181

4,java代码 一个接口一个实现类

Java代码  收藏代码
  1. package com.zhang.service;
  2. public interface demo {
  3.     public String sayHello();
  4. }

 

Java代码  收藏代码
  1. package com.zhang.service;
  2. public class demoImpl implements demo {
  3.     @Override
  4.     public String sayHello() {
  5.         return “hello”;
  6.     }
  7. }

 

 

三 DubboClient工程

 

1,pom.xml和web.xml,config.properties配置文件和DubboServer配置一样

2,增加一个消费者配置文件applicationContext-dubbo-consumer.xml,注:这里需要把服务提供者的jar包打入到客户端,打接口就可以

Xml代码  收藏代码
  1. <strong><?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xmlns:dubbo=“http://code.alibabatech.com/schema/dubbo”
  4.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd
  6.         http://code.alibabatech.com/schema/dubbo
  7.         http://code.alibabatech.com/schema/dubbo/dubbo.xsd”>
  8.     <dubbo:reference id=“demo” interface=“com.zhang.service.demo” check=“false” protocol=“dubbo”/>
  9. </beans> </strong>

 3,使用

Java代码  收藏代码
  1. <strong>package com.zhang.contr;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. public class test {
  4.     @Autowired
  5.     private com.zhang.service.demo demo;
  6.     public String testD(){
  7.         return demo.sayHello();
  8.     }
  9. }
  10. </strong>

 

四 dubbo-admin-2.5.4.war 管理控制台

    war包下载地址:http://pan.baidu.com/s/1i4xZamD

把这个war包放到tomcat的webapps目录下

1 修改tomcat的端口号不要和其他服务冲突.

2 修改 WEB-INF 下的dubbo.properties文件

 

 

3 启动tomcat,访问.

 

 

想看源码的提供一个github的访问地址

https://github.com/zqh1989/DubboDemo

 

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

强烈推荐

高性能SSD云服务器ECS抗攻击,高可用云数据库RDS