简单的基于dubbo的调用
1首先下载zookeeper-3.4.6,修改zoo.cfg下的配置信息,然后启动zookeeper2编写服务端package com.mor.server.dubbo.service;//方法接口public interface DemoServer {String sayHello(String str);}/****/package com.mor.ser
·
1首先下载zookeeper-3.4.6,修改zoo.cfg下的配置信息,然后启动zookeeper
2编写服务端
package com.mor.server.dubbo.service;
//方法接口
public interface DemoServer {
String sayHello(String str);
}<pre name="code" class="java">/**
*
*/
package com.mor.server.dubbo.service;
import java.util.Date;
//方法实现
public class DemoServerImpl implements DemoServer {
/**
* 返回添加后的语句
*/
public String sayHello(String str) {
str = "Hello " + str + " 2:" + new Date();
System.err.println("server:" + str);
return str;
}
}
/**
*
*/
package com.mor.main;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
//先启动这个 注册到zookpeer里面
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });
context.start();
System.out.println("按任意键退出");
System.in.read();
}
}
<?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="hello-world-app" />
<!-- 本机 伪集群 测试 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<!-- dubbo暴露地址 -->
<dubbo:protocol name="dubbo" port="8088" />
<!-- Dubbo要实现的接口类 -->
<dubbo:service interface="com.mor.server.dubbo.service.DemoServer"
ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.mor.server.dubbo.service.DemoServerImpl" />
</beans>
启动服务端应用
编写客户端代码
package com.mor.client.dubbo.action;
import java.util.Date;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mor.server.dubbo.service.DemoServer;
public class ChatAction {
public void SayHello(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationConsumer.xml" });
context.start();
DemoServer demoServer = (DemoServer) context.getBean("demoService");
System.out.println("client:"+demoServer.sayHello("Wanggq"+"1:"+new Date())+"3:"+new Date());
}
}
package com.mor.client.dubbo.main;
import com.mor.client.dubbo.action.ChatAction;
public class Main {
public static void main(String[] args) throws InterruptedException {
int i=0;
while(i++<100){
ChatAction act = new ChatAction();
System.out.println("--------------------测试1111111------------------------------------");
act.SayHello();
Thread.sleep(3000);
}
}
}
<?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-helloworld-app" /> <!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<!-- dubbo暴露地址 -->
<dubbo:protocol name="dubbo" port="8088" />
<dubbo:reference id="demoService" interface="com.mor.server.dubbo.service.DemoServer" />
</beans>
启动客户端,然后再控制台查看信息
更多推荐
已为社区贡献1条内容
所有评论(0)