创建三个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. LG2444/BZOJ2938 「POI2000」病毒 AC自动机

    问题描述 LG2444 BZOJ2938 I \(\mathrm{AC}\)自动机 \(\mathrm{AC}\)自动机是一种多模式串匹配算法,本萌新今天刚学了它qwq 约定在构造\(\mathrm{ ...

  2. win10 去除快捷方式小箭头

    @echo off color 2 reg delete HKCR\lnkfile /v IsShortcut /f reg delete HKCR\piffile /v IsShortcut /f ...

  3. CF1207G Indie Album

    题目链接 problem 有\(n\)个字符串,对于第\(i\)个字符串通过以下两种方式中的一个给出. \(1\; c\),该字符串只含一个字符\(c\). \(2\ x\ c\),该字符串为第\(x ...

  4. Java 未来行情到底如何,来看看各界人士是怎么说的

    这是黄小斜的第102篇文章 作者 l 黄小斜 来源 l 公众号[程序员黄小斜](ID:AntCoder) 转载请联系作者(wx_ID:john_josh) Java从出生到现在已经走过了 20 多个年 ...

  5. Springboot使用ehcache缓存

    本文部分步骤继承于springboot使用cache缓存,如果有不清楚的,请移驾springboot使用cache缓存 ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存,Java ...

  6. jdk自带监控程序jvisualvm的使用

    监控小程序的配置 生产环境tomcat的配置 编辑应用所在的tomcat服务器下的bin目录下的catalina.sh文件,修改如下: 配置如下内容: export JAVA_OPTS="- ...

  7. netstat查看端口状态

    netstat netstat -tunlp 用于显示 tcp,udp 的端口和进程等相关情况. netstat 查看端口占用语法格式: netstat -tunlp | grep 端口号 -t (t ...

  8. MAT 4378 – MAT 5317, Analysis of categorical

    MAT 4378 – MAT 5317, Analysis of categorical data, Assignment 3 1MAT 4378 – MAT 5317, Analysis of ca ...

  9. 阿里云CentOS7.x安装nodejs及pm2

    对之前文章的修订 您将了解 CentOS下如何安装nodejs CentOS下如何安装NVM CentOS下如何安装git CentOS下如何安装pm2 适用对象 本文档介绍如何在阿里云CentOS系 ...

  10. Vue.js 源码分析(二十五) 高级应用 插槽 详解

    我们定义一个组件的时候,可以在组件的某个节点内预留一个位置,当父组件调用该组件的时候可以指定该位置具体的内容,这就是插槽的用法,子组件模板可以通过slot标签(插槽)规定对应的内容放置在哪里,比如: ...