Dubbo入门-简易配置

前言

想看分布式的东西好久了,目前的公司用不到,只能自己去网上找相关的,看到最多的Dubbo,于是趁着今天有时间自己照着一些教程自己配置了个基础的demo,顺便放出来。本文实例基于maven+spring+dubbo+zookeeper,开发工具为IDEA,系统环境为Windows10。

-2017.8.31

一、安装并启动zookeeper

此处用zookeeper作为dubbo的注册中心。

1.下载

本文使用的为zookeeper-3.4.10,下载地址为:http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.10/,其他版本可从http://mirrors.hust.edu.cn/apache/zookeeper/获取,其他下载地址可前往http://www.apache.org/dyn/closer.cgi/zookeeper/查阅。

2.安装

由于本身是绿色版,只需在磁盘中解压出来即可。另,zookeeper-3.4.10\conf目录下的zoo_sample.cfg文件需要复制一份并重命名为zoo.cfg

zoo.cfg为zookeeper默认查找的配置文件名称。

3.配置

编辑zoo.cfg文件。

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

其中

相关参数

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

dataDir目录需改成你真实输出目录,其余的看个人喜好修改。

4.启动

进入zookeeper-3.4.10\bin目录下,根据个人系统启动项目,其中Windows下直接运行zkServer.cmd文件即可。其余可参考文末附录。启动成功后会停留在binding to port 处,如图:

二、创建对外提供服务的接口---dubbo-api

这是一个项目,也可以算是一个模块,因为提供者(provider)和消费者(consumer)都需要引入该模块。

提供者对该接口的做具体实现,消费者通过该接口调用提供者的具体实现并获取结果。

可以通过添加module或者将该项目打成jar的方式引入提供者与消费者项目中。

该项目在本文被命名为dubbo-api。

1.接口TestService.java

package com.windcoder.dubbo.service;

/**
 * Description:
 * User: WindCoder
 * Date: 2017-08-30
 * Time: 14:39 下午
 */
public interface TestService {
    /**
     * 测试发消息
     * @param name
     * @return
     */
    public String sayHello(String name);
}

2.该项目的pom.xml配置

默认即可,类似如下,主要需要注意packaging应为jar

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.windcoder</groupId>
  <artifactId>dubbo-api</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>dubbo-api</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>dubbo-api</finalName>
  </build>
</project>

三、创建服务提供者---dubbo-server

该项目用于对上面dubbo-api中定义的接口进行相关实现。

记得开始所说,需要引入dubbo-api项目或其构建的jar包,否则会报无法找到TestService的错误。

1.具体实现类TestServiceImpl.java

package com.windcoder.dubboServer.service.impl;


import com.windcoder.dubbo.service.TestService;
import org.springframework.stereotype.Service;

/**
 * Description:
 * User: wind
 * Date: 2017-08-30
 * Time: 11:59 上午
 */
@Service("testService")
public class TestServiceImpl implements TestService {
    public String sayHello(String name) {
        return name + " service say hello Test service By windCoder.com!";
    }
}

2.该项目的pom.xml配置

由于引入过多,故做了折叠处理。

dubbo-server项目的pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.windcoder</groupId>
  <artifactId>dubbo-server</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>dubbo-server Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <java.version>1.8</java.version>
    <!-- spring版本号 -->
    <spring.version>4.3.7.RELEASE</spring.version>
    <!-- log4j日志文件管理包版本 -->
    <slf4j.version>1.7.7</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
    <dubbo.version>2.5.3</dubbo.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.windcoder</groupId>
      <artifactId>dubbo-api</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>${dubbo.version}</version>
      <exclusions>
        <exclusion>
          <artifactId>log4j</artifactId>
          <groupId>log4j</groupId>
        </exclusion>
        <exclusion>
          <artifactId>commons-logging</artifactId>
          <groupId>commons-logging</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.6</version>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.1</version>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.apache.zookeeper</groupId>
          <artifactId>zookeeper</artifactId>
        </exclusion>
      </exclusions>
    </dependency>



    <!-- spring核心包 -->

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <!-- spring核心包 end -->
    <!-- log start -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>



    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <!-- log end -->
  </dependencies>

  <build>
    <finalName>dubbo-server</finalName>
    <defaultGoal>compile</defaultGoal>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>

    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>
</project>

3、项目的spring配置

(1)spring.xml

spring的基本配置文件。鉴于非主要内容,故折叠。

spring的基本配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 设置需要进行Spring注解扫描的类包 -->
    <context:component-scan base-package="com.windcoder.*"/>

    <import resource="classpath:conf/dubbo.xml"/>
</beans>

(2)web.xml

此处顺便将log4j的web.xml文件配置一起贴出,故这里的xml是一个完整的web.xml配置。

web.xml完整配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
  <display-name>Archetype Created Web Application</display-name>

  <!-- 读取spring配置文件 -->

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:conf/spring.xml</param-value>

  </context-param>

  <!-- 设计路径变量值 -->



  <!-- 设计路径变量值 -->

  <context-param>
    <param-name>webAppRootKey</param-name>
    <!--<param-value>authplatTest.root</param-value>-->
    <param-value>dubbo-server.root</param-value>
  </context-param>

  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:conf/log4j.properties</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>


  <!-- 编码过滤器 -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <async-supported>true</async-supported>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- Spring监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 防止Spring内存溢出监听器 -->
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>

</web-app>

4.dubbo.xml配置文档

此处用于dubbo提供者的配置,测试中该提供者实现了两个接口,为教程简洁,上面仅提了TestService接口:

<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">




    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="test_provider" />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.0.251:2181" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.windcoder.dubbo.service.TestService" ref="testService" />
    <dubbo:service interface="com.windcoder.dubbo.service.HelloService" ref="helloService" />

    <!--错误声明,因为该服务中未实现WindCoderService-->
    <!--<dubbo:service interface="com.windcoder.dubbo.service.WindCoderService" ref="windCoderService" />-->

</beans>

5.log4j.properties

由于不是主要内容,此处暂且省略介绍。

四、创建服务消费者---dubbo-consumer

该项目为调用远程服务的服务消费方。

记得开始所说,需要引入dubbo-api项目或其构建的jar包,否则会报无法找到TestService的错误。

1.消费者类实现JSAPIController.java

此处本身实现了多个,其酌情删减。

JSAPIController.java

package com.windcoder.dubboConsumer.controller;

import com.windcoder.dubbo.service.HelloService;
import com.windcoder.dubbo.service.TestService;
import com.windcoder.dubbo.service.WindCoderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Description:
 * User: wind
 * Date: 2017-08-30
 * Time: 14:57 下午
 */
@Controller("/*")
public class JSAPIController {
    @Autowired
    private TestService testService;
    @Autowired
    private HelloService helloService;
    @Autowired
    private WindCoderService windCoderService;

    @RequestMapping(value = "/test.do")
    @ResponseBody
    public String testSay(@RequestParam(value = "name",defaultValue = "") String name){
        StringBuffer sb = new StringBuffer();
        sb.append("Dubbo: ").append(testService.sayHello(name));
        return sb.toString();
    }
    @RequestMapping(value = "/hello.do")
    @ResponseBody
    public String helloSay(@RequestParam(value = "name",defaultValue = "") String name){
        StringBuffer sb = new StringBuffer();
        sb.append("Dubbo: ").append(helloService.sayHello(name));
        return sb.toString();
    }

    @RequestMapping(value = "/wind.do")
    @ResponseBody
    public String windCoderService(@RequestParam(value = "name",defaultValue = "") String name){
        StringBuffer sb = new StringBuffer();
        sb.append("Dubbo: ").append(windCoderService.sayHello(name));
        return sb.toString();
    }


}

2.该项目的pom.xml配置

由于引入过多,故做了折叠处理。

dubbo-consumer项目的pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.windcoder</groupId>
  <artifactId>dubbo-consumer</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>dubbo-consumer Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <java.version>1.8</java.version>
    <!-- spring版本号 -->
    <spring.version>4.3.7.RELEASE</spring.version>
    <!-- log4j日志文件管理包版本 -->
    <slf4j.version>1.7.7</slf4j.version>
    <log4j.version>1.2.17</log4j.version>
    <dubbo.version>2.5.3</dubbo.version>
  </properties>


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.windcoder</groupId>
      <artifactId>dubbo-api</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>${dubbo.version}</version>
      <exclusions>
        <exclusion>
          <artifactId>log4j</artifactId>
          <groupId>log4j</groupId>
        </exclusion>
        <exclusion>
          <artifactId>commons-logging</artifactId>
          <groupId>commons-logging</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.6</version>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.1</version>
      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.apache.zookeeper</groupId>
          <artifactId>zookeeper</artifactId>
        </exclusion>
      </exclusions>
    </dependency>



    <!-- spring核心包 -->

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- spring核心包 end -->
    <!-- log start -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>



    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <!-- log end -->
  </dependencies>
  <build>
    <finalName>dubbo-consumer</finalName>
    <defaultGoal>compile</defaultGoal>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>

    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>
</project>

3、项目的spring配置

(1)spring.xml

spring的基本配置文件,内容同上面server中的。

(2)spring-mvc.xml

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.windcoder.dubboConsumer.controller">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation"
                                expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />


    <!-- springmvc传json值时的乱码解决 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>


    <!-- 控制器异常处理 -->
    <bean id="exceptionHandlerExceptionResolver" class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
    </bean>




</beans>

(3)web.xml

在提供者的web.xml的基础上配置springMVC。

web.xml

 <!-- springMVC核心配置 -->

  <servlet>

    <servlet-name>spring</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

      <param-name>contextConfigLocation</param-name>

      <param-value>classpath:conf/spring-mvc.xml</param-value>

    </init-param>


    <load-on-startup>2</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>spring</servlet-name>

    <url-pattern>*.do</url-pattern>

  </servlet-mapping>

4.dubbo.xml配置文档

<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">




    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="demo-consumer"  />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.0.251:2181" />

    <!-- 生成远程服务代理,可以和本地bean一样使用testService-->
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:reference interface="com.windcoder.dubbo.service.TestService" id="testService" check="false" />
    <dubbo:reference interface="com.windcoder.dubbo.service.HelloService" id="helloService" check="false" />
    <dubbo:reference interface="com.windcoder.dubbo.service.WindCoderService" id="windCoderService" check="false" />


</beans>

5.log4j.properties

由于不是主要内容,此处暂且省略介绍。

五、安装管理控制台

本身经过以上几步,一个简易程序便可运行了。但由于Zookeeper本身为命令窗口形式,无法很好的检测与管理相关接口等,dubbo官方则为我们提供了相应;de管理后台。

下载

从官方仓库下载所有源码文件即可,地址:https://github.com/alibaba/dubbo

配置

对其中的dubbo-admin模块下的webapps/ROOT/WEB-INF/dubbo.properties文件进行修改。分别是zookeeper地址,root的登陆密码,guest的登陆密码:

dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

请根据实际情况修改,此处仅作参考。

启动

对其中的dubbo-admin模块进行打包,将生成的WAR包放到Tomcat中运行起来即可。或者本地测试也可以在idea中打开运行。最终可查看到如本文开头的界面。

附录

扩展资料

待续。

demo源文件地址:https://github.com/iwinder/dubboDemo.git

参考资料

Maven+SpringMVC+Dubbo 简单的入门demo配置

使用者指南

Zookeeper注册中心安装

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏微信公众号:Java团长

教你理清SpringBoot与SpringMVC的关系

spring boot就是一个大框架里面包含了许许多多的东西,其中spring就是最核心的内容之一,当然就包含spring mvc。spring mvc 是只是...

2034
来自专栏Spring相关

第5章—构建Spring Web应用程序—SpringMVC详解

第二步:前端控制器请求HandlerMapping查找 Handler (可以根据xml配置、注解进行查找)

1554
来自专栏Java进阶干货

SpringMVC工作流程

DispatcherServlet:前端控制器,是 SpringMVC 工作流程的中心,负责调用其他组件,在系统启动时就加载该类。

2.1K3
来自专栏令仔很忙

Hibernate从入门到放弃(二)----和这个世界打声招呼(HelloWorld)

2)、新建项目hw_hibernate 3)、导入Jar包 请下载hibernate_lib.zip拷贝到lib目录下 4)、新建配置文件hiber...

801
来自专栏熊二哥

Spring快速入门

Spring源码剖析一书,非常细致,但对于个人来说,意义不算特别的大,因此选其可用部分做个记录就好。 ? 功能 示例 配置文件的解析...

20510
来自专栏源码之家

终于解决WIN7共享经常连不上的问题了。

1492
来自专栏IT笔记

SpringBoot 2.x中英文配置详解

此示例文件作为指导提供。请勿将其全部内容复制到您自己的应用程序中。^^^ 英文配置 # ==================================...

3554
来自专栏Java架构

教你理清SpringBoot与SpringMVC的关系

spring boot就是一个大框架里面包含了许许多多的东西,其中spring就是最核心的内容之一,当然就包含spring mvc。spring mvc 是只是...

1533
来自专栏一枝花算不算浪漫

[Java面试六]SpringMVC总结以及在面试中的一些问题.

36310
来自专栏一个会写诗的程序员的博客

第9章 Spring Boot开发者工具第9章 Spring Boot开发者工具

Spring Boot Maven Plugin,提供了使用Maven构建Spring Boot 工程的支持。我们可以用这个插件完成打包功能。支持打可执行jar...

2062

扫码关注云+社区

领取腾讯云代金券