Dubbo+Zookeeper平台的搭建
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo+Zookeeper平台的搭建相关的知识,希望对你有一定的参考价值。
1、创建两个maven工程:provider和consumer
2、切换到provider
1)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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wb.dubbo</groupId> <artifactId>provider</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <!-- spring版本号 --> <spring.version>4.2.5.RELEASE</spring.version> </properties> <dependencies> <!-- DUBBO --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>commons-logging</artifactId> <groupId>commons-logging</groupId> </exclusion> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> <version>4.11</version> </dependency> <!-- spring核心包 --> <!-- springframe start --> <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> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency> <!-- springframe end --> </dependencies> </project>
2) 创建DemoService接口
package com.wb.dubbo.service; public interface DemoService { String sayHello(String name); }
实现DemoService接口,DemoServiceImpl
package com.wb.dubbo.serviceImpl; import com.wb.dubbo.service.DemoService; public class DemoServiceImpl implements DemoService { @Override public String sayHello(String name) { return "hello " + name; } }
3)配置文件provider.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.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="CIF" /> <!-- 使用multicast广播注册中心暴露服务地址 uat --> <dubbo:registry address="zookeeper://10.199.11.11:2181?backup=10.199.11.22:2181,10.199.101.213:2181"/> <dubbo:provider retries="0" cluster="failfast"></dubbo:provider> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="-1" /> <dubbo:monitor protocol="registry" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.wb.dubbo.service.DemoService" ref="demoService" timeout="1000" version="1.0.0" /> <!-- 具体的实现bean --> <bean id="demoService" class="com.wb.dubbo.serviceImpl.DemoServiceImpl" /> </beans>
2、切换到consumer
1)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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wb.dubbo</groupId> <artifactId>consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <!-- spring版本号 --> <spring.version>4.2.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>com.shhxzq.fin.cif</groupId> <version>1.0.14-SNAPSHOT</version> <artifactId>cif-service</artifactId> </dependency> <dependency> <groupId>com.wb.dubbo</groupId> <artifactId>provider</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- DUBBO --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> <exclusion> <artifactId>commons-logging</artifactId> <groupId>commons-logging</groupId> </exclusion> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> <version>4.11</version> </dependency> <!-- spring核心包 --> <!-- springframe start --> <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> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency> <!-- springframe end --> </dependencies> </project>
2)调用provider的服务,创建 BaseTest
package consumer; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:consumer.xml" }) public class BaseTest { }
package consumer; import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import com.wb.dubbo.service.DemoService; public class DemoTest extends BaseTest { @Autowired private DemoService demoService; @Test public void sayHello() { System.out.println(demoService.sayHello("world")); } }
3)配置consumer
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- enable component scanning (beware that this does not enable mapper scanning!) --> <!-- enable autowire --> <context:annotation-config /> <dubbo:application name="cif"/> <!-- 使用multicast广播注册中心暴露服务地址 uat --> <dubbo:registry address="zookeeper://10.199.11.11:2181?backup=10.199.11.22:2181,10.199.101.213:2181"/> <!-- 注册需要消费的服务 --> <dubbo:reference id="demoService" interface="com.wb.dubbo.service.DemoService" lazy="true" version="1.0.0" check="false" timeout="1000" /> </beans>
大功告成!!
以上是关于Dubbo+Zookeeper平台的搭建的主要内容,如果未能解决你的问题,请参考以下文章
dubbo框架Demo之dubbo-admin管理平台和zookeeper注册中心搭建
dubbo系列dubbo+zookeeper+dubboadmin分布式服务框架搭建(windows平台)