用Dubbo+zookeeper 调用java接口程序
2017-01-08 17:34
405 查看
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&
9d3e
gt;
</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下载地址
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&
9d3e
gt;
</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下载地址
相关文章推荐
- java程序实现短信发送(可调用免费短信接口)
- 通过dubbo暴露接口调用方法,及基于zookeeper的dubbo涉及配置文件【转】
- 通过dubbo+zookeeper实现服务接口的封装及调用
- 用dubbo+zookeeper+spring搭建一个简单的http接口程序
- dubbo+zookeeper 简单实现远程接口调用
- zookeeper结合dubbo实现分布式接口调用
- 通过dubbo暴露接口调用方法,及基于zookeeper的dubbo涉及配置文件
- java程序调用.net接口服务地址的写法
- 通过dubbo暴露接口调用方法,及基于zookeeper的dubbo涉及配置文件【转】
- 通过dubbo暴露接口调用方法,及基于zookeeper的dubbo涉及配置文件【转】
- 通用权限管理系统多语言开发接口 - java,php 调用接口程序,多业务子系统集成
- 通过dubbo暴露接口调用方法,及基于zookeeper的dubbo涉及配置文件【转】
- 通过dubbo暴露接口调用方法,及基于zookeeper的dubbo涉及配置文件【转】
- 通过dubbo暴露接口调用方法,及基于zookeeper的dubbo涉及配置文件【转】
- 通过dubbo暴露接口调用方法,及基于zookeeper的dubbo涉及配置文件
- webservice实际应用之调用外部系统提供的服务端地址,写java的webservice客户端程序并通过接口给外部系统传数据
- java微信小程序调用支付接口
- 通过dubbo暴露接口调用方法,及基于zookeeper的dubbo涉及配置文件
- java通过jni调用C程序接口
- 通过dubbo暴露接口调用方法,及基于zookeeper的dubbo涉及配置文件