Dubbo现在支持的有三种方式:
1.multicast;
2.zookeeper;
3.redis
下面的Demo使用的是multicast方式。
服务端:
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.0modelVersion>
<groupId>com.minggroupId>
<artifactId>dubboserverartifactId>
<version>0.0.1-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.5.3version>
dependency>
<dependency>
<groupId>org.javassistgroupId>
<artifactId>javassistartifactId>
<version>3.15.0-GAversion>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.15version>
<exclusions>
<exclusion>
<groupId>com.sun.jdmkgroupId>
<artifactId>jmxtoolsartifactId>
exclusion>
<exclusion>
<groupId>com.sun.jmxgroupId>
<artifactId>jmxriartifactId>
exclusion>
<exclusion>
<artifactId>jmsartifactId>
<groupId>javax.jmsgroupId>
exclusion>
<exclusion>
<artifactId>mailartifactId>
<groupId>javax.mailgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.1.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.5version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.6.1version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.3.6version>
dependency>
<dependency>
<groupId>com.github.adyliugroupId>
<artifactId>zkclientartifactId>
<version>2.0version>
dependency>
<dependency>
<groupId>org.jboss.nettygroupId>
<artifactId>nettyartifactId>
<version>3.2.0.Finalversion>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>0.4version>
dependency>
dependencies>
project>
applicationProvider.xml配置:
<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: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>
helloworld接口:
package com.ming.dubboserver;
public interface HelloWorld {
public String hello(String name);
}
helloworld实现类
package com.ming.dubboserver;
public class HelloWorldImpl implements HelloWorld {
public String hello(String name) {
name = name + "小明测试";
return name;
}
}
服务启动类:
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();
}
}
客户端:
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.0modelVersion>
<groupId>com.minggroupId>
<artifactId>dubboconsumerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>dubboconsumername>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.11version>
<scope>testscope>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.5.3version>
dependency>
<dependency>
<groupId>org.javassistgroupId>
<artifactId>javassistartifactId>
<version>3.15.0-GAversion>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.15version>
<exclusions>
<exclusion>
<groupId>com.sun.jdmkgroupId>
<artifactId>jmxtoolsartifactId>
exclusion>
<exclusion>
<groupId>com.sun.jmxgroupId>
<artifactId>jmxriartifactId>
exclusion>
<exclusion>
<artifactId>jmsartifactId>
<groupId>javax.jmsgroupId>
exclusion>
<exclusion>
<artifactId>mailartifactId>
<groupId>javax.mailgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.1.6.RELEASEversion>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.5version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.6.1version>
dependency>
<dependency>
<groupId>com.github.adyliugroupId>
<artifactId>zkclientartifactId>
<version>2.0version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.3.6version>
dependency>
<dependency>
<groupId>com.minggroupId>
<artifactId>dubboserverartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
dependencies>
project>
applicationConsumer.xml
<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>
测试类:
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();
}
}
还有一种方式是将Zookeeper作为注册中心,需要下载并安装zookeeper。将配置文件中的注册中心修改为如下即可:
其他代码不用修改,推荐使用zookeeper作为注册中心。如此可以通过Dubbo+注册中心实现分布式服务。服务消费者不用知道服务的具体位置,只要知道注册中心位置即可,消费者只要能消费,不用管消费的是哪的服务,服务提供方与服务消费方解耦。
使用zookeeper注册中心,与dubbo结合,可以查看服务提供者和消费者的信息,将dubbo管理的war包部署到tomcat上,查看服务提供者