β

SpringBoot整合Dubbo2.5.10

Harries Blog™ 24 阅读

Dubbo已经进入了Apache孵化器,并且发布了官方的 spring -boot-s tar ter0.1.0,用于简化 dubbo 应用的 配置 ,主要包括了autoconfigure(自动装配),externalized-configuration(外部化配置),actuator(生产准备)等,可参考官方 git hub dubbo-spring-boot-starter .

准备工作

需要提前 安装 好JDK1.8,Maven,Zookeeper。

初始化Maven项目

为了整个项目结构清晰,使用模块化的 maven 项目。 pom 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>org.boot.dubbo</groupId>
    <artifactId>boot-dubbo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>boot-dubbo</name>
    <description>Dubbo project for Spring Boot</description>

    <modules>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
        <module>dubbo-api</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <dubbo-spring-boot-starter.version>0.1.0</dubbo-spring-boot-starter.version>
        <springboot.version>1.5.8.RELEASE</springboot.version>
        <fastjson-version>1.2.31</fastjson-version>
        <zk-client.version>0.2</zk-client.version>
        <dockerfile-maven.version>1.4.3</dockerfile-maven.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo-spring-boot-starter.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
                <version>${springboot.version}</version>
            </dependency>

            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>${zk-client.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

主要分为三个模块,api,prov id er和consumer

SpringBoot整合Dubbo2.5.10

创建生产者

有了spring-boot-starter,dubbo的配置变得非常简单,再也不用像以前一样配置一大堆xml文件,只需要几个简单的配置,就可以做到开箱即用。

  1. 先配置生产者的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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.boot.dubbo</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>${parent.version}</version>
    <packaging>jar</packaging>

    <parent>
        <artifactId>boot-dubbo</artifactId>
        <groupId>org.boot.dubbo</groupId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <name>dubbo-provider</name>
    <description>Dubbo project for Spring Boot:Provider</description>

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

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

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

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

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>zookeeper</artifactId>
                    <groupId>org.apache.zookeeper</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.boot.dubbo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>${parent.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  1. 接着使用properties进行SpringBoot和Dubbo的配置,配置如下:
spring.application.name=springboot-dubbo-provider
   server.port=9090
   #dubbo配置
   dubbo.application.id=springboot-dubbo-provider
   dubbo.application.name=springboot-dubbo-provider
   dubbo.application.owner=luoliang
   #协议配置
   dubbo.protocol.id=dubbo
   dubbo.protocol.name=dubbo
   #把默认的20880端口换成12345
   dubbo.protocol.port=12345
   #服务注册配置
   dubbo.registry.id=my-registry
   dubbo.registry.address=zookeeper://localhost:2181
   #配置dubbo的包扫描,针对dubbo的@Service, @Reference注解
   dubbo.scan.base-packages=org.boot.dubbo.provider.service
   #dubbo健康监控
   endpoints.dubbo.enabled=true
   management.health.dubbo.status.defaults=memory
   management.health.dubbo.status.extras=load,threadpool
   management.port=9091
  1. 进行了上面两步之后,Dubbo已经集成好了,接下来就可以直接开始撸服务 代码 了,可以直接使用注解来暴露服务接口
  • 先在api中写一个interface
public interface HelloService {
   String sayHello(String name);
}
  • 实现接口,加上自己的业务逻辑
@Service(version = "1.0.0",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}")
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHello(String name) {
        return "Hello, " + name + " (from Spring Boot)";
    }
}

注意,这里的service注解是com.alibaba.dubbo.config.annotation.Service

创建消费者

  1. 配置消费者者的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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.boot.dubbo</groupId>
    <artifactId>dubbo-consumer</artifactId>
    <version>${parent.version}</version>
    <packaging>jar</packaging>

    <parent>
        <artifactId>boot-dubbo</artifactId>
        <groupId>org.boot.dubbo</groupId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <name>dubbo-consumer</name>
    <description>Dubbo project for Spring Boot:Consumer</description>

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

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

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

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

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.boot.dubbo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>${parent.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  1. application.properties配置如下:
spring.application.name=springboot-dubbo-consumer
server.port=8081
#dubbo配置
dubbo.application.id=springboot-dubbo-consumer
dubbo.application.name=springboot-dubbo-consumer
dubbo.application.owner=luoliang
#服务注册配置
dubbo.registry.id=my-registry
dubbo.registry.address=zookeeper://localhost:2181
management.port=8082
  1. 编写service来消费dubbo的服务,主要代码如下:
@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Reference(version = "1.0.0",
            application = "${dubbo.application.id}")
    private HelloService helloService;

    @Override
    public String sayHello(String name) {
        return helloService.sayHello(name);
    }
}
在mvc的controller中注入此服务
@RestController
@RequestMapping("/user")
public class DefaultController {
    @Resource
    private ConsumerService consumerService;

    @RequestMapping("/sayHello")
    public String register(String name) {
        return consumerService.sayHello(name);
    }
}

到这里,整个项目基本结构已经搭建完成,consumer已经能够消费provider提供的服务。

现在来 测试 一下,分别启动provider和consumer,打开浏览器,输入http://localhost:8081/user/sayHello?name=dubbo

可以看到,返回的结果和预期一样,说明项目已经成功集成

SpringBoot整合Dubbo2.5.10

需要 源码 请移步本人github,如果能顺手star就更好啦! boot-dubbo ,下一篇 博客 接这篇博客,学习使用 Docker 容器化SpringBoot+Dubbo应用。

第一次在掘金发文,各位大佬轻喷~

原文

https ://juejin.im/post/5b1f3a91f265da6e626ae6b8

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。 PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处: Harries Blog™ » SpringBoot整合Dubbo2.5.10

作者:Harries Blog™
追心中的海,逐世界的梦
原文地址:SpringBoot整合Dubbo2.5.10, 感谢原作者分享。

发表评论