服务器之家

服务器之家 > 正文

springboot2.0整合dubbo的示例代码

时间:2021-05-24 13:48     来源/作者:z七夜

写在前面:

使用springboot作为web框架,方便开发许多,做分布式开发,dubbo又不可少,那么怎么整合在一起呢,

跟我学一遍,至少会用

注意,springboot2.0和springboot1.x与dubbo整合不一样,

1.环境

1.新建一个空的maven项目,作为父工程,新建moudle,,service(接口层,及实现层,没有具体分,),web(web层,springboot项目)

项目结构如下

springboot2.0整合dubbo的示例代码

父pom如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
 
  <dependencymanagement>
    <dependencies>
 
 
      <dependency>
        <!-- import dependency management from spring boot -->
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-dependencies</artifactid>
        <version>2.0.3.release</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--如果要把springboot工程打包成war执行,需要该jar-->
      <!--<dependency>-->
      <!--<groupid>org.springframework.boot</groupid>-->
      <!--<artifactid>spring-boot-legacy</artifactid>-->
      <!--<version>1.0.2.release</version>-->
      <!--</dependency>-->
 
      <dependency>
        <groupid>com.alibaba.boot</groupid>
        <artifactid>dubbo-spring-boot-starter</artifactid>
        <version>0.2.0</version>
      </dependency>
 
      <!--引入zookeeper的客户端工具-->
      <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
      <dependency>
        <groupid>com.github.sgroschupf</groupid>
        <artifactid>zkclient</artifactid>
        <version>0.1</version>
      </dependency>
    </dependencies>
  </dependencymanagement>

web层pom

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<dependencies>
   <dependency>
     <groupid>com.itzmn</groupid>
     <artifactid>dubbo-service</artifactid>
     <version>1.0-snapshot</version>
   </dependency>
   <dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-starter-web</artifactid>
   </dependency>
   <dependency>
     <groupid>com.alibaba.boot</groupid>
     <artifactid>dubbo-spring-boot-starter</artifactid>
   </dependency>
 
   <!--引入zookeeper的客户端工具-->
   <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
   <dependency>
     <groupid>com.github.sgroschupf</groupid>
     <artifactid>zkclient</artifactid>
   </dependency>
   <dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-starter-test</artifactid>
     <scope>test</scope>
   </dependency>
 </dependencies>

service层

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependencies>
   <dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-starter-web</artifactid>
   </dependency>
   <dependency>
     <groupid>com.alibaba.boot</groupid>
     <artifactid>dubbo-spring-boot-starter</artifactid>
   </dependency>
 
   <!--引入zookeeper的客户端工具-->
   <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
   <dependency>
     <groupid>com.github.sgroschupf</groupid>
     <artifactid>zkclient</artifactid>
   </dependency>
 </dependencies>

2.接口设计

springboot2.0整合dubbo的示例代码

在service模块新建接口,

springboot2.0整合dubbo的示例代码

接口实现类的注解,service一定是dubbo的注解

3.配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# spring boot application
spring.application.name = /springboot-dubbo
server.port = 9099
management.port = 9091
 
# service version
demo.service.version = 1.0.0
 
# base packages to scan dubbo components (e.g @service , @reference)
dubbo.scan.basepackages = com.itzmn.dubbo.service.impl
 
# dubbo config properties
## applicationconfig bean
dubbo.application.id = springboot-dubbo
dubbo.application.name = springboot-dubbo
 
## protocolconfig bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880
 
## registryconfig bean
dubbo.registry.id = my-registry1
dubbo.registry.address = zookeeper://47.106.64.158:2181

在web层的配置文件中,配置,即可,前提,要先安装zookeeper,才能进行服务的注册,然后启动即可

4.消费者

配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# spring boot application
spring.application.name = dubbo-consumer-demo
server.port = 8080
management.port = 8081
 
# service version
demo.service.version = 1.0.0
 
# dubbo config properties
## applicationconfig bean
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo
 
## protocolconfig bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345

只需将服务提供者的接口jar包引入,然后注入服务即可

注意,springboot2.0和springboot1.x与dubbo整合不一样,

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jianshu.com/p/226df7409641

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部