dubbo简单入门(helloworld例子) 快来打我* 2022-06-01 02:56 216阅读 0赞 Dubbo现在支持的有三种方式: 1.multicast; 2.zookeeper; 3.redis 下面的Demo使用的是multicast方式。 提供者项目结构: ![这里写图片描述][20160531094349379] 消费者项目结构: ![这里写图片描述][20160531094514333] 服务端: 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.ming</groupId> <artifactId>dubboserver</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.15.0-GA</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> <exclusions> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> <exclusion> <artifactId>jms</artifactId> <groupId>javax.jms</groupId> </exclusion> <exclusion> <artifactId>mail</artifactId> <groupId>javax.mail</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.6</version> </dependency> <dependency> <groupId>com.github.adyliu</groupId> <artifactId>zkclient</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> <version>3.2.0.Final</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency> </dependencies> </project> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 applicationProvider.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="hello-world" /><!-- 注册地址 --> <dubbo:registry address="multicast://224.5.6.7:1234"/> <!-- <dubbo:registry address="127.0.0.1:2181" protocol="zookeeper"/> --> <dubbo:protocol name="dubbo" port="20880" /> <bean id="demoService" class="com.ming.dubboserver.HelloWorldImpl" /> <dubbo:service interface="com.ming.dubboserver.HelloWorld" ref="demoService" executes="10" /> </beans> 1 2 3 4 5 6 7 8 9 10 11 12 13 helloworld接口: package com.ming.dubboserver; public interface HelloWorld { public String hello(String name); } 1 2 3 4 5 6 helloworld实现类 package com.ming.dubboserver; public class HelloWorldImpl implements HelloWorld { public String hello(String name) { name = name + "小明测试"; return name; } } 1 2 3 4 5 6 7 8 9 10 11 12 服务启动类: package com.ming.dubboserver; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DubboProviderMain { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" }); context.start(); System.out.println("Press any key to exit."); System.in.read(); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 客户端: 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.ming</groupId> <artifactId>dubboconsumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>dubboconsumer</name> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.15.0-GA</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> <exclusions> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> <exclusion> <artifactId>jms</artifactId> <groupId>javax.jms</groupId> </exclusion> <exclusion> <artifactId>mail</artifactId> <groupId>javax.mail</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>com.github.adyliu</groupId> <artifactId>zkclient</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.6</version> </dependency> <dependency> <groupId>com.ming</groupId> <artifactId>dubboserver</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 applicationConsumer.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="consumer-of-helloworld-app" /> <dubbo:registry address="multicast://224.5.6.7:1234" /> <dubbo:reference id="demoService" interface="com.ming.dubboserver.HelloWorld" /> </beans> 1 2 3 4 5 6 7 8 9 测试类: package com.ming.dubboconsumer; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ming.dubboserver.HelloWorld; public class ConsumerThd implements Runnable { public void run() { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationConsumer.xml" }); context.start(); context.getBean("demoService"); HelloWorld helloWorld = (HelloWorld) context.getBean("demoService"); String hello = helloWorld.hello("小明"); System.out.println(hello); System.out.println("执行完毕"); } public static void main(String[] args) { new Thread(new ConsumerThd()).start(); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 还有一种方式是将Zookeeper作为注册中心,需要下载并安装zookeeper。将配置文件中的注册中心修改为如下即可: 其他代码不用修改,推荐使用zookeeper作为注册中心。如此可以通过Dubbo+注册中心实现分布式服务。服务消费者不用知道服务的具体位置,只要知道注册中心位置即可,消费者只要能消费,不用管消费的是哪的服务,服务提供方与服务消费方解耦。 使用zookeeper注册中心,与dubbo结合,可以查看服务提供者和消费者的信息,将dubbo管理的war包部署到tomcat上,查看服务提供者 [20160531094349379]: /images/20220601/90cc6c5ccd594ae291df959cfc09b9a5.png [20160531094514333]: /images/20220601/6efe3959b1934cfcb040fd2831b4a18c.png
还没有评论,来说两句吧...