The simplest dubbo application consists of three parts: service provider, service consumer, and registry.
The first two are written by ourselves, and the third registry recommends using zookeeper with better stability.
So we need to download zookeeper first: http://apache.fayea.com/zookeeper/current/
After downloading, unzip it to a certain directory and enter the conf directory inside. Make a copy of zoo_sample.cfg and rename it to zoo.cfg.
Modify its content to
tickTime=2000 initLimit = 10 syncLimit=5 dataDir=D:\\data\\zookeeper clientPort=2181
The dataDir can be freely modified.
Enter the bin directory of zookeeper and run zkServer.cmd
{ To test if it works well run zkCli.cmd }
Next, open eclipse, create a new maven project, and use the default quickstart for arctype.
Modify the pom.xml file:
<dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.0.13</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.6</version> <exclusions> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> </dependencies>
Create a new interface in the Java source folder. This interface should be used by both provider and consumer
public interface DemoService { public void sayHello(); public String returnHello(); public MsgInfo returnMsgInfo(MsgInfo info); }
Create a new implementation class
public class DemoServiceImpl implements DemoService{ public void sayHello() { System.err.println("Hello world."); } public String returnHello() { return "hello world"; } public MsgInfo returnMsgInfo(MsgInfo info) { info.getMsgs().add("done!"); return info; } }
Create a new test entity
public class MsgInfo implements Serializable { int id; String name; List<String> msgs; //getters setters }
Create a new directory spring under the resource folder (although it is not necessary, but we are integrated with spring).
Create a new xml file inside 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"> <!-- provider application information for calculating dependencies --> <dubbo:application name="hello-world-app" /> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- Expose services on port 20880 using the dubbo protocol --> <dubbo:protocol name="dubbo" port="20880" /> <!-- Declare the service interface that needs to be exposed --> <dubbo:service interface="com.abc.qkdb.DemoService" ref="demoService" /> <!-- Implement service like local bean --> <bean id="demoService" class="com.abc.qkdb.DemoServiceImpl" /> </beans>
Among them, <dubbo:registry> is the registry center using zookeeper. For other options, please refer to http://dubbo.io/Administrator+Guide-zh.htm#AdministratorGuide-zh-Redis%E6%B3%A8%E5% 86%8C%E4%B8%AD%E5%BF%83%E5%AE%89%E8%A3%85
Create a new dubbo-consumer.xml in the same directory
<?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"> <!--Consumer application name, used to calculate dependencies, not matching conditions, not the same as the provider --> <dubbo:application name="consumer-of-helloworld-app" /> <!--zookeeper registration center--> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- Generate remote service proxy, you can use demoService like local bean--> <dubbo:reference id="demoService" interface="com.abc.qkdb.DemoService" /> </beans>
Note that it also contains a <dubbo:registry> node.
Go back to the Java folder and create a new service class
import java.io.IOException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class LuncherProvider { public static void main(String[] args) throws InterruptedException, IOException { LuncherProvider provider = new LuncherProvider(); provider.start(); System.in.read(); } void start() { String configLocation = "spring/dubbo-provider.xml"; ApplicationContext context = new ClassPathXmlApplicationContext(configLocation); String[] names = context.getBeanDefinitionNames(); for (String name : names) { System.err.println(name); } } }
Create a new consumer class
import java.util.ArrayList; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class LuncherConsumer { public static void main(String[] args) { LuncherConsumer provider = new LuncherConsumer(); provider.start(); } void start() { String configLocation = "spring/dubbo-consumer.xml"; ApplicationContext context = new ClassPathXmlApplicationContext(configLocation); DemoService ds = (DemoService) context.getBean("demoService"); String[] names = context.getBeanDefinitionNames(); System.out.print("Beans:"); for (String string : names) { System.out.println(string); } MsgInfo info = new MsgInfo(); info.setId(1); info.setName("ruisheh"); List<String> msgs = new ArrayList<String>(); msgs.add("I"); msgs.add("am"); msgs.add("test"); info.setMsgs(msgs); System.out.println(ds.returnMsgInfo(info).getMsgs()); System.err.println(ds.returnHello()); } }
Encoding is complete.
Run the LuncherProvider class first, after there is no error output (the beans in the context will be output), run the LuncherConsumer, and output
[I, am, test, done!]
hello world
That is normal.