400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

SpringBootwithApacheDubbo工程的示例分析

这篇文章主要介绍了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工程的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!


分享名称:SpringBootwithApacheDubbo工程的示例分析
文章出自:http://mzwzsj.com/article/ishgpi.html

其他资讯

让你的专属顾问为你服务