Dubbo中文官网地址 http://dubbo.apache.org/zh-cn/
Dubbo服务提供者和消费者搭建主要配置
服务提供者和服务消费者的pom可以一样,配置如下
<properties>
<!-- 指定maven编译时编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 统一spring版本 -->
<spring-version>4.2.5.RELEASE</spring-version>
<!-- mybatis -->
<mybatis.version>3.4.1</mybatis.version>
<!-- mysql -->
<mysql.version>5.1.39</mysql.version>
<!-- log4j2 -->
<log4j2.version>2.7</log4j2.version>
<!-- slf4j -->
<slf4j.version>1.7.21</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>com.zxp.dubbo</groupId>
<artifactId>DubboApi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- Spring&SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<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-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- c3p0 数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- myBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<!-- myBatis-spring 插件 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!--spring-json依赖 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<!-- log4j 2日志依赖 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!-- 桥接:告诉Slf4j使用Log4j2,已包含Slf4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!-- slf4j 支持 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- jstl表达式 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!-- junit4 测试框架 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- sevlet-api编译支持 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
服务提供者配置文件示例如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用名称, 用于计算依赖关系 -->
<dubbo:application name="dubboprovider"/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<!-- <dubbo:registry address="zookeeper://192.168.220.100:2181"/> -->
<dubbo:registry protocol="zookeeper" address="192.168.220.100:2181,192.168.220.100:2182,192.168.220.100:2183"/>
<!-- 使用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- service实现类作为本地的一个bean -->
<bean id="helloDubbo" class="com.zxp.dubbo.serveiceImpl.HelloServiceImpl"/>
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.zxp.dubbo.HelloService" ref="helloDubbo"/>
</beans>
服务消费者配置文件示例如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="dubboconsumer"/>
<!-- use multicast registry center to discover service -->
<!-- <dubbo:registry address="zookeeper://192.168.220.100:2181"/> -->
<dubbo:registry protocol="zookeeper" address="192.168.220.100:2181,192.168.220.100:2182,192.168.220.100:2183"/>
<!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="helloService" check="false" interface="com.zxp.dubbo.HelloService"/>
</beans>
将上面的服务提供者,消费者加入spring或springmvc项目,就可以搭建dubbo服务了。
解决Eclipse中不识别dubbo标签引起配置文件报错的问题
在Eclipse中使用dubbo时,会发现Eclipse默认不识别dubbo的标签。虽然不妨碍使用,看着红叉也不舒服,所以还是让Eclipse识别一下dubbo标签。
步骤一:
网上下载 dubbo.xsd,放到任意目录,比如E:eclipseplugins目录下,配置时用得到
步骤二:
在Eclipse中点开Window--Preferences,在弹出的对话框中找到XML--XML Catalog,点击右侧的Add
步骤三:
弹出的对话框中,正确填写Location和key,点击OK ,就配好啦
配置dubbo.xsd文件之后,如下图所示
最后步骤:
选择显示错误的“引入dubbo标签的xml文件”,点击右键,选择 "validate"就ok了,如果不行,关闭文件再打开一次就好了。
Dubbo 管控台搭建
步骤一,下载dubbo-admin项目,打成war部署,或直接现在现成的dubbo-admin-2.6.0.war
步骤二,拷贝dubbo-admin-2.6.0.war到tomcat的ROOT目录,解压即可
步骤三,找到dubbo.properties文件,配置zookeeper服务器地址及账号(默认root就行),如下图
最后,启动tomcat,就可以访问了。账号密码都为root。(另外,要保证zookeeper服务器已经启动,否则链接不上zookeeper,会报错)