該工程示例使用最新的Dubbo版本,Dubbo Starter以及SpringBoot版本,經過Gradle
進行工程管理和構建,輸出可執行程序。html
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服務的提供端備註:script目錄歸檔執行腳本模版信息java
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框架提供的git
@SpringBootApplication public class DubboServerApplication { public static void main(String[] args) { new EmbeddedZooKeeper(2181, true).start(); SpringApplication.run(DubboServerApplication.class, args); } }
備註:此處使用嵌入式Zookeeper,實現詳情參見源碼;或者能夠直接採用獨立Zookeeper服務github
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.htmlspring
@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版本管理以及子工程中依賴的統一管理。apache
gradle -DactiveProfile=prod clean distZip
基於SpringBoot的dubbo-server
和dubbo-client
工程會輸出發佈包到各自工程目錄的build/distributions
目錄。windows