Spring Boot 集成Dubbo框架实例
使用spring boot 与dubbo集成,这里我之前尝试了使用注解的方式,简单的使用注解注册服务其实是没有问题的,但是当你涉及到使用注解的时候在服务里面引用事务,注入其他对象的时候,会有一些问题。于是我就果断放弃了注解了,使用的是xml,这里可能介绍的是dubbo,但是如果使用dubbox的话,基本上是兼容的。接下来,将说说使用xml的方式与spring boot在一起开发。
1.创建工程在pom.xml中加入依赖
创建工程名为:
(1)springboot-dubbo-provide
(2)springboot-dubbo-api
(3)springboot-dubbo-consume
springboot-dubbo-api工程主要是放一些service接口,用于提供给消费者使用 。springboot-dubbo-provide工程用于提供服务。 springboot-dubbo-consume工程为消费者。在springboot-dubbo-provide工程中打开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.chengli</groupid> <artifactid>springboot-dubbo-provide</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>springboot-dubbo-provide</name> <url>http://maven.apache.org</url> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.4.3.release</version> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <java.version>1.8</java.version> <com.alibaba.dubbo.version>2.5.3</com.alibaba.dubbo.version> <org.apache.zookeeper.version>3.4.6</org.apache.zookeeper.version> <com.github.sgroschupf.zkclient.version>0.1</com.github.sgroschupf.zkclient.version> </properties> <dependencies> <dependency> <groupid>com.chengli</groupid> <artifactid>springboot-dubbo-api</artifactid> <version>0.0.1-snapshot</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <!-- dubbo --> <dependency> <groupid>com.alibaba</groupid> <artifactid>dubbo</artifactid> <exclusions> <exclusion> <groupid>org.springframework</groupid> <artifactid>spring</artifactid> </exclusion> </exclusions> <version>${com.alibaba.dubbo.version}</version> </dependency> <dependency> <groupid>org.apache.zookeeper</groupid> <artifactid>zookeeper</artifactid> <version>${org.apache.zookeeper.version}</version> </dependency> <dependency> <groupid>com.github.sgroschupf</groupid> <artifactid>zkclient</artifactid> <version>${com.github.sgroschupf.zkclient.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
打开springboot-dubbo-consume工程,在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.chengli</groupid> <artifactid>springboot-dubbo-consume</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>springboot-dubbo-consume</name> <url>http://maven.apache.org</url> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.4.3.release</version> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <java.version>1.8</java.version> <com.alibaba.dubbo.version>2.5.3</com.alibaba.dubbo.version> <org.apache.zookeeper.version>3.4.6</org.apache.zookeeper.version> <com.github.sgroschupf.zkclient.version>0.1</com.github.sgroschupf.zkclient.version> </properties> <dependencies> <dependency> <groupid>com.chengli</groupid> <artifactid>springboot-dubbo-api</artifactid> <version>0.0.1-snapshot</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <!-- dubbo --> <dependency> <groupid>com.alibaba</groupid> <artifactid>dubbo</artifactid> <exclusions> <exclusion> <groupid>org.springframework</groupid> <artifactid>spring</artifactid> </exclusion> </exclusions> <version>${com.alibaba.dubbo.version}</version> </dependency> <dependency> <groupid>org.apache.zookeeper</groupid> <artifactid>zookeeper</artifactid> <version>${org.apache.zookeeper.version}</version> </dependency> <dependency> <groupid>com.github.sgroschupf</groupid> <artifactid>zkclient</artifactid> <version>${com.github.sgroschupf.zkclient.version}</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-configuration-processor</artifactid> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
2.dubbo配置
2.1springboot-dubbo-provide服务提供者
(1)在springboot-dubbo-provide项目中创建入口启动类mainconfig,完整代码如下:
package com.chengli.springboot; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplication public class mainconfig { public static void main(string[] args) { springapplication.run(mainconfig.class, args); try { system.in.read(); } catch (exception e) { e.printstacktrace(); } } }
(2)创建dubbo配置类
package com.chengli.springboot.dubbo; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.importresource; import org.springframework.context.annotation.propertysource; @configuration @propertysource("classpath:dubbo/dubbo.properties") @importresource({ "classpath:dubbo/*.xml" }) public class dubboconfig { }
(3)创建dubbo配置文件
在src/main/resources下新建文件夹dubbo,并加入以下配置:
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.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="${dubbo.application.name}" /> <!-- 注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <!-- <dubbo:registry protocol="zookeeper" address="10.170.219.98:2181,10.173.55.173:2181" /> --> <dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" /> <!-- 暴露服务 --> <dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}" /> <dubbo:service interface="com.chengli.springboot.example.service.exampleservice" ref="exampleserviceimpl" retries="0" timeout="6000" /> </beans>
注意:这里我发布的example服务是示例,具体的根据实际修改
(4)创建dubbo.properties
#应用名称 dubbo.application.name=example-provider #注册中心类型 dubbo.registry.protocol=zookeeper #注册中心地址 dubbo.registry.address=127.0.0.1:2181 #暴露服务方式 dubbo.protocol.name=dubbo #暴露服务端口 dubbo.protocol.port=20880
2.2springboot-dubbo-consume服务消费者
(1)创建入口启动类mainconfig
package com.chengli.springboot; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplication public class mainconfig { public static void main(string[] args) { springapplication.run(mainconfig.class, args); } }
(2)创建dubbo配置类
package com.chengli.springboot.dubbo; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.importresource; import org.springframework.context.annotation.propertysource; @configuration @propertysource("classpath:dubbo/dubbo.properties") @importresource({ "classpath:dubbo/*.xml" }) public class dubboconfig { }
(3)创建dubbo配置文件
在src/main/resources下新建文件夹dubbo,并加入以下配置:
dubbo-consume.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="${dubbo.application.name}" /> <!-- 注册中心暴露服务地址 --> <dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" /> <dubbo:reference id="axempleservice" interface="com.chengli.springboot.example.service.exampleservice" /> </beans>
(4)创建dubbo.properties
#应用名称 dubbo.application.name=example-consume #注册中心类型 dubbo.registry.protocol=zookeeper #注册中心地址 dubbo.registry.address=127.0.0.1:2181
到这里基本上就已经可以了,不过测试类的代码我就不贴上来了。只要在api中定义接口实现即可。使用spring boot 与dubbo集成的时候,需要注意的是,不要使用spring boot提供的devtools热启动,因为devtools提供了两个classloader,加载策略问题导致出现错误,无法启动。如果开发中需要热加载,那么使用spring 提供的springloaded。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(二)
-
Spring Boot 集成Dubbo框架实例
-
spring-boot整合dubbo:Spring-boot-dubbo-starter
-
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一)
-
详解Spring Boot 集成Shiro和CAS
-
Spring boot + mybatis + orcale实现步骤实例代码讲解
-
实例详解Spring Boot实战之Redis缓存登录验证码
-
spring-boot集成spring-security的oauth2实现github登录网站的示例
-
Spring-boot原理及spring-boot-starter实例和代码
-
spring boot 监控处理方案实例详解