先不管什么原理,直接写个demo。
服务端(dubbo-server)
1. pom.xml
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.0modelVersion>
<groupId>com.lwbgroupId>
<artifactId>dubbo-serverartifactId>
<version>1.0-SNAPSHOTversion>
<packaging>jarpackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.1.6.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.5.3version>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.3.6version>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.1version>
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.jboss.nettygroupId>
<artifactId>nettyartifactId>
<version>3.2.0.Finalversion>
dependency>
<dependency>
<groupId>com.101tecgroupId>
<artifactId>zkclientartifactId>
<version>0.8version>
dependency>
dependencies>
project>
2. dubbo.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-server" owner="lwb" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol contextpath="dubbo" port="20881" />
<dubbo:monitor protocol="registry"/>
<bean id="elasticService" class="com.lwb.service.impl.ElasticServiceImpl" />
<dubbo:service interface="com.lwb.service.ElasticService" ref="elasticService" protocol="dubbo" timeout="50000"/>
beans>
3. 代码
package com.lwb.service;
/**
* @author liuweibo
* @date 2018/5/9
*/
public interface ElasticService {
String helloDubbo(String msg);
}
package com.lwb.service.impl; import com.lwb.service.ElasticService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; /** * @author liuweibo * @date 2018/5/9 */ public class ElasticServiceImpl implements ElasticService { private static final Logger LOGGER = LoggerFactory.getLogger(ElasticServiceImpl.class); public String helloDubbo(String msg) { System.out.println(msg); return "hi!" + msg; } }
4. 项目结构
5. 启动类(Application.java)
这里就直接用main方法启动了
package com.lwb;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* @author liuweibo
* @date 2018/5/9
*/
public class Application {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "dubbo.xml" });
context.start();
System.out.println("任意按键退出");
System.in.read();
context.close();
}
}
客户端(dubbo-client)1. pom.xml
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.0modelVersion> <groupId>com.lwbgroupId> <artifactId>dubbo-clientartifactId> <version>1.0-SNAPSHOTversion> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> properties> <dependencies> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>4.1.6.RELEASEversion> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>dubboartifactId> <version>2.5.3version> dependency> <dependency> <groupId>org.apache.zookeepergroupId> <artifactId>zookeeperartifactId> <version>3.3.6version> 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>com.101tecgroupId> <artifactId>zkclientartifactId> <version>0.8version> dependency> <dependency> <groupId>com.lwbgroupId> <artifactId>dubbo-serverartifactId> <version>1.0-SNAPSHOTversion> dependency> dependencies> project>
2. dubbo.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-dubbo-demo" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:reference id="elasticService" interface="com.lwb.service.ElasticService" />
beans>
3. 服务调用(这里为了简单,就在main方法里面处理了哈,Application.java)
package com.lwb;
import com.lwb.service.ElasticService;
import com.lwb.service.impl.ElasticServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author liuweibo
* @date 2018/5/9
*/
public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "dubbo.xml" });
context.start();
ElasticService service = (ElasticService) context.getBean("elasticService");
System.out.println(service.helloDubbo("world"));
context.close();
}
}
4. 项目结构