SpringBoot with Apache Dubbo工程的示例分析

发布时间:2021-11-22 09:36:54 作者:小新
来源:亿速云 阅读:119

这篇文章主要介绍了SpringBoot with Apache Dubbo工程的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

1. 说明

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

2. 框架版本

2.1  Dubbo版本

2.2 SpringBoot版本

3. 模块关系

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服务

# 应用名称
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");

    }
}
# 应用程序名
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配置

systemProp.activeProfile=dev
systemProp.javaVersion=1.8
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 Plugin和dependency-management结合起来进行SpringBoot版本管理以及子工程中依赖的统一管理。

5.2 打包命令

gradle   -DactiveProfile=prod  clean  distZip

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

5.3 发布包格式

SpringBoot with Apache Dubbo工程的示例分析

感谢你能够认真阅读完这篇文章,希望小编分享的“SpringBoot with Apache Dubbo工程的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. SpringBoot整合Dubbo案例
  2. springboot2.2.2集成dubbo的实现方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot dubbo

上一篇:php是什么

下一篇:c语言怎么实现含递归清场版扫雷游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》