SpringBoot with Apache Dubbo 工程示例

1. 说明

该工程示例使用最新的Dubbo版本,Dubbo Starter以及SpringBoot版本,通过Gradle进行工程管理和构建,输出可执行程序。

2. 框架版本

2.1 Dubbo版本

  • org.apache.dubbo:dubbo:2.7.1
  • org.apache.dubbo:dubbo-spring-boot-starter:2.7.1

2.2 SpringBoot版本

  • org.springframework.boot:spring-boot-dependencies:2.1.1.RELEASE

3. 模块关系

  • 根工程(dubbo-case),仅用来定义构建任务,工程信息
  • 子工程(dubbo-api) ,定义RPC服务的接口,参数和响应结果的数据传输对象
  • 子工程(dubbo-client), RPC服务的消费端,实际开发过程中实际情况是一些服务调用其它服务的RPC服务
  • 子工程(dubbo-server),RPC服务的提供端

SpringBoot with Apache Dubbo 工程示例

4. 工程详情

4.1 工程信息

SpringBoot with Apache Dubbo 工程示例

备注:script目录归档执行脚本模版信息

4.2 接口定义(dubbo-api)

public interface HelloService {

    String sayHello(String name);
}

4.3 服务提供者(dubbo-server)

  • 接口实现
import com.github.broncho.dubbo.api.HelloService;
import org.apache.dubbo.config.annotation.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Author: secondriver
 * Created: 2019/8/4
 */
@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "Welcome " + name + " at " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
    }
}

备注:@Service注解是由Dubbo框架提供的

  • 启动类

@SpringBootApplication
public class DubboServerApplication {

    public static void main(String[] args) {
        new EmbeddedZooKeeper(2181, true).start();
        SpringApplication.run(DubboServerApplication.class, args);
    }
}

备注:此处使用嵌入式Zookeeper,实现详情参见源码;或者可以直接采用独立Zookeeper服务

  • 配置文件application.properties
# 应用名称
dubbo.application.name=dubbo-app1

dubbo.application.qosEnable=true
dubbo.application.qosPort= 22222
dubbo.application.qosAcceptForeignIp=true

# 接口实现者(服务实现)包
dubbo.scan.base-packages=com.github.broncho.dubbo.server.impl

# 注册中心信息
dubbo.registry.id=my-zk
dubbo.registry.address=localhost:2181
dubbo.registry.protocol=zookeeper
dubbo.registry.client=curator

关于Dubbo的配置参见:https://dubbo.apache.org/zh-cn/docs/user/references/api.html

4.4 服务消费者(dubbo-client)

  • 服务消费示例
@Component
public class BusinessComponent {

    @Reference
    private HelloService helloService;

    public void greeting(String name) {
        System.out.println(helloService.sayHello(name));
    }

}
  • 启动类
@SpringBootApplication
public class DubboClientApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = SpringApplication.run(DubboClientApplication.class, args);

        BusinessComponent component = context.getBean(BusinessComponent.class);

        //RPC
        component.greeting("Dubbo RPC");

    }
}
  • 配置文件(application.properties)
# 应用程序名
dubbo.application.name=dubbo-app2

# 注册中心信息
dubbo.registry.id=my-zk
dubbo.registry.address=localhost:2181
dubbo.registry.protocol=zookeeper
dubbo.registry.client=curator

5. 打包部署

5.1 Gradle配置

  • gradle.properties
systemProp.activeProfile=dev
systemProp.javaVersion=1.8
  • build.gradle
import org.springframework.boot.gradle.plugin.SpringBootPlugin

buildscript {
    repositories {
        mavenLocal()
        maven {
            name "alimaven"
            url "http://maven.aliyun.com/nexus/content/groups/public/"

        }
        mavenCentral()
    }
}

plugins {
    id 'java'
    id 'idea'
    id 'org.springframework.boot' version '2.1.1.RELEASE' apply false
    id 'io.spring.dependency-management' version '1.0.8.RELEASE' apply false
}

//所有工程定义
allprojects {

    sourceCompatibility = System.properties["javaVersion"]
    targetCompatibility = System.properties["javaVersion"]

    repositories {
        mavenLocal()
        maven {
            name "alimaven"
            url "http://maven.aliyun.com/nexus/content/groups/public/"

        }
        mavenCentral()
    }
    group 'com.github.broncho'
    version '1.0.0'
}

//子工程定义
subprojects {
    apply plugin: 'java'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'distribution'

    if (!name.contains("api")) {
        apply plugin: 'org.springframework.boot'
        apply plugin: 'application'
    }

    dependencyManagement {
        imports {
            mavenBom(SpringBootPlugin.BOM_COORDINATES)
        }
        dependencies {
            dependencySet(group: 'org.apache.dubbo', version: '2.7.1') {
                entry 'dubbo'
                entry 'dubbo-spring-boot-starter'
            }
        }
        dependencies {
            dependencySet(group: 'org.apache.curator', version: '4.0.0') {
                entry 'curator-framework'
                entry 'curator-recipes'
            }
        }
        dependencies {
            dependency 'org.apache.zookeeper:zookeeper:3.5.5'
        }
    }

    if (!project.name.contains("api")) {

        println "Project ${name}  activeProfile ${System.properties['activeProfile']}"

        jar {
            enabled true
        }

        applicationDefaultJvmArgs = ['-Xms256m', '-Xmx256m']

        /**
         * 生成启动脚本
         */
        startScripts() {
            doFirst {
                unixStartScriptGenerator.template = resources.text.fromFile(
                        project.getRootDir().getAbsolutePath() + "/script/unixStartScript.txt"
                )
                windowsStartScriptGenerator.template = resources.text.fromFile(
                        project.getRootDir().getAbsolutePath() + "/script/windowsStartScript.txt"

                )
            }
        }

        /**
         * 开发环境下不能排除配置文件,不然没法通过IDE运行项目
         */
        if (!"dev".equals(System.properties['activeProfile'])) {
            proce***esources {
                exclude 'application*.properties'
            }
        }

        /**
         * application*.properties统一复制到config目录
         * SpringBoot程序启动时从config目录读取配置文件
         */
        applicationDistribution.from("src/main/resources") {
            include 'application*.properties'
            into 'config'
        }

    }
}

项目采用了Spring Boot Gradle Plugindependency-management结合起来进行SpringBoot版本管理以及子工程中依赖的统一管理。

5.2 打包命令

gradle   -DactiveProfile=prod  clean  distZip  

基于SpringBoot的dubbo-serverdubbo-client工程会输出发布包到各自工程目录的build/distributions目录。

5.3 发布包格式

SpringBoot with Apache Dubbo 工程示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值