<?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="zookeeper://127.0.0.1:2181" />
<!-- 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>
d、启动服务
public class DubboProviderMain {
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();
}
}
3、服务调用者的工程
a、调用类
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();
/**
* 输出结果:
* Thread-0 Finished:nihao
*/
}
}
b、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="zookeeper://127.0.0.1:2181" />
<dubbo:consumer timeout="5000"/>
<!-- which service to consume? -->
<dubbo:reference id="demoService" interface="com.huangjie.dubbo_Service.service.IProcessData" />
</beans> 4、最后需要把zookeeper的服务给启动,在zookeeper安装文件夹下面的bin目录里面的zkServer.cmd给点击运行。不要关闭窗口,保持服务运行