用Dubbo+zookeeper 调用java接口程序

1.首先需要准备Dubbo和zookeeper的jar包放入到我们提供服务的javaweb项目中  Demo下载地址

2.新建服务段提供服务的  web项目  接口和实现为


public interface SearchService {
public String sayHello(String str);
}


public class SearchServiceImpl implements SearchService,Serializable{


/**
* 必须实现Serializable接口 否则会报错
*/
private static final long serialVersionUID = 1L;


public String sayHello(String str) {
// TODO Auto-generated method stub
return str;
}



}


3.配置application文件和日志文件  内容如下:

<?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
        ">
<!-- 具体的实现bean -->
<bean id="searchService" class="com.zzx.search.impl.SearchServiceImpl"/>

<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="zzx_provider" />


<!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234" 
/> -->


<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />


<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />



<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.zzx.search.SearchService" 
ref="searchService" />


</beans>


4.因为我是web项目 所以需要在web.xml 中监听application文件和日志文件   web.xml中的配置如下


   <!--application 配置文件  -->    
   <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
   <!--检索日志信息  -->
   <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>
  <!--监听器  -->
  <!-- 启用日志服务监听器 -->
   <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
   </listener>
   <!--启用 资源加载监听器  -->
   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
   <!--启用内存溢出监听器  -->  
   <listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
   </listener>

5.至此服务端的程序 就写完了。可以启动web项目啦!不过需要启动zookeeper服务  可以参考我上一篇的文章


6.java客户端调用 服务器

7.在客户端写相同的接口代码 

public interface SearchService {
public String sayHello(String str);
}

切记 接口和接口的路径要与服务器端一致

8.配置consumer.xml文件

<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->


<dubbo:application name="zzx_consumer" />

<!-- 使用zookeeper注册中心暴露服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />


<!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
<dubbo:reference id="searchService"
interface="com.zzx.search.SearchService" />


9.测试客户端调用服务器端的代码


ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "consumer.xml" });

context.start();

SearchService searchService = (SearchService) context.getBean("searchService");
String hello = searchService.sayHello("tom==========================================");
System.out.println(hello);

10.至此全部流程完毕!   Demo下载地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你微笑莳很美丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值