1. Environment
Zookeeper is installed on the machine 192.168.0.4 machine for Dubbo Service registration, installation tutorials in another blog
Http://www.cnblogs.com/520playboy/p/6235415.html
2. Production End
2.1. Project Structure
2.2, 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.mujourney</groupId> <artifactId> Dubbo-provider</artifactid> <version>0.0.1-SNAPSHOT</version> <name>dubbo-provider</n ame> <url>http://maven.apache.org</url> <properties> <project.build.sourceencodin g>utf-8</project.build.sourceencoding> </properties> <dependencies> <DEPENDENCY&G
T <groupId>com.mujourney</groupId> <artifactId>dubbo-service</artifactId> &L t;version>0.0.1-snapshot</version> </dependency> <dependency> <groupid >org.apache.zookeeper</groupid> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency>
; <dependency> <groupId>com.alibaba</groupId> <artifactid>dubbo</artifact
id> <version>2.5.3</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <versi on>0.5</version> </dependency> </dependencies> <build> <plugins>
; <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactid>m Aven-compiler-plugin</artifactid> <version>3.3</version> <configurati On> <source>1.7</source> <target>1.7</target> <verbose>true</verbose> </configuration> </plugi n> </plugins> </build> </project>
2.3, Userserviceimpl.java
package com.mujourney.service;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Com.alibaba.dubbo.config.annotation.Service; @Service (interfacename= "Com.mujourney.service.UserService", timeout=10000) public class Userserviceimpl implements
UserService {public map<string, object> FindByID (int id) {return get (ID); Public list<map<string, Object>> querylist () {list<map<string, object>> List = new
Arraylist<map<string, object>> ();
for (int i = 1; I <=. i++) {List.add (Get (i));
} return list; Private map<string, object> get (int id) {map<string, object> res = new hashmap<string, Obje
Ct> ();
Res.put ("id", id);
Res.put ("Name", "1");
Res.put ("type", "1");
Res.put ("Date", "1");
return res; }
}
2.4, Startserver.java
Package com.mujourney.service;
Import Java.util.concurrent.TimeUnit;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class StartServer {public
static void Main (string[] args) {
Classpathxmlapplicationcontext context = new C Lasspathxmlapplicationcontext (
new string[] {"Application-provider.xml"});
Context.start ();
System.out.println ("=========dubbo producer completed ========");
try {
TimeUnit.HOURS.sleep (1l);
} catch (Interruptedexception e) {
e.printstacktrace ();
}
}}
2.5, Application-provider.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmln
S: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/sp Ring-beans-4.0.xsd Http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/ Dubbo.xsd "> <!--Specify Dubbo service name--> <dubbo:application name=" My_provider "/> < !--use multicast broadcast registry to expose service addresses--> <!--<dubbo:registry protocol= "zookeeper" address= "zookeeper://192.168.18.2"
5:2181 "/>--> <dubbo:registry protocol=" zookeeper "address=" zookeeper://192.168.0.4:2181 "/>" <!--Dubbo protocol in 20880-port exposure--> <dubbo:protocol name= "Dubbo" port= "20880"/> <!--specific implementation bean --> <!--<bean id= "UserService" class= "Com.mujourney.service.UserServiceImpl"/>--> <!--Declare service interfaces that need to be exposed--> <!--<dubbo:service interface= "Com.mujourney.service.UserService" ref= "UserService"/>--> <! --Scan the note package path, multiple packages comma separated, not fill pacakge to represent the current ApplicationContext in all classes--> <dubbo:annotation package= " Com.mujourney.service "></dubbo:annotation> </beans>
Because the service is created using annotations, the creator's configuration file is created with the Userserviceimpl class and the specified interface annotated
3, Consumer end
3.1. Project structure
3.2, 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.mujourney</groupId> <artifactId> Dubbo-consumer</artifactid> <version>0.0.1-SNAPSHOT</version> <name>dubbo-consumer</n ame> <url>http://maven.apache.org</url> <properties> <spring.version>3.2.4.rele Ase</spring.version> <junit.version>4.8.2</junit.version> <project.build.sourceencodi ng>utf-8</project.build.sourceencoding> </properties> <dependencies> <dependency&
Gt <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version& Gt;2.5.3</version> ≪exclusions> <exclusion> <artifactId>spring</artifactId>
<groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupid>org.apache.zookee Per</groupid> <artifactId>zookeeper</artifactId> <version>3.4.6</versio n> </dependency> <dependency> <groupid>com.github.sgroschupf</groupid&
Gt
<artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!--related Dubbo-service project source--> <dependency> <groupid>com.mujourney</groupid > <artifactId>dubbo-service</artifactId> <version>0.0.1-snapshot</version&
Gt </dependency> <dependency> <groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId> <version>${spring.version}</version> <type>jar</type> <optional>false</optional> </dependency> & Lt;dependency> <groupId>org.springframework</groupId> <artifactid>spring-bean
S</artifactid> <version>${spring.version}</version> <type>jar</type> <optional>false</optional> </dependency> <dependency>
;groupid>org.springframework</groupid> <artifactId>spring-context</artifactId> <version>${spring.version}</version> <type>jar</type> <OPTIONAL>FA
Lse</optional> </dependency> <dependency> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> <type>jar</type> </dependency> <depe
Ndency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> <type>j ar</type> </dependency> <dependency> <groupId>org.springframework< /groupid> <artifactId>spring-context-support</artifactId> <version>3.0.5.rele ase</version> <optional>false</optional> </dependency> </depen Dencies> <build> <plugins> <plugin> <groupid>org.apache.maven.plugins</grou Pid> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</
Version> <configuration> <source>1.7</source> <target>1.7</target> <verbose>true</verbose> </configurati on> </plugin> </plugins> </build> </project>
3.3, Application-consumer.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-4.0.xsd
Http://code.alibabatech.com/schema/dubbo
http:// Code.alibabatech.com/schema/dubbo/dubbo.xsd ">
<!--Specify the creator-->
<dubbo:application name=" My_ Provider "/>
<!--use multicast broadcast registry to expose service address-->
<dubbo:registry address=" zookeeper:// 192.168.0.4:2181 "/>
<!--scan annotation package path, multiple packages comma separated, not filled package means scanning all classes in the current ApplicationContext-->
< Dubbo:annotation package= "Com.mujourney.service" ></dubbo:annotation>
</beans>
3.4, Clienttest.java
Package com.mujourney.service;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.springframework.stereotype.Component;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Import com.alibaba.dubbo.config.annotation.Reference;
@Component
@RunWith (springjunit4classrunner.class)
@ContextConfiguration (locations={"/ Application-consumer.xml "}" public
class Clienttest {
@Reference (interfacename=) Com.mujourney.service.UserService ")
private userservice uservice;
@Test public
void Pors () {
System.out.println (Uservice.findbyid (1));
}
}
4, the Service interface end
4.1. Project Structure
4.2 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.mujourney</groupId> <artifactId> dubbo-service</artifactid> <version>0.0.1-SNAPSHOT</version> <dependencies> <d
Ependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency&
Gt </dependencies> <build> <plugins> <plugin> < Groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifact Id> <version>3.3</versiOn> <configuration> <source>1.7</source> <target>1.7</target> <verbose>true</verbose> </config uration> </plugin> <plugin> <groupid>org.apache.mave N.plugins</groupid> <artifactId>maven-source-plugin</artifactId>
;version>2.4</version> <executions> <execution>
<id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> &L
t;/goals> </execution> </executions> </plugin> </pluginS> </build> </project>
4.3, Userservice.java
Package com.mujourney.service;
Import java.util.List;
Import Java.util.Map;
Public interface UserService {public
map<string, object> findbyid (int id);
Public list<map<string, Object>> querylist ();
}
The reason why there is a service interface, because both producers and consumers to use the interface, the interface as a single MAVEN Java project can be placed into the private library to the production side and the consumer end of the same use.