该例子用maven来构建,客户端和服务端之间采用组播技术进行通信,并未使用zookeeper。
服务端代码:
服务端代码结构:
服务端pom文件:
<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.huangjie</groupId>
<artifactId>dubbo-Service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dubbo-Service</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</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.18.1-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</artifactId>
<version>2.5.6.SEC03</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</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.4.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.4</version>
</dependency>
</dependencies>
</project>
服务端配置文件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
">
<!-- Application name -->
<dubbo:application name="hello-world-app" />
<!-- registry address, used for service to register itself -->
<dubbo:registry address="multicast://224.5.6.7:1234" />
<!-- expose this service through dubbo protocol, through port 20880 -->
<!--
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:protocol name="dubbo" port="9090" server="netty"
client="netty" codec="dubbo" serialization="hessian2" charset="UTF-8"
threadpool="fixed" threads="100" queues="0" iothreads="9" buffer="8192"
accepts="1000" payload="8388608" />
-->
<!-- Service interface Concurrent Control -->
<dubbo:service interface="com.huangjie.dubbo_Service.service.IProcessData"
ref="demoService" executes="10" />
<!-- Default Protocol -->
<!--
<dubbo:protocol server="netty" />
-->
<!-- designate implementation -->
<bean id="demoService" class="com.huangjie.dubbo_Service.service.impl.ProcessDataImpl" />
</beans>
服务端接口代码:
package com.huangjie.dubbo_Service.service;
public interface IProcessData {
public String deal(String data);
}
服务度接口实现代码:
package com.huangjie.dubbo_Service.service.impl;
import com.huangjie.dubbo_Service.service.IProcessData;
public class ProcessDataImpl implements IProcessData {
/*
* @see com.xxx.bubbo.provider.IProcessData#deal(java.lang.String)
*/
@Override
public String deal(String data) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Finished:" + data;
}
}
启动服务端代码:
package com.huangjie.dubbo_Service;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DubboProviderMain {
/**
* @Title main
* @Description TODO
* @Author weizhi2018
* @param args
* @throws
*/
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"applicationProvider.xml"});
context.start();
System.out.println("Press any key to exit.");
System.in.read();
}
}
消费端代码:
消费端代码结构:
消费端pom文件:
<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.huangjie</groupId>
<artifactId>dubbo-Consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dubbo-Consumer</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</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.18.1-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</artifactId>
<version>2.5.6.SEC03</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</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.4.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.huangjie</groupId>
<artifactId>dubbo-Service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
消费端配置文件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
">
<!-- consumer application name -->
<dubbo:application name="consumer-of-helloworld-app" />
<!-- registry address, used for consumer to discover services -->
<dubbo:registry address="multicast://224.5.6.7:1234" />
<dubbo:consumer timeout="5000"/>
<!-- which service to consume? -->
<dubbo:reference id="demoService" interface="com.huangjie.dubbo_Service.service.IProcessData" />
</beans>
消费端代码:
package com.huangjie.dubbo_Consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.huangjie.dubbo_Service.service.IProcessData;
public class ConsumerThd implements Runnable {
/*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"applicationConsumer.xml"});
context.start();
IProcessData demoService = (IProcessData) context.getBean("demoService"); // get
// service
// invocation
// proxy
String hello = demoService.deal("nihao"); // do invoke!
System.out.println(Thread.currentThread().getName() + " "+hello);
}
public static void main(String[] args) {
new Thread(new ConsumerThd()).start();
}
}