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服務的提供端
4. 工程詳情
4.1 工程信息
- 代碼倉庫:https://github.com/broncho/dubbo-case
- 項目目錄結構
備註: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 Plugin和dependency-management結合起來進行SpringBoot版本管理以及子工程中依賴的統一管理。
5.2 打包命令
gradle -DactiveProfile=prod clean distZip
基於SpringBoot的dubbo-server
和dubbo-client
工程會輸出發佈包到各自工程目錄的build/distributions
目錄。