创建三个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的搭建(六)的更多相关文章

  1. 关于dubbo的provider和consumer都配置timeout超时时间的情况

    本文转自:http://blog.csdn.net/lkforce/article/details/54380201 前言 在dubbo的provider和consumer的配置文件中,如果都配置了t ...

  2. 嵌入式框架Zorb Framework搭建六:定时器的实现

    我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...

  3. provider和consumer配置参数的优先级

    <dubbo:service>和<dubbo:reference>存在一些相同的参数,例如:timeout,retries等,那么哪个配置的优先级高呢? consumer合并u ...

  4. spring cloud microservice provider and consumer

    MicroService Provider:https://files.cnblogs.com/files/xiandedanteng/empCloud190824.rarMicroService C ...

  5. Spring+SpringMVC+MyBatis深入学习及搭建(六)——MyBatis关联查询

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6923464.html 前面有将到:Spring+SpringMVC+MyBatis深入学习及搭建(五)--动 ...

  6. Unity 游戏框架搭建 (六) 关于框架的一些好文和一些思考

      在进行项目架构阶段,游戏框架可以解决一部分问题.剩下的架构问题还需要根据不同的项目解决.总之游戏框架是游戏架构的一部分. 关于锤子和钉子:   最近又拿起了<代码大全>和<暗时间 ...

  7. IDEA项目搭建六——使用Eureka和Ribbon进行项目服务化

    一.Eureka的作用 这里先简单说明使用eureka进行业务层隔离,实现项目服务化也可以理解为微服务,我一直崇尚先实现代码再学习理论,先简单上手进行操作,eureka使用分为三块,1是服务注册中心, ...

  8. Jenkins+PowerShell持续集成环境搭建(六)参数化构建

    参数化构建可以应用于动态绑定源码地址等情况. 勾选“This build is parameterized”: 如果需要动态绑定源码地址,参考: 配置完成后构建项目变成:

  9. Linux_CentOS-服务器搭建 <六>

    修改MySQL编码: 二话不说先登录: mysql -u root -p 查看下神奇的mysql系统变量及其值: show variables like '%character%'; //记住分号哦, ...

随机推荐

  1. luoguP4254 [JSOI2008]Blue Mary开公司

    题意 李超树裸题,注意一开始截距是\(S-P\). code: #include<bits/stdc++.h> using namespace std; #define ls(p) (p& ...

  2. php 学习笔记之日期时间操作一箩筐

    格式化日期时间 date : 格式化日期时间 场景 将当前日期时间或者特定日期时间格式化输出为特定格式的字符串,常用于人性化展示信息. 说明 返回给定时间戳格式化后所产生的日期时间字符串,如果没有给出 ...

  3. Python thread & process

    线程 点击查看 <- 进程 点击查看 <- 线程与进程的区别 线程共享内部空间:进程内存空间独立 同一个进程的线程之间可以直接交流:两个进程之间想通信必须通过一个中间代理 创建新线程很简单 ...

  4. 使用Sourcetree(for windows)建立github同步仓库

    回顾Linux作业中用命令行的方式管理本地与远程仓库的过程,原理图如下: 与之前本质相同,但是使用GUI的github管理工具将更为便捷,Sourcetree就是其中之一 1.在Windows下安装g ...

  5. Note | 期刊答复(response)

    第一次TPAMI回复整理的经验. 190926:TPAMI中了,特回来补充. 简洁 尤其对于问答型问题,一定要在前三句话就回答审稿人的问题:yes or no. 不要绕弯子,专注于审稿人问题,解决就行 ...

  6. (三十二)golang--面向对象之封装

    封装:把抽象出来的字段和对字段的操作封装在一起,数据被保护在内部,程序的其它包只有通过被授权的操作(方法),才能对字段进行操作. 封装的好处: (1)隐藏实际的细节: (2)可以对数据进行验证,保证安 ...

  7. BFS(四):搜索状态判重

    在采用广度优先算法进行搜索时,一个需要重点注意的是在搜索过程中判重和去重.前面介绍的几个例子中,判重都较简单,如采用vis[]数组,若vis[i]==0,则i未访问过,i入队列:若vis[i]!=0, ...

  8. Comment file

    /// This is the head comment of a file. /*********************************************************** ...

  9. 机器学习(十一)-------- 异常检测(Anomaly Detection)

    异常检测(Anomaly Detection) 给定数据集

  10. 【论文阅读】Objects as Points 又名 CenterNet | 目标检测

    目录 Abstract Instruction 分析 CenterNet 的Loss公式 第一部分:\(L_k\) 第二部分:\(L_{size}\) 第三部分:\(L_{off}\) Abstrac ...