Dubbo---初体验
Posted anpeiyong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo---初体验相关的知识,希望对你有一定的参考价值。
1、项目结构(maven项目)
2、dubbotest.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-test</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <properties> <motan.version>0.3.0</motan.version> <!-- 在阿里巴巴内部广泛使用的GA版本为:2.4.9,强烈推荐此版本 --> <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> <modules> <module>provider</module> <module>consumer</module> <module>api</module> </modules> <dependencies> <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> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</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-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-test</artifactId> <groupId>com.an</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>provider</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <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=[%ddd/MM/yy hh:mm:ss:sss z] %t %5p %c2: %m%n
4.3、HelloServiceImpl
package com.an.service; public class HelloServiceImpl implements HelloService public String sayHello(String msg) return msg;
4.4、dubbo-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="dubbo-provider"/> <!--使用multicast广播注册中心暴露服务地址--> <dubbo:registry address="multicast://224.5.6.7:1234"/> <!--用dubbo协议在20880端口暴露服务--> <dubbo:protocol name="dubbo" port="20880"/> <!--声明需要暴露的服务接口 timeout 超时时间 超过时间过报错--> <dubbo:service interface="com.an.service.HelloService" ref="helloService" timeout="5000"/> </beans>
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-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=[%ddd/MM/yy hh:mm:ss:sss z] %t %5p %c2: %m%n
5.3、dubbo-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: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="dubbo-consumer"/> <!--使用multicast广播注册中心暴露发现服务地址--> <dubbo:registry address="multicast://224.5.6.7:1234"/> <!--生成远程服务代理,可以和本地bean一样使用demoService--> <dubbo:reference id="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);
以上是关于Dubbo---初体验的主要内容,如果未能解决你的问题,请参考以下文章