这篇文章主要介绍了SpringBoot with Apache Dubbo工程的示例分析,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
该工程示例使用最新的Dubbo版本,Dubbo Starter以及SpringBoot版本,通过Gradle
进行工程管理和构建,输出可执行程序。
Dubbo
版本org.apache.dubbo:dubbo:2.7.1
org.apache.dubbo:dubbo-spring-boot-starter:2.7.1
SpringBoot
版本org.springframework.boot:spring-boot-dependencies:2.1.1.RELEASE
根工程(dubbo-case
),仅用来定义构建任务,工程信息
子工程(dubbo-api
) ,定义RPC服务的接口,参数和响应结果的数据传输对象
子工程(dubbo-client
), RPC服务的消费端,实际开发过程中实际情况是一些服务调用其它服务的RPC服务
子工程(dubbo-server
),RPC服务的提供端
代码仓库:https://github.com/broncho/dubbo-case
项目目录结构
备注:script目录归档执行脚本模版信息
public interface HelloService { String sayHello(String name); }
接口实现
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
服务消费示例
@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
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 Plugin和dependency-management结合起来进行SpringBoot版本管理以及子工程中依赖的统一管理。
gradle -DactiveProfile=prod clean distZip
基于SpringBoot的dubbo-server
和dubbo-client
工程会输出发布包到各自工程目录的build/distributions
目录。
感谢你能够认真阅读完这篇文章,希望小编分享的“SpringBoot with Apache Dubbo工程的示例分析”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。