dubbo API的使用方式
本文使用maven方式
1:pom文件
<dependencies>
<!-- 引入spring的jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.2.RELEASE</version>
</dependency> <!-- 引入zk -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
</dependency> <!-- 引入dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency> </dependencies>
2:服务端
//服务端的代码
public class DubboServer { public static void main(String[] args) throws IOException {
initServer();
} public static void initServer() throws IOException {
//设置应用名称
ApplicationConfig config = new ApplicationConfig();
config.setName("dubboAppServer"); //连接注册中心
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("192.168.30.128:2181"); //本地虚拟机的地址
registryConfig.setProtocol("zookeeper"); //设置协议
ProtocolConfig protocolConfigRmi = new ProtocolConfig();
protocolConfigRmi.setPort(12880);
protocolConfigRmi.setName("rmi"); //设置rmi的协议 ProtocolConfig protocolConfigDubbo = new ProtocolConfig();
protocolConfigDubbo.setPort(12881);
protocolConfigDubbo.setName("dubbo"); //设置dubbo的协议 //服务提供者暴露服务
ServiceConfig<OrderService> serviceServiceConfig = new ServiceConfig<OrderService>();
serviceServiceConfig.setApplication(config); //设置应用名称
serviceServiceConfig.setRegistry(registryConfig); //设置注册中心
serviceServiceConfig.
setProtocols(Arrays.asList(protocolConfigRmi,protocolConfigDubbo)); //设置两个协议 serviceServiceConfig.setInterface(OrderService.class); //设置接口
serviceServiceConfig.setRef(new OrderServiceImpl()); //设置具体实现类 serviceServiceConfig.export(); //暴露和注册服务 System.in.read(); }
3:消费端
//消费端代码
public class DubboConsumer { public static void main(String[] args) {
initConsumer();
} public static void initConsumer(){
//设置应用名称
ApplicationConfig config = new ApplicationConfig();
config.setName("dubboAppConsumer"); //连接注册中心
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("192.168.30.128:2181"); //本地虚拟机的地址
registryConfig.setProtocol("zookeeper"); ReferenceConfig<OrderService> referenceConfig = new ReferenceConfig<OrderService>();
referenceConfig.setApplication(config);
referenceConfig.setRegistry(registryConfig);
referenceConfig.setInterface(OrderService.class);
referenceConfig.setProtocol("dubbo"); OrderService order = referenceConfig.get();
Integer num = order.buyShop(); //具体的调用
referenceConfig.destroy(); System.out.println(num); }
dubbo API的使用方式的更多相关文章
- 关于RESTFUL API 安全认证方式的一些总结
常用认证方式 在之前的文章REST API 安全设计指南与使用 AngularJS & NodeJS 实现基于 token 的认证应用两篇文章中,[译]web权限验证方法说明中也详细介绍,一般 ...
- 关于 RESTFUL API 安全认证方式的一些总结
常用认证方式 在之前的文章REST API 安全设计指南与使用 AngularJS & NodeJS 实现基于 token 的认证应用两篇文章中,[译]web权限验证方法说明中也详细介绍,一般 ...
- dubbo服务启动的方式
dubbo服务启动的方式: 1.dubbo自带的脚本, 2.直接用main方法启动dubbo的spring容器,参见dubbo-test里的各个例子 3.dubbo的spring boot start ...
- 利用SparkLauncher 类以JAVA API 编程的方式提交Spark job
一.环境说明和使用软件的版本说明: hadoop-version:hadoop-2.9.0.tar.gz spark-version:spark-2.2.0-bin-hadoop2.7.tgz jav ...
- 深入了解Kubernetes REST API的工作方式
关于Kubernetes REST API的工作方式: 在哪里以及如何定义从REST路径到处理REST调用的函数的映射? 与etcd的交互发生在哪里? 从客户端发出请求到保存在etcd中对象的端到端路 ...
- 几种部署Goku API Gateway的方式,最快一分钟可使用上网关
本文将介绍几种部署Goku API Gateway的方式,最快一分钟可使用上为网关,详情请看全文. 什么是Goku API Gateway? Goku API Gateway (中文名:悟空 API ...
- RESTFUL API 安全认证方式
一般基于REST API 安全设计常用方式有: HTTP Basic Basic admin:admin Basic YWRtaW46YWRtaW4= Authorization: Basic YWR ...
- java:struts框架2(方法的动态和静态调用,获取Servlet API三种方式(推荐IOC(控制反转)),拦截器,静态代理和动态代理(Spring AOP))
1.方法的静态和动态调用: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...
- dubbo学习(三)配置dubbo API方式配置
provider(生产者) import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.Pro ...
随机推荐
- 123457123456#5#----com.MC.HuaHuaGame866----前拼后广--babyDrawGame-mc555
com.MC.HuaHuaGame866----前拼后广--babyDrawGame-mc
- 今天被这个BDE错误搞了半天,不过终于好了,分享一下
今天正编译程序时,突然就报了这个错误出来,重启电脑都没用,多亏网上高手指教,先把解决方案列于下,供受此累得朋友查阅,自己也留底供查找:"Shared memory conflict ($21 ...
- PLSQL集合类型的使用总结
PLSQL集合类型的使用总结 在pl sql 中,集合(collection) 是一组有序的元素组成的对象,这些元素的类型必须一致. pl sql 将collection 分成3 类,分别为Assoc ...
- 【docker 使用】创建镜像docker build
创建镜像命令 docker build -t [镜像name]:[镜像tag] -f [dockerfile路径] [资源目录] 执行该命令后,docker会将[资源目录]打包,之后在dockerfi ...
- The underlying connection was closed: The connection was closed unexpectedly.
基础连接已经关闭: 连接被意外关闭. 基础连接已经关闭: 发送时发生错误 防火墙问题.或是杀毒软件,卫士之类的.(360 卸载 )
- 解决ubuntu的firefox上网速度慢【转】
在ubuntu上用firefox上网十分慢,但是在切换了chrome后发现上网速度很快,是解析域名上出现了问题,所以要为FF设置DNS缓存以提高速度.(在WIN下这个是自动设置好的,在ubuntu下需 ...
- Django 操作已经存在的数据库
反向操作数据库 何为反向操作.即是数据库在项目之前已经存在,不需要新建表,操作已经存在的表 # 进入站点目录下执行 python manage.py inspectdb #可以看到settings中连 ...
- 走进热修复框架AndFix的世界
关于阿里的开源热修复框架AndFix引起了广泛共鸣,受到了很多人的青睐.那今天就跟随我的步伐来详细了解一下AndFix的详细信息和使用方法.1.什么是AndFix? AndFix是阿里巴巴出的一个专门 ...
- Google BERT
概述 BERT的全称是Bidirectional Encoder Representation from Transformers,即双向Transformer的Encoder,因为decoder是不 ...
- LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)
783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的 ...