【dubbo从入门到跑路】hello world

【dubbo从入门到跑路】hello world

    • dubbo简介
    • 创建服务提供者项目
    • 创建服务消费者项目
    • 测试
    • 注意点

dubbo简介

dubbo的介绍可以查看官网说明,这里不做过多的介绍。
官网:http://dubbo.apache.org/zh-cn/index.html
github:https://github.com/apache/dubbo
dubbo spring boot:https://github.com/apache/dubbo-spring-boot-project

创建服务提供者项目

在日常工作中我们一般会使用SpringBoot作为基础开发框架,它的自动装配功能可以极大的提升我们的开发效率,故在本文中也采用SpringBoot集成dubbo进行演示。

  1. 通过IDEA创建一个SpringBoot项目(provider):
    【dubbo从入门到跑路】hello world_第1张图片
  2. 向外暴露的接口需要在服务提供方和服务消费方之间共享,故我们将接口的定义放在一个独立的模块中,接口的实现放到另外一个模块中。将我们上面新建的项目改造成一个多模块项目:
  • provider-client:负责存放接口定义,以及模型对象,并将该模块与服务消费者共享。
  • provider-server:负责实现provider-client中定义的接口等,需依赖provider-client模块。
    【dubbo从入门到跑路】hello world_第2张图片
  1. 集成dubbo
    参照 https://github.com/apache/dubbo-spring-boot-project/blob/master/README_CN.md 在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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <parent>
        <groupId>com.dubbogroupId>
        <artifactId>providerartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>

    <groupId>com.dubbogroupId>
    <artifactId>provider-serverartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>provider-servername>
    <description>Demo project for Spring Bootdescription>

    <packaging>jarpackaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
            <version>${spring-boot.version}version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
            <version>${dubbo.version}version>
        dependency>

        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubboartifactId>
            <version>${dubbo.version}version>
        dependency>

        <dependency>
            <groupId>com.dubbogroupId>
            <artifactId>provider-clientartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>
    dependencies>

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

project>


  1. 编写接口代码

项目结构:
【dubbo从入门到跑路】hello world_第3张图片

UserDTO代码,对象需要实现Serializable接口:

package com.dubbo.provider.client.model;

import java.io.Serializable;

public class UserDTO implements Serializable {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

UserService代码:

package com.dubbo.provider.client;

import com.dubbo.provider.client.model.UserDTO;

public interface UserService {

    public UserDTO findUserById(Integer id);
}
  1. 实现UserService接口

项目结构:
【dubbo从入门到跑路】hello world_第4张图片
UserServiceAdapter代码:

package com.dubbo.provider.server.adapter;

import com.dubbo.provider.client.UserService;
import com.dubbo.provider.client.model.UserDTO;

public class UserServiceAdapter implements UserService {

    @Override
    public UserDTO findUserById(Integer id) {
        UserDTO userDTO = new UserDTO();
        userDTO.setId(id);
        userDTO.setName("Leo");
        return userDTO;
    }
}
  1. 将该接口注册到dubbo中

在provider-server模块中的resources中新建provider.xml文件。
由于这是第一个dubbo演示项目,为了简单起见,这里采用直连模式,所以不配置注册中心。


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    
    <dubbo:application name="provider"  />

    
    <dubbo:registry address="N/A" />

    
    <dubbo:protocol name="dubbo" port="20880" />

    
    <dubbo:service interface="com.dubbo.provider.client.UserService" ref="userService" />

    
    <bean id="userService" class="com.dubbo.provider.server.adapter.UserServiceAdapter" />
beans>

在启动类上加载配置文件:

package com.dubbo.provider.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource(locations = "classpath:provider.xml")
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}
  1. 启动项目

【dubbo从入门到跑路】hello world_第5张图片

创建服务消费者项目

消费者项目的创建可以参考生产者项目创建的过程,步骤基本类似,由于只是演示,消费者项目不需要是多模块的。但是需要依赖provider-client模块,所以需要先在provider-client模块目录中运行 mvn deploy命令将该模块打包后提交到本地仓库中备用。
项目结构:
【dubbo从入门到跑路】hello world_第6张图片
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.1.RELEASEversion>
        <relativePath/> 
    parent>

    <groupId>com.dubbogroupId>
    <artifactId>consumerartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>consumername>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <spring-boot.version>2.1.1.RELEASEspring-boot.version>
        <dubbo.version>2.7.1dubbo.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
            <version>${spring-boot.version}version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <version>${spring-boot.version}version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>com.dubbogroupId>
            <artifactId>provider-clientartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>

        
        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
            <version>${dubbo.version}version>
        dependency>

        <dependency>
            <groupId>org.apache.dubbogroupId>
            <artifactId>dubboartifactId>
            <version>${dubbo.version}version>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

consumer.xml文件内容:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    
    <dubbo:application name="consumer"  />

    
    <dubbo:registry address="N/A" />

    
    <dubbo:reference id="userService" interface="com.dubbo.provider.client.UserService"/>
beans>

HelloController代码:

package com.dubbo.consumer.controller;

import com.dubbo.provider.client.UserService;
import com.dubbo.provider.client.model.UserDTO;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Reference(url = "dubbo://127.0.0.1:20880")
    private UserService userService;

    @GetMapping(value = "/hello")
    public String hello() {
        UserDTO userDTO = userService.findUserById(1);
        return "hello world!!! ID:" + userDTO.getId() + " Name:" + userDTO.getName();
    }
}

测试

至此代码部分就开发完毕,启动项目查看效果。由于dubbo默认会开启启动时检查,所以我们需要先启动provider项目,再启动consumer项目,否则会报错。
【dubbo从入门到跑路】hello world_第7张图片

注意点

  1. 接口定义和POJO对象需要在服务间共享,建议单独进行打包。
  2. POJO要实现Serializable接口,否则会报错。
  3. 如果没配置注册中心,采用直连模式需要在@Reference注解中配置url属性,在xml中配置启动会报错,这一点有可能是我操作有误?

你可能感兴趣的:(dubbo)