工具说明:
开发工具:Eclipse
构建工具:Gradle
Jdk版本:1.8
注册中心:Zookeeper,本地测试采用的Docker启动Zookeeper,至于这么玩Docker,这里有我另一篇博客有简单说明,并且正好用Zookeeper来举例说明了使用过程: 01 第一次玩Docker的实操步骤
项目工程构建:如下图的项目结构,没有开发工具能生成这样层级结构的。为了在实际开发中,让我们的项目结构更清晰明了,所以我们通过目录结构来对项目组织,以提高开发人员对整个项目整体认识。所以像这样的结构只能手动创建。在实际开发中,我们的Gradle项目结构是3层目录结构。
setting.gradle
include 'hello-interface'
include 'hello-provider'
include 'hello-api'
rootProject.children.each {project ->
def size = project.children.size();
project.buildFileName = "${project.name}.gradle"
if(size > 0) {
project.children.each {_project ->
def _projectSize = _project.children.size();
_project.buildFileName = "${_project.name}.gradle"
if(_projectSize > 0) {
_project.children.each {__project ->
__project.buildFileName = "${__project.name}.gradle"
}
}
}
}
}
说明: rootProject.children.each 这个方法,为了支持对每个子项目的build.gradle的文件名进行修改,约定每个子项目的build脚本名称为子项目的名称。
build.gradle
buildscript {
ext {
springBootVersion = '2.2.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
subprojects {
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'idea'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: "io.spring.dependency-management"
group = "com.example.dubbo"
version = "0.0.1-SNAPSHOT"
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
}
configure(allprojects) { project ->
ext.dubbo_version='2.7.5'
ext.curator_version='4.0.1'
configurations.all {
// Check for updates every build
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
}
说明: 这个build.gradle可能与在start.spring.io自动生成的gradle项目不一样,因为gradle的发展真的太快了,一天一个样,能理解Gradle语法就行了。
依赖版本选择说明:
dubbo-springboot的版本当前最新版是2.7.5,dubbo-springboot对应的dubbo版本是同步的,所以dubbo版本也是2.7.5.
springboot版本,dubbo-springboot版本2.7.5依赖的是springboot的2.2.2.RELEASE,因为dubbo是我们整个项目的核心项目。所以整体的版本号都以dubbo为基准线。
curator为zookeeper的一个客户端,也是dubbo使用的zookeeper客户端。所以需要依赖这个
1、接口项目开发:
hello-interface.gradle 空的,因为暂时只需要集成接口,无需引入任何依赖
dependencies {
}
DemoService.java
package com.example.hello.service;
public interface DemoService {
String sayHello(String name);
}
2个文件搞定。
2、服务提供者开发:
hello-provider.gradle
dependencies {
compile(project(":hello-interface"))
implementation "org.springframework.boot:spring-boot-starter"
implementation "org.apache.dubbo:dubbo-spring-boot-starter:${dubbo_version}"
implementation "org.apache.curator:curator-framework:${curator_version}"
implementation "org.apache.curator:curator-recipes:${curator_version}"
}
说明: 引入依赖,并且依赖接口项目,注意我们没有引入spring-web的启动包。
application.yaml springboot配置文件
spring:
application:
name: hello-provider
dubbo:
# 指定服务提供者类所在包路径
scan:
base-packages:
- com.example.demo.provider.service
# 指定Dubbo服务往外暴露的协议类型,以及暴露的端口
# dubbo服务暴露的服务不是使用web容器暴露的端口,并且该Demo不使用web容器
protocol:
name: dubbo
port: 12345
# 指定注册中心为zookeeper
registry:
address: zookeeper://192.168.50.220:2181
HelloProviderApplication.java springboot启动类:
package com.example.demo.provider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class HelloProviderApplication {
public static void main(String[] args) {
SpringApplication.run(HelloProviderApplication.class, args);
}
}
说明:要启用@EnableDubbo注解,dubbo配置才会生效
DemoServiceImpl.java 服务提供类
package com.example.demo.provider.service;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.example.hello.service.DemoService;
@Service
public class DemoServiceImpl implements DemoService {
private final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
@Override
public String sayHello(String name) {
logger.info("welcome {}", name);
return name + ", hi";
}
}
注意:@Service注解是 apache dubbo的
启动
3、服务消费者编写
该消费者还提供对外暴露的api接口,所以这里需要使用到spring web
hello-api.gradle
dependencies {
compile(project(":hello-interface"))
implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.apache.dubbo:dubbo-spring-boot-starter:${dubbo_version}"
implementation "org.apache.curator:curator-framework:${curator_version}"
implementation "org.apache.curator:curator-recipes:${curator_version}"
}
说明:这里引入的就是spring-boot-starter-web
application.yaml springboot配置文件
spring:
application:
name: hello-api
dubbo:
# 指定注册中心
registry:
address: zookeeper://192.168.50.220:2181
# 指定消费者,提供者所在的包路径
scan:
base-packages:
- com.example.demo.web
HelloApiApplication.java 消费者项目启动类
package com.example.demo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class HelloApiApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApiApplication.class, args);
}
}
TestController.java 测试controller
package com.example.demo.web;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.hello.service.DemoService;
@RestController
public class TestController {
@Reference
private DemoService demoService;
@GetMapping("/test")
public String hello(String name) {
return demoService.sayHello(name);
}
}
启动测试:
浏览器访问: