spring boot 集成dubbo

该教程详细介绍了如何在SpringBoot2.4.1项目中集成Dubbo2.7.15,包括创建Maven多模块工程,定义接口模块、服务提供者模块和服务消费者模块,以及配置Zookeeper作为注册中心。服务提供者启动类需添加@EnableDubbo注解,服务消费者通过@DubboReference注入RPC服务。最后启动服务提供者和消费者,并通过前端访问测试集成效果。
摘要由CSDN通过智能技术生成

本demo使用spring boot 2.4.1版本集成 dubbo 2.7.15

1.创建maven项目及其子模块

父工程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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.lee.demo.dubbo.demo</groupId>
  <artifactId>coupon-platform-normal-springboot</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
  	<module>coupon-service-api</module>
  	<module>coupon-service-provider</module>
  	<module>user-service-api</module>
  	<module>user-service-provider</module>
  	<module>coupon-portal</module>
  </modules>
  
  <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>
    <spring-boot.version>2.4.1</spring-boot.version>
    <dubbo.version>2.7.15</dubbo.version>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  
    <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>com.lee.demo.dubbo.demo.userapi</groupId>
        <artifactId>user-service-api</artifactId>
        <version>1.0-SNAPSHOT</version>
      </dependency>
      <dependency>
		<groupId>com.lee.demo.dubbo.demo.couponapi</groupId>
        <artifactId>coupon-service-api</artifactId> 
        <version>1.0-SNAPSHOT</version>
      </dependency>
      <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-dependencies-zookeeper</artifactId>
        <version>2.7.15</version>
        <type>pom</type>
      </dependency>
      <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-bom</artifactId>
        <version>${dubbo.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>

  </dependencyManagement>
</project>

2.其中coupon-service-api为接口

3.coupon-service-provider为接口实现类,注意,其启动类需要添加注解@EnableDubbo

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.lee.demo.dubbo.demo</groupId>
    <artifactId>coupon-platform-normal-springboot</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
   
  
  <groupId>com.lee.demo.dubbo.demo.couponprovider</groupId>
  <artifactId>coupon-service-provider</artifactId>
  <name>coupon-service-provider</name>
  <description>coupon-service-provider</description>
  
   <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    
     <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency> 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency> 
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency> 
         <dependency>
			<groupId>com.lee.demo.dubbo.demo.couponapi</groupId>
            <artifactId>coupon-service-api</artifactId> 
        </dependency>
        
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.lee.demo.dubbo.demo.CouponServiceProviderApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

配置文件

实现类添加注解@DubboService

4.coupon-portal为web访问层,配置如下

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.lee.demo.dubbo.demo</groupId>
    <artifactId>coupon-platform-normal-springboot</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
   
  
  <groupId>com.lee.demo.dubbo.demo.portal</groupId>
  <artifactId>coupon-portal</artifactId>
  <name>coupon-portal</name>
  <description>coupon-portal</description>
  
  <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency> 
         <dependency>
            <groupId>com.lee.demo.dubbo.demo.userapi</groupId>
            <artifactId>user-service-api</artifactId>
        </dependency> 
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <type>pom</type>
        </dependency>
        
        
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.lee.demo.dubbo.demo.CouponPortalApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    
</project>

启动类

 controller访问层,属性注入时,如果为RPC调用,则需要添加注解@DubboReference

5.依次启动接口提供者(springboot项目)coupon-service-provider、user-service-provider以及web层coupon-portal

6.前端访问:127.0.0.1:8080/coupon

 

至此完成了springboot 2.4.1集成 dubbo 2.7.15

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值