第五节 SpringBoot集成Dubbo

亮子 2021-08-30 21:23:32 17307 0 0 0

1、创建父pom文件

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example.dubbo</groupId>
    <artifactId>springboot-dubbo-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>dubbo-api</module>
        <module>dubbo-customer</module>
        <module>dubbo-provider</module>
    </modules>
</project>

2、创建api工程

  • 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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.shenmazong</groupId>
    <artifactId>server-dubbo-api</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

</project>
  • api
package com.shenmazong.service;

public interface UserService {
    String sayHello();
}

3、创建生产者

  • 配置文件

spring.application.name=server-dubbo-provider server.port=8000 # 配置service实现类的包路径 dubbo.scan.base-packages=com.shenmazong.serverdubboprovider.service # Dubbo Protocol dubbo.protocol.name=dubbo ## Random port dubbo.protocol.port=-1 ## Dubbo Registry dubbo.registry.address=zookeeper://127.0.0.1:2181 #dubbo.registry.file = ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
  • pom文件
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.shenmazong</groupId>
    <artifactId>server-dubbo-provider</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>server-dubbo-provider</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <!--状态检查的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--dubbo服务-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.6</version>
        </dependency>

        <!-- zookeeper dependencies -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.6</version>
            <type>pom</type>
        </dependency>

        <!--自定义接口包-->
        <dependency>
            <groupId>com.shenmazong</groupId>
            <artifactId>server-dubbo-api</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • 实现类
package com.shenmazong.serverdubboprovider.service;

import com.shenmazong.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;

/**
 * @description UserService实现类
 * @author 军哥
 * @date 2021/8/31 16:57
 * @version 1.0
 */

@Service(version = "1.0.0")
public class UserServiceImpl implements UserService {

    @Value("${server.port}")
    private String port;

    @Override
    public String sayHello() {
        return "hello(1.0.0)" + port;
    }
}
  • 启动类
package com.shenmazong.serverdubboprovider;

import org.apache.dubbo.container.Main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ServerDubboProviderApplication {

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

        // 在容器里面创建一个生产者
        Main.main(args);
    }
}

4、定义消费者

  • 配置文件

spring.application.name=server-dubbo-consumer server.port=9000 dubbo.registry.address=zookeeper://127.0.0.1:2181
  • pom文件
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.shenmazong</groupId>
    <artifactId>server-dubbo-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>server-dubbo-consumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--状态检查的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--dubbo服务-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.6</version>
        </dependency>

        <!-- zookeeper dependencies -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.6</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--自定义接口包-->
        <dependency>
            <groupId>com.shenmazong</groupId>
            <artifactId>server-dubbo-api</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • 控制层
package com.shenmazong.serverdubboconsumer.controller;

import com.shenmazong.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class UserController {
    @Reference(version = "1.0.0", interfaceClass = UserService.class)
    private UserService userService;

    @GetMapping(value = "/hi")
    public String sayHi() {
        String msg = userService.sayHello();
        System.out.println(msg);
        return msg;
    }

}

5、版本问题

在使用上述版本,运行时没有问题的。但是使用springboot的最新版本,会出现如下错误:

org.apache.dubbo.rpc.RpcException: Fail to start server(url: dubbo://172.28.224.1:20880/com.shenmazong.serverdubboapi.service.UserService?anyhost=true&application=server-dubbo-provider&bind.ip=172.28.224.1&bind.port=20880&channel.readonly.sent=true&codec=dubbo&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&heartbeat=60000&interface=com.shenmazong.serverdubboapi.service.UserService&methods=sayHello&pid=28844&qos.enable=false&release=2.7.6&revision=1.0&side=provider&timestamp=1630401153654&version=1.0) Failed to bind NettyServer on /172.28.224.1:20880, cause: io/netty/bootstrap/ServerBootstrap
	at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol.createServer(DubboProtocol.java:348) ~[dubbo-2.7.6.jar:2.7.6]
	at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol.openServer(DubboProtocol.java:320) ~[dubbo-2.7.6.jar:2.7.6]
	at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol.export(DubboProtocol.java:303) ~[dubbo-2.7.6.jar:2.7.6]
	at org.apache.dubbo.qos.protocol.QosProtocolWrapper.export(QosProtocolWrapper.java:66) ~[dubbo-2.7.6.jar:2.7.6]
	at org.apache.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:62) ~[dubbo-2.7.6.jar:2.7.6]
	at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:153) ~[dubbo-2.7.6.jar:2.7.6]

出现如上错误,是因为少了netty的包,添加如下依赖即可解决。

        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.44.Final</version>
        </dependency>