服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - springboot2.0整合dubbo的示例代码

springboot2.0整合dubbo的示例代码

2021-05-24 13:48z七夜 Java教程

这篇文章主要介绍了springboot2.0整合dubbo的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

写在前面:

使用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

延伸 · 阅读

精彩推荐