Dubbo的相关资料:
dubbo官网: http://dubbo.apache.org dubbo github
源码地址:https://github.com/apache/incubator-dubbo dubbo
运维项目源码地址:https://github.com/apache/incubator-dubbo-ops
由于Kafka的运行依赖于Zookeeper
,所以在运行Kafka
之前需要安装并运行Zookeeper
Zookeeper下载路径:http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.7/ 如上图所示。
1.解压到文件下(我的目录是 D:\webserver\apache-zookeeper-3.5.7-bin),我习惯修改目录名称为:apache-zookeeper-3.5.7
2.打开D:\webserver\apache-zookeeper-3.5.7\conf,复制zoo_sample.cfg重命名成zoo.cfg
3.编辑zoo.cfg,修改dataDir为【dataDir=D:\webserver\apache-zookeeper-3.5.7\data】,
在D:\webserver\apache-zookeeper-3.5.7目录下创建data目录。
//zookeeper默认用的是8080端口,和tomcat的默认端口冲突,因此这个地方最好修改。
在文件的最后增加一行:admin.serverPort=8888
ZOOKEEPER_HOME D:\webserver\apache-zookeeper-3.5.7
Path 在现有的值后面添加 %ZOOKEEPER_HOME%\bin
打开cmd,输入zkServer.cmd
,运行如下:
说明运行成功了。
集成之前我们先介绍一下目前结构:基本dubbo通信需要有客户端和服务端,服务端服务提供接口,客户端负责调用接口,同时,关于接口的定义及通讯数据结构需要实现定义。因此,我们需要创建三个项目:
在idea中,单击file》new》project,弹出下面对话框,左边选择maven,这里不要选择Create from archetype复选框。
如图所示操作;
如图所示操作;
完成父工程创建。
这个初始目录下有src目录,可以删除。
在dubbopro工程上点击右键,选择new->Module…
选择new->Module… 输入group及Artifact,Artifact一般是工程名称,group一般是包路径名。
点击下一步
不用选择,直接Next,然后Finish,这样,接口工程就创建完毕。
在dubbopro工程上点击右键,选择new->Module… 如图所示进行选择(和上一步不一样,这个是建立Springboot的工程):
next后,组的名称和工程的名称都需要修改,如下图:客户端名称为:dubbo-consumer,服务端名称为:dubbo-provider
Next,如图所示,需要打钩,开启热部署及lombok插件;
next后,选择左侧的web,然后右侧选择spring web 建立web工程:
点击Finish结束创建。
首先在dubbo-api工程中定义通信的数据结构类,如下:
package com.ieslab.powergrid.dubboapi.model;
import java.io.Serializable;
public class TestDubboData implements Serializable {
String id;
String name;
String data;
Long time;
String des;
public TestDubboData() {
}
public TestDubboData(String id, String name, String data, Long time, String des) {
this.id = id;
this.name = name;
this.data = data;
this.time = time;
this.des = des;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
}
接口类TestDubboService.java:
package com.ieslab.powergrid.dubboapi.service;
import com.ieslab.powergrid.dubboapi.model.TestDubboData;
public interface TestDubboService{
TestDubboData TestDubboService(String uuid, String name);
}
这个接口很简单,传入两个参数,返回我们定义的数据结构。
如果跨语言通讯,数据结构可以用Google Protobuff进行定义,这样方便序列化和反序列化
下面是dubbo-provider的pom文件。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.ieslab.powergrid</groupId>
<artifactId>dubbo-provider</artifactId>
<name>dubbo-provider</name>
<properties>
<revision>2.7.6</revision>
<dubbo.version>2.7.6</dubbo.version>
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
</properties>
<dependencies>
<!-- Spring Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.ieslab.powergrid</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0</version>
</dependency>
<!-- Zookeeper dependencies -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>false</executable>
<finalName>${project.artifactId}-${spring-boot.version}</finalName>
<outputDirectory>${user.dir}/target</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
编写实现类TestDubboServiceImp.java
package com.ieslab.powergrid.dubboprovider.service;
import com.ieslab.powergrid.dubboapi.model.TestDubboData;
import com.ieslab.powergrid.dubboapi.service.TestDubboService;
import org.apache.dubbo.config.annotation.Service;
@Service(version = "${provider.service.version}")
public class TestDubboServiceImp implements TestDubboService {
/**
* 实现TestDubboService接口
* @param uuid 访问的订单ID
* @param name 用户名称
* @return
*/
@Override
public TestDubboData getTestDubboData(String uuid, String name) {
//构建一个数据进行返回
TestDubboData td = new TestDubboData();
td.setId(uuid);
td.setName(name);
td.setTime(System.currentTimeMillis());
td.setDes("这是一个测试接口,你测试成功了");
td.setData("您好,"+name+",这是您访问的数据!!!");
return td;
}
}
注意:@Service(version = “${provider.service.version}”)
这个注释,表示这是个dubbo的接口实现类,@Service注解是属于dubbo的,包是:org.apache.dubbo.config.annotation.Service
其中的version = “${provider.service.version}” 表示接口的版本,在配置文件中配置的。
实现很简单,我就是实现了接口的方法,构建了一个测试数据直接返回。
到此,我们完成了实现,但是我们怎么把这个实现发布出去呢?
在application.properties添加如下配置信息:
server.port=20201
# Spring boot application
spring.application.name=dubbo-provider
# Base packages to scan Dubbo Component: @org.apache.dubbo.config.annotation.Service
dubbo.scan.base-packages=com.ieslab.powergrid.dubboprovider.service
# Dubbo Application
## The default value of dubbo.application.name is ${spring.application.name}
## dubbo.application.name=${spring.application.name}
# Dubbo Protocol
dubbo.protocol.name=dubbo
## Random port
dubbo.protocol.port=-1
## Dubbo Registry
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.registry.file = ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
## Service version
provider.service.version=1.0.0
因为我们的zookeeper运行在本机,因此,配置成127.0.0.1:2182,如果zookeeper是集群方式,只需要配置集群的几个ip:端口 用分号隔开即可。此处我们用单机版的。
其中的 dubbo.scan.base-packages=com.ieslab.powergrid.dubboprovider.service 配置了实现类的路径。
运行主类:DubboProviderApplication.java
package com.ieslab.powergrid.dubboprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
在dubbo-consumer,在pom文件中增加依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.ieslab.powergrid</groupId>
<artifactId>dubbo-consumer</artifactId>
<name>dubbo-consumer</name>
<properties>
<revision>2.7.6</revision>
<dubbo.version>2.7.6</dubbo.version>
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
</properties>
<dependencies>
<!-- Spring Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.ieslab.powergrid</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0</version>
</dependency>
<!-- Zookeeper dependencies -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>false</executable>
<finalName>${project.artifactId}-${spring-boot.version}</finalName>
<outputDirectory>${user.dir}/target</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
客户端的pom文件基本和服务端一致,只是名称不一样。
修改application.yml文件:
spring:
application:
name: dubbo-registry-zookeeper-consumer-sample
server:
port: 8090
provider:
service:
version: 1.0.0
embedded:
zookeeper:
port: 2181
dubbo:
registry:
address: zookeeper://127.0.0.1:${embedded.zookeeper.port}
file: ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
和服务端的格式不一致,这里只为学习,生产环境最好统一格式。
版本号要和服务端保持一致
创建测试类:TestController.java
package com.ieslab.powergrid.dubboconsumer;
import com.ieslab.powergrid.dubboapi.model.TestDubboData;
import com.ieslab.powergrid.dubboapi.service.TestDubboService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.dubbo.config.annotation.Reference;
@RestController
public class TestController {
@Reference(version = "${provider.service.version}")
TestDubboService testDubboService;
@RequestMapping("/test")
public TestDubboData test(String uuid, String name){
TestDubboData test = testDubboService.getTestDubboData(uuid,name);
return test;
}
}
@Reference(version = “${provider.service.version}”) 一定指定的版本一定要和服务端一致,如果服务端设置了版本,这个地方一定也要设置版本,不然链接不上。
package com.ieslab.powergrid.dubboconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
运行dubbo-provider的主类:DubboProviderApplication.java
运行dubbo-consumer的主类:DubboConsumerApplication.java
访问接口:http://localhost:8090/test?uuid=1&name=binge
如下图,返回如下测试数据就成功了。
本文是使用的最新版本的dubbo,如果服务端和客户端的dubbo版本不一致,会导致链接不成功,请切记。