SpringBoot与Dubbo整合下篇
(1)pom.xml引入相关依赖jar包,如下:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
(2)编写服务的接口以及实现类,如下:
package springboot.dao;
public interface DubboService {
public String sayHello();
}
接口实现类:
package springboot.dao.imp;
import org.springframework.stereotype.Service;
import springboot.dao.DubboService; @Service("DubboService")
public class DubboServiceImp implements DubboService{ @Override
public String sayHello() { return "Hello Dubbo";
} }
(3)在resources文件下建立服务方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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 服务应用名称 -->
<dubbo:application name="provider"/>
<!-- 使用zookeeper做为注册中心 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://172.31.19.224:2181"/>
<!-- ref中的值要跟服务实现类中的@Server的值一致 -->
<dubbo:service interface="springboot.dao.DubboService" ref="DubboService"></dubbo:service>
</beans>
(4)修改springboot启动类,启动时引入provider.xml文件,使用@ImportResource("classpath:provider.xml"),如下:
package springboot; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication
@EnableScheduling
@EnableCaching
@ImportResource("classpath:consumer.xml")
public class SpringbootApplication extends SpringBootServletInitializer{ public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringbootApplication.class);
}
}
至此,服务方已经配置完毕了,我这里将代码发布到tomcat上。如果想在本地测试话,需要更改一下启动端口,因为后面消费方也需要启动。
(5)接下来我们将服务接口.class文件(即上述的DubboService.class接口)打成jar包,(服务消费方在项目中需要导入该jar包才能调用服务),进入到DubboService.class目录下,启动控制台,输入:

至此,dubbo服务方提供已经开发完毕!
(6)接下来开发服务消费方,将上述jar包导入到项目中。如果是maven项目,还需要将jar继续打包,将上述jar包修改成DubboService-1.0.jar,输入如下命令:
mvn install:install-file -Dfile=DubboService-1.0.jar -DgroupId=dubboService -DartifactId=dubboService -Dversion=1.0 -Dpackaging=jar
结束后,会在本地仓库生成如下文件夹:

(7)在resources文件下新建consumer.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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 应用名称 -->
<dubbo:application name="consumer" />
<!-- zookeeper作为注册中心 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://172.31.19.224:2181" />
<!-- 生成远程服务代理 -->
<dubbo:reference interface="springboot.dao.DubboService" id="dubboService"></dubbo:reference>
</beans>
(8)在pom.xml中引入刚刚打包生成的jar包,如下:
<dependency>
<groupId>dubboService</groupId>
<artifactId>dubboService</artifactId>
<version>1.0</version>
</dependency>
(9)新建Controller,如下:
package springboot.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springboot.dao.DubboService; @RestController
public class HelloController { @Autowired
private DubboService dubboService; @RequestMapping("/hello")
public String hello(){
return dubboService.sayHello();
}
}
(10)修改启动类,如下:
package springboot; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication
@EnableScheduling
@EnableCaching
@ImportResource("classpath:consumer.xml")
public class SpringbootApplication extends SpringBootServletInitializer{ public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringbootApplication.class);
}
}
(11)测试验证,浏览器输入:http://localhost:8080/hello,显示如下:

至此,springBoot与Dubbo整合完毕!过程中也遇到一些小问题,记录一下吧。
(1)过程provider.xml,consumer.xml文件会报错,但是不影响项目运行,如下:Description Resource Path Location Type cvc-complex-type.2.4.c: The matching..........
解决方法如下:http://blog.csdn.net/u010457406/article/details/50696390
(2)项目没有报错,但是项目名出现了个红叉,百度搜了好多都不对,只有这个有效,地址如下:
解决方法如下:http://blog.csdn.net/hongchangfirst/article/details/7663287
继续努力!
SpringBoot与Dubbo整合下篇的更多相关文章
- springboot与dubbo整合入门(三种方式)
Springboot与Dubbo整合三种方式详解 整合环境: jdk:8.0 dubbo:2.6.2 springboot:2.1.5 项目结构: 1.搭建项目环境: (1)创建父项目与三个子项目,创 ...
- SpringBoot与Dubbo整合的三种方式
1. 使用默认application.properties和注解的方式 导入dubbo-starter,在application.properties配置属性,使用@Service注解来暴露服务,使用 ...
- springboot与dubbo整合遇到的坑
整合环境: dubbo 2.6.2 springboot 2.1.5 遇到的问题:服务一直无法注册到zookeeper注册中心 项目结构: 使用application.properties文件: 配置 ...
- SpringBoot与Dubbo整合-项目搭建
本章节建立生产者和消费者来演示dubbo的demo 生产者:springboot-dubbo-provider 和 消费者:springboot-dubbo-consumer 工程配置详解 Apach ...
- 关于Springboot中dubbo整合注意的误区(不对之处请指出)
这是我的客户端配置! 这是生产的配置, 首先注意一下 scan 我之前尝试这样的客户端配置 然后 果断客户端不能注册接口 @Reference(version="1.0") ...
- SpringBoot与Dubbo整合上篇
最近学习了一下dubbo,是阿里巴巴公司的一个开源服务框架.目前我们公司实现两个不同系统的之间通信,是采用了Oracle的OSB作为服务的管理(即企业服务总线的一种实现),服务提供方在OSB上注册业务 ...
- 玩转 SpringBoot 2 之整合 JWT 下篇
前言 在<玩转 SpringBoot 2 之整合 JWT 上篇> 中介绍了关于 JWT 相关概念和JWT 基本使用的操作方式.本文为 SpringBoot 整合 JWT 的下篇,通过解决 ...
- dubbo入门学习(三)-----dubbo整合springboot
springboot节省了大量的精力去配置各种bean,因此通过一个简单的demo来整合springboot与dubbo 一.创建boot-user-service-provider 本篇博文基于上篇 ...
- Dubbo整合Springboot框架
本文使用的是alibaba的Dubbo. Dubbo整合Springboot可以分为四步: 第一步:首先需要了解Dubbo官方给的建议,至少有三个工程: 接口工程:主要存实体bean和业务接口 服务提 ...
随机推荐
- kubernetes 创建tomcat 容器
方案一: 使用k8s dashboard 创建rc 1. 界面操作 提示:暂时 忽略 查看: 2.测试 由于是外部服务 直接用 节点的ip访问: 同样也是 第二个端口可以访问.感觉 跟之前的提 ...
- ICMP重定向及其攻防技术
1.ICMP重定向概念: ICMP重定向技术,是用来提示主机改变自己的主机路由从而使路由路径最优化的一种ICMP报文.其概念理解的要义是原主机路由不是最佳路由,而其默认网关提醒主机优化自身的主机路由而 ...
- JS复制制定内容到剪贴板怎么做?
可以使用input也可以使用textare文本域来做(而且这个input/textarea不能够被隐藏): <a href="javascript:;" onclick=&q ...
- 170505、MySQL的or/in/union与索引优化
假设订单业务表结构为: order(oid, date, uid, status, money, time, …) 其中: oid,订单ID,主键 date,下单日期,有普通索引,管理后台经常按照da ...
- Lucene.net的简单使用
一.Lucene.net的简单介绍 1.为什么要使用Lucene.net 使用like的模糊查询,模糊度太低,中间添加几个字就无法查找.同时会造成数据库的全文检索,效率低下,数据库服务器造 ...
- Hive与ES整合
https://www.elastic.co/guide/en/elasticsearch/hadoop/current/hive.html 注:添加的第三方jar必须位于namenode下,否则依然 ...
- Portugal 2 1 minute has Pipansihuan Germany and USA tacit or kick the ball
C Luo assists last moment so that Portugal "back to life", but with just two games to allo ...
- python之celery队列模块
一.celery队列简介 Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery. 1 ...
- [py]类属性和实例属性
默认类和实例的内置属性一致 class A: """测试类""" name = "maotai" def __init_ ...
- PAT Sum of Number Segments[数学问题][一般]
1104 Sum of Number Segments(20 分) Given a sequence of positive numbers, a segment is defined to be a ...