Bootstrap

springboot整合dubbo简单实例

回顾了下以前学过的dubbo,使用最新的springboot与dubbo简单搭建了实例,借此巩固下所学知识。

环境:springboot2.5.5、dubbo3.0.3、zookeeper3.7.0、maven3.6.3、jdk8、idea2020.3.4

1、首先新建空的maven父项目用于管理版本,各版本依赖如下

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <packaging>pom</packaging>
    <modules>
        <module>provider-service</module>
        <module>consumer-service</module>
        <module>interfaces</module>
    </modules>

    <groupId>com.example</groupId>
    <artifactId>dubbo-learning</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-learning</name>
    <description>dubbo</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.5.5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- zookeeper -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper</artifactId>
                <version>3.0.3</version>
                <type>pom</type>
            </dependency>
            <!-- dubbo依赖 -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>3.0.3</version>
            </dependency>
            <!-- 公共接口依赖 -->
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>interfaces</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.5</version>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2、新建提供者模块:provider-service

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-learning</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>provider-service</artifactId>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>interfaces</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
            <scope>provided</scope>
        </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>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
</project>

application.yml

server:
  port: 8001
spring:
  application:
    name: provider-service
dubbo:
  scan:
    base-packages: com.example.service # 指定需要注册到注册中心的服务扫描路径,也可以使用注解@EnableDubbo指定
  protocol:
    name: dubbo
    port: -1 # random
    host: 192.168.0.100 # 多网卡时需要指定一下取本地的哪个ip,尤其是有虚拟网卡的时候
  registry:
    address: zookeeper://192.168.192.99:2181

提供者就两个类,一个启动类和一个OrderService类。

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProviderApp {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApp.class, args);
    }
}

需要注册到注册中心的提供者服务,在旧版本中使用dubbo@Service注解,很容易与spring的弄混,新版本中用的是@DubboService注解,消费者引用的时候用的是@DubboReference,不再使用@Reference注解。

package com.example.service;

import com.example.pojo.OrderInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service // 注,这是spring的注解,不是dubbo的
@DubboService
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Override
    public OrderInfo getOrder(String userName) {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(100L);
        orderInfo.setAmount(new BigDecimal("10000.23"));
        orderInfo.setDate(new Date());
        orderInfo.setUserName(userName);
        log.info(">>> provider-serevice被调用了...");
        return orderInfo;
    }
}

3、服务消费者:consumer-service

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-learning</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer-service</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>interfaces</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
            <scope>provided</scope>
        </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>
            <!-- 注意这个pom -->
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

</project>

application.yml

server:
  port: 8002
spring:
  application:
    name: comsumer-service
dubbo:
  registry:
    address: zookeeper://192.168.192.99:2181 # 消费者可以不指定协议等信息

主启动类及测试controller

package com.example;

import com.example.pojo.orderInfo;
import com.example.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class UserApp {

    public static void main(String[] args) {
        SpringApplication.run(UserApp.class, args);
    }

    /**
    * 省事,写一个类里了,平时开发还是要单独写的
     */
    @RestController
    @RequestMapping("user")
    @Slf4j
    public static class UserController {
        /**
        * check=false,关闭启动时检查,避免当前服务启动时所依赖的提供者不能正常提供服务导致启动失败,生产环境默认true即可,可以及早发现问题所在
        * 同时指定了getOrder方法超时时间为5000毫秒
         */
        @DubboReference(check = false, methods = {@Method(name = "getOrder", timeout = 5000)})
        private OrderService orderService;

        @GetMapping("getOrderByUserName")
        public OrderInfo getOrderByUserName(@RequestParam("userName") String userName) {
            // 调用远程服务测试
            OrderInfo orderInfo = orderService.getOrder(userName);
            log.info(">>> dubbo 远程调用成功:{}", orderInfo);
            return orderInfo;
        }
    }
}

简单的一个入门案例就完成了。

主要在于maven依赖,依赖正确,就算成功一半了。

;