Spring Boot 集成Dubbo框架实例

网友投稿 223 2023-05-14


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加入以下依赖,完整代码如下:

xsi:schemaLocation="http://maven.apache.org/POM/4.http://0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.chengli

springboot-dubbo-provide

0.0.1-SNAPSHOT

jar

springboot-dubbo-provide

http://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

1.4.3.RELEASE

UTF-8

1.8

2.5.3

3.4.6

0.1

com.chengli

springboot-dubbo-api

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-starter

com.alibaba

dubbo

org.springframework

spring

${com.alibaba.dubbo.version}

org.apache.zookeeper

zookeeper

${org.apache.zookeeper.version}

com.github.sgroschupf

zkclient

${com.github.sgroschupf.zkclient.version}

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.http://0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.chengli

springboot-dubbo-provide

0.0.1-SNAPSHOT

jar

springboot-dubbo-provide

http://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

1.4.3.RELEASE

UTF-8

1.8

2.5.3

3.4.6

0.1

com.chengli

springboot-dubbo-api

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-starter

com.alibaba

dubbo

org.springframework

spring

${com.alibaba.dubbo.version}

org.apache.zookeeper

zookeeper

${org.apache.zookeeper.version}

com.github.sgroschupf

zkclient

${com.github.sgroschupf.zkclient.version}

org.springframework.boot

spring-boot-maven-plugin

打开springboot-dubbo-consume工程,在pom.xml中加入以下依赖,完整代码如下:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.chengli

springboot-dubbo-consume

0.0.1-SNAPSHOT

jar

springboot-dubbo-consume

http://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

1.4.3.RELEASE

UTF-8

1.8

2.5.3

3.4.6

0.1

com.chengli

springboot-dubbo-api

0.0.1-SNAPSHOTIuAohpL

org.springframework.boot

spring-boot-starter-web

com.alibaba

dubbo

org.springframework

spring

${com.alibaba.dubbo.version}

org.apache.zookeeper

zookeeper

${org.apache.zookeeper.version}

com.github.sgroschupf

zkclient

${com.github.sgroschupf.zkclient.version}

org.springframework.boot

spring-boot-configuration-processor

true

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.chengli

springboot-dubbo-consume

0.0.1-SNAPSHOT

jar

springboot-dubbo-consume

http://maven.apache.org

org.springframework.boot

spring-boot-starter-parent

1.4.3.RELEASE

UTF-8

1.8

2.5.3

3.4.6

0.1

com.chengli

springboot-dubbo-api

0.0.1-SNAPSHOTIuAohpL

org.springframework.boot

spring-boot-starter-web

com.alibaba

dubbo

org.springframework

spring

${com.alibaba.dubbo.version}

org.apache.zookeeper

zookeeper

${org.apache.zookeeper.version}

com.github.sgroschupf

zkclient

${com.github.sgroschupf.zkclient.version}

org.springframework.boot

spring-boot-configuration-processor

true

org.springframework.boot

spring-boot-maven-plugin

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内容如下:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

ref="exampleServiceImpl" retries="0" timeout="6000" />

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

ref="exampleServiceImpl" retries="0" timeout="6000" />

ref="exampleServiceImpl" retries="0" timeout="6000" />

注意:这里我发布的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,http:// 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内容如下:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

(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。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:ES6入门教程之let和const命令详解
下一篇:bootstrap的工具提示实例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~