Provider和Consumer的搭建(六)
创建三个Maven Project:
- dubbo-service:公共模块,包括服务接口(packaging:jar)
- dubbo-service-impl:服务提供方,提供服务接口的具体实现,需要依赖dubbo-service(packaging:jar)
- dubbo-consumer:服务调用方,需要依赖dubbo-service(packaging:jar/war)
一、dubbo-service
public interface DemoService {
public String demo(String name);
}
二、dubbo-service-impl
1. pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.linhw.demo</groupId>
<artifactId>dubbo-service-impl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.linhw.demo</groupId>
<artifactId>dubbo-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency> <!-- 访问 zookeeper 的客户端 jar -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
</project>
2. 接口实现类
public class DemoServiceImpl implements DemoService{
@Override
public String demo(String name) {
return "dubbo RPC " + name;
}
}
3. 新增配置文件dubbo-provider.xml
<?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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 给当前provider自定义一个名字 -->
<dubbo:application name="demo-provider"/>
<!-- 配置注册中心 -->
<dubbo:registry address="192.168.178.5:2181" protocol="zookeeper"/>
<!-- 配置协议及端口 -->
<dubbo:protocol name="dubbo" port="28888"/>
<!-- 注册功能 -->
<bean id="demoService" class="com.linhw.demo.service.impl.DemoServiceImpl"/>
<dubbo:service interface="com.linhw.demo.service.DemoService" ref="demoService"/>
</beans>
4. 启动容器
(1) 通过 spring 方式启动:对dubbo-provider.xml的位置没有要求
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("dubbo-provider.xml");
ctx.start();
System.out.println("启动成功");
System.in.read();
(2) 使用 dubbo 提供的方式启动(推荐使用这种方式):要求 dubbo-provider.xml必须放入类路径下/META-INF/spring/*.xml
Main.main(args);
查看是否发布成功,可以启动Dubbo Admin,在管理控制台查看。
三、dubbo-consumer
1. 服务调用
import com.alibaba.dubbo.config.annotation.Reference;
import com.linhw.demo.service.DemoService;
import com.linhw.demo.service.TestService;
public class TestServiceImpl implements TestService{ @Reference
private DemoService demoService; @Override
public String sayHello(String name) {
name = "hello";
return demoService.demo(name);
}
}
2. 新建applicationContext-dubbo.xml
<?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-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 给当前 Provider 自定义个名字 -->
<dubbo:application name="dubbo-consumer"/>
<!-- 配置注册中心 -->
<dubbo:registry address="192.168.178.5:2181" protocol="zookeeper"/> <!-- 配置注解扫描 -->
<dubbo:annotation package="com.linhw.demo.service.impl"/> <bean id="testServiceImpl" class="com.linhw.demo.service.impl.TestServiceImpl"/> </beans>
3. 测试
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-dubbo.xml");
TestService testService = ctx.getBean("testServiceImpl", TestServiceImpl.class);
System.out.println(testService.sayHello("hello"));
Provider和Consumer的搭建(六)的更多相关文章
- 关于dubbo的provider和consumer都配置timeout超时时间的情况
本文转自:http://blog.csdn.net/lkforce/article/details/54380201 前言 在dubbo的provider和consumer的配置文件中,如果都配置了t ...
- 嵌入式框架Zorb Framework搭建六:定时器的实现
我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...
- provider和consumer配置参数的优先级
<dubbo:service>和<dubbo:reference>存在一些相同的参数,例如:timeout,retries等,那么哪个配置的优先级高呢? consumer合并u ...
- spring cloud microservice provider and consumer
MicroService Provider:https://files.cnblogs.com/files/xiandedanteng/empCloud190824.rarMicroService C ...
- Spring+SpringMVC+MyBatis深入学习及搭建(六)——MyBatis关联查询
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6923464.html 前面有将到:Spring+SpringMVC+MyBatis深入学习及搭建(五)--动 ...
- Unity 游戏框架搭建 (六) 关于框架的一些好文和一些思考
在进行项目架构阶段,游戏框架可以解决一部分问题.剩下的架构问题还需要根据不同的项目解决.总之游戏框架是游戏架构的一部分. 关于锤子和钉子: 最近又拿起了<代码大全>和<暗时间 ...
- IDEA项目搭建六——使用Eureka和Ribbon进行项目服务化
一.Eureka的作用 这里先简单说明使用eureka进行业务层隔离,实现项目服务化也可以理解为微服务,我一直崇尚先实现代码再学习理论,先简单上手进行操作,eureka使用分为三块,1是服务注册中心, ...
- Jenkins+PowerShell持续集成环境搭建(六)参数化构建
参数化构建可以应用于动态绑定源码地址等情况. 勾选“This build is parameterized”: 如果需要动态绑定源码地址,参考: 配置完成后构建项目变成:
- Linux_CentOS-服务器搭建 <六>
修改MySQL编码: 二话不说先登录: mysql -u root -p 查看下神奇的mysql系统变量及其值: show variables like '%character%'; //记住分号哦, ...
随机推荐
- React内容
React Fiber 16版本 registerServiceWorker 的作用 PWA progressive web application 写手机app应用 在断网的情况下,第二 ...
- Vue.js 创建第一个应用
VUE官网下载Vue.js文件或者用Vue的CDN地址 在项目中引入Vue.js文件 代码: <!doctype html> <html lang="en"> ...
- 剑指offer:滑动窗口的最大值(栈和队列)
1. 题目描述 /* 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值. 例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别 ...
- css样式重置 移动端适配
css 默认样式重置 @charset "utf-8"; *{margin:0;padding:0;} img {border:none; display:block;} em, ...
- pytest 常见用法
前言 之前一篇文章简单介绍了 pytest 以及 fixture :https://www.cnblogs.com/shenh/p/11572657.html .实际在写自动化测试脚本中,还会有一些很 ...
- 一个人的公众号,我写了1w+
大家好,我是Bypass,一个人一直保持着写博客的习惯,为此维护了一个技术公众号,致力于分享原创高质量干货,写的内容主要围绕:渗透测试.WAF绕过.代码审计.应急响应.企业安全. 一直以来,我把它当成 ...
- Window权限维持(三):服务
如果未正确配置Windows环境中的服务或这些服务可以用作持久性方法,则这些服务可能导致权限提升.创建一个新的服务需要管理员级别的特权,它已经不是隐蔽的持久性技术.然而,在红队的行动中,针对那些在威胁 ...
- Git以及GitHub的一些基本使用
1:注册GitHub账号: https://github.com/ 2:Git bash工具下载地址 https://gitforwindows.org/ 3:怎么在GitHub 新增 SSH Key ...
- Windows 10应答文件
将其保存为autounattend.xml文件,然后放入到ISO镜像根目录即可 <?xml version="1.0" encoding="utf-8"? ...
- javaweb里html的一些基本代码意义(学)
<html> <head> <title>body.text属性示例</title> </head> <body text=" ...