本文使用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的使用方式的更多相关文章

  1. 关于RESTFUL API 安全认证方式的一些总结

    常用认证方式 在之前的文章REST API 安全设计指南与使用 AngularJS & NodeJS 实现基于 token 的认证应用两篇文章中,[译]web权限验证方法说明中也详细介绍,一般 ...

  2. 关于 RESTFUL API 安全认证方式的一些总结

    常用认证方式 在之前的文章REST API 安全设计指南与使用 AngularJS & NodeJS 实现基于 token 的认证应用两篇文章中,[译]web权限验证方法说明中也详细介绍,一般 ...

  3. dubbo服务启动的方式

    dubbo服务启动的方式: 1.dubbo自带的脚本, 2.直接用main方法启动dubbo的spring容器,参见dubbo-test里的各个例子 3.dubbo的spring boot start ...

  4. 利用SparkLauncher 类以JAVA API 编程的方式提交Spark job

    一.环境说明和使用软件的版本说明: hadoop-version:hadoop-2.9.0.tar.gz spark-version:spark-2.2.0-bin-hadoop2.7.tgz jav ...

  5. 深入了解Kubernetes REST API的工作方式

    关于Kubernetes REST API的工作方式: 在哪里以及如何定义从REST路径到处理REST调用的函数的映射? 与etcd的交互发生在哪里? 从客户端发出请求到保存在etcd中对象的端到端路 ...

  6. 几种部署Goku API Gateway的方式,最快一分钟可使用上网关

    本文将介绍几种部署Goku API Gateway的方式,最快一分钟可使用上为网关,详情请看全文. 什么是Goku API Gateway? Goku API Gateway (中文名:悟空 API ...

  7. RESTFUL API 安全认证方式

    一般基于REST API 安全设计常用方式有: HTTP Basic Basic admin:admin Basic YWRtaW46YWRtaW4= Authorization: Basic YWR ...

  8. java:struts框架2(方法的动态和静态调用,获取Servlet API三种方式(推荐IOC(控制反转)),拦截器,静态代理和动态代理(Spring AOP))

    1.方法的静态和动态调用: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...

  9. dubbo学习(三)配置dubbo API方式配置

    provider(生产者) import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.Pro ...

随机推荐

  1. Python 初级 5 判断再判断(四)

    一 .复习 分支:完成测试并根据结果做出判断称为分支. 代码块:一行或放在一起的多行代码 缩进:一个代码行稍稍靠右一点 关系操作符(比较操作符):==, >, >=, <, < ...

  2. Python - Django - 页面上展示固定的页码数

    如果页数太多的话,全部显示在页面上就会显得很冗杂 可以在页面中显示规定的页码数 例如: book_list.html: <!DOCTYPE html> <html lang=&quo ...

  3. LeetCode_242. Valid Anagram

    242. Valid Anagram Easy Given two strings s and t , write a function to determine if t is an anagram ...

  4. redis cluster环境搭建

    环境搭建 http://blog.51cto.com/zhoushouby/1560400 http://hot66hot.iteye.com/blog/2050676 ruby环境安装---ruby ...

  5. Nginx之开启压缩

    参考:https://blog.csdn.net/php12345679/article/details/80843939 https://blog.csdn.net/pf1234321/articl ...

  6. 常见问题:MySQL/B+树

    平衡二叉树 此前讲红黑树时也提到了平衡二叉树,红黑树和AVL树都是能保证树不退化的平衡二叉树,平衡二叉树采用二分思想组织数据,能大大提高单点查找数据的效率,其组装过程略. 作为对比,此处也列出平衡二叉 ...

  7. 反馈神经网络Hopfield网络

    一.前言 经过一段时间的积累,对于神经网络,已经基本掌握了感知器.BP算法及其改进.AdaLine等最为简单和基础的前馈型神经网络知识,下面开启的是基于反馈型的神经网络Hopfiled神经网络.前馈型 ...

  8. eNSP基于接口地址池的dhcp服务

    拓扑图如下 基于接口的dhcp是最简单的一种 我们对路由器的两个端口分别设置ip地址为192.168.1.254 192.168.2.254 然后分别进入接口进行下一步配置 dhcp select i ...

  9. 在Jetty中部署Jenkins遇到的问题

    1. Jetty 9.0.3 启动时的错误: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 [root@kvm-guest jetty-9.0.3]# java -jar star ...

  10. CSS float属性

    表示向左浮动,比如多个div在一个页面上,默认情况是:一行一个div,但是只要在div的css中使用float:left,可以使一行有多个div,这样可以把网页划分成很多块,但是使用该属性会影响后面的 ...