1, project structure (maven project)
2、pom
<?xml version="1.0" encoding="UTF-8"?> <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.an</groupId> <artifactId>dubbo-zookeeper-test</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>api</module> <module>provider</module> <module>consumer</ Module> <- GA version is widely used in internal Alibaba:! 2.4.9, this version is strongly recommended -> <the Properties> </ modules> <motan.version>0.3.0</motan.version> <dubbo.version>2.5.3</dubbo.version> <dubbox.version>2.8.4</dubbox.version> <spring.version>4.3.6.RELEASE</spring.version> <java.version>1.7</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!--dubbo--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <!--zookeeper 客户端 jar 包依赖--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.3</version> </dependency> <!--zookeeper zkclient客户端--> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!--zookeeper curator客户端--> <dependency> <groupId>com.netflix.curator</groupId> <artifactId>curator-framework</artifactId> <version>1.1.10</version> </dependency> <!-- spring相关 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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-web</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-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</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.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.11</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> </dependency> </dependencies> </project>
3、api
3.1 pom
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>dubbo-zookeeper-test</artifactId> <groupId>com.an</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>api</artifactId> </project>
3.2 HelloService
package com.an.service; public interface HelloService { String sayHello(String msg); }
4、provider
4.1 pom
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>dubbo-zookeeper-test</artifactId> <groupId>com.an</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>provider</artifactId> <dependencies> <dependency> <groupId>com.an</groupId> <artifactId>api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
4.2 log4j.properties
###set log levels### log4j.rootLogger=info, stdout ###output to the console### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n
4.3 dubbo-provider.xml
<? XML Version = "1.0" encoding = "UTF-. 8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: the 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: // the WWW. springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <the bean ID =" helloService "class =" com.an.service.HelloServiceImpl "/> <- application configuration:! for the current application configuration information, regardless of whether the application is a provider or consumer -> <! - Provider Application Name -> <dubbo:application name="dubbo-provider"/> <-! Registration Center Configuration: used to configure the connection to the registry information -> <- - Use zookeeper exposure registry service address!> <Dubbo: Registry address = "zookeeper: //192.168.1.106: 2181" = Client "Curator" /> <- protocol configuration:! for protocol configuration provides information services, protocols specified by the provider, consumer passively accept -> <! - dubbo agreement with exposure to port services 20880 - > <Dubbo: protocol name = "Dubbo" Port = "20 881" /> <- service configuration:! for exposing a service and meta information defining the service, a service may be exposed to a plurality of protocols, a service may be registered to more than one registry -> <! - statement needs to be exposed service interface timeout timeout error over time too -> <Dubbo: service interface = "com.an.service.HelloService" ref = "helloService" timeout = "5000" /> </ Beans>
4.4 HelloServiceImpl
package com.an.service; public class HelloServiceImpl implements HelloService { public String sayHello(String msg) { return msg; } }
4.5 ProviderRunner
package com.an; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.io.IOException; public class ProviderRunner { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] {"dubbo-provider.xml"}); context.start(); System.out.println("provider...started..."); System.in.read(); } }
5、consumer
5.1 pom
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>dubbo-zookeeper-test</artifactId> <groupId>com.an</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>consumer</artifactId> <dependencies> <dependency> <groupId>com.an</groupId> <artifactId>api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
5.2 log4j.properties
###set log levels### log4j.rootLogger=info, stdout ###output to the console### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n
5.3 dubbo-consumer.xml
<? XML Version = "1.0" encoding = "UTF-. 8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: the 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: // the WWW. springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <-! application configuration: used to configure the current application information, regardless of whether the application is the provider or the consumer -> <- consumer application name ->! <Dubbo: the application name = "Dubbo-consumer" /> <-! Registration Center Configuration: used to configure the connection to the registry information -> <Dubbo: Registry Protocol = "ZooKeeper" address = "ZooKeeper: //192.168.1.106: 2181" /> <- reference configuration:! to create a remote service agent, a reference can point to more than one registry -> <Dubbo: Reference ID = "the helloService" interface = "com.an.service.HelloService" /> </ Beans>
5.4 ConsumerRunner
package com.an; import com.an.service.HelloService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ConsumerRunner { public static void main(String[] args){ ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"dubbo-consumer.xml"}); context.start(); HelloService helloService =(HelloService) context.getBean("helloService"); String msg=helloService.sayHello("hello world"); System.out.println(msg); } }