欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot2.0整合dubbo的示例代码

程序员文章站 2024-02-24 09:53:28
写在前面: 使用springboot作为web框架,方便开发许多,做分布式开发,dubbo又不可少,那么怎么整合在一起呢, 跟我学一遍,至少会用 注意,s...

写在前面:

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

跟我学一遍,至少会用

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

1.环境

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

项目结构如下

springboot2.0整合dubbo的示例代码

父pom如下

<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

 <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层

 <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.配置文件

# 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.消费者

配置

# 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整合不一样,

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