IDEA搭建SpringBoot+Dubbo+Zookeeper
一、准备工作
1.安装zookeeper
dubbo
项目官方建议使用zookeeper
做为dubbo
的服务注册与发现的注册中心,所以首先需要在服务器中安装zookeeper
。我使用的是mac开发,在mac中,首先cd到你要安装的目录,然后使用brew命令brew install zookeeper
,等待安装完成后,cd到Cellar
,然后cd zookeeper/3.4.13/bin
,
在bin中执行
zkServer start
,运行zookeeper
。
image.png
brew
安装好的zookeeper是配置好的,不用手动去配置zoo.cfg
文件,如果要手动修改配置文件的话,可以在安装根目录中进入/etc/zookeeper
目录修改zookeeper
。image.png
image.png
2.部署dubbo后台管理
打开dubboGithub链接,下载zip文件,然后使用maven
进行打包,然后在dubbo-admin-server
的target
中得到dubbo-admin-server-0.1.jar
,
拷贝jar包到其他目录中,这里我拷贝到本地
tomcat
的webapps
中,使用命令java -jar dubbo-admin-server-0.1.jar
运行项目,然后就可以本地通过localhost:8080
访问dubbo管理后台了。image.png
这里可以看到注册到后台的接口等其他信息,暂时还没有详细研究。
二、正式创建项目
1.创建多模块项目
使用idea新建一个Spring Initializr
项目
配置
Group``Artifact
等image.png
一直
next
到项目创建
在打开的dubbo项目中右击新建module
image.png
依次创建
dubbo-api
,dubbo-server
,dubbo-web
三个模块,其中api是提供接口,server是接口的实现类。
2.配置项目
创建好项目后要分别配置一下项目的pom.xml
文件,我直接贴出来
父项目的
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.crossyf.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo</name>
<description>Demo project for Spring Boot</description>
<packaging>pom</packaging>
<modules>
<module>dubbo-api</module>
<module>dubbo-service</module>
<module>dubbo-web</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<boot.version>2.1.6.RELEASE</boot.version>
<tomcat.version>8.5.42</tomcat.version>
<dubbo.version>2.0.0</dubbo.version>
<zookeeper.version>3.4.10</zookeeper.version>
<zkclient.version>0.5</zkclient.version>
<dubbo-spring-boot>1.0.0</dubbo-spring-boot>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- alibaba dubbo-spring-boot-starter -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- ZooKeeper client -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
api
的pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.crossyf.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<packaging>jar</packaging>
<artifactId>dubbo-api</artifactId>
<groupId>com.crossyf.dubbo</groupId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-api</name>
<url>http://maven.apache.org</url>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<start-class>com.crossyf.dubbo.web.DubboWebApplication</start-class>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
</project>
注意 这里pom
里的build
标签内的内容,如果不加上这个,sevice
运行是没有问题的,但是使用maven
构建项目的时候,所有引用到api module
里的包名都会报错找不到包名
。这里我在这里耽误了很久,找不到问题,最后在这个帖子里找到了答案
https://hbxflihua.iteye.com/blog/2431537
service
的pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.crossyf.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<version>0.0.1-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<groupId>com.crossyf.dubbo</groupId>
<artifactId>dubbo-service</artifactId>
<name>dubbo-service</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>com.crossyf.dubbo</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>dubbo-service</finalName>
<resources>
<resource>
<targetPath>${project.build.directory}/classes</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.yml</include>
</includes>
</resource>
</resources>
<pluginManagement>
<plugins>
<!-- 解决Maven插件在Eclipse内执行了一系列的生命周期引起冲突 -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[2.0,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<classesDirectory>target/classes/</classesDirectory>
<archive>
<manifest>
<mainClass>com.crossyf.dubbo.service.DubboServiceApplication</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeTypes>jar</includeTypes>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
web
的pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.crossyf.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.crossyf.dubbo</groupId>
<artifactId>dubbo-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-web</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>dubbo-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- beetl -->
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-framework-starter</artifactId>
<version>1.1.22.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>dubbo-web</finalName>
</build>
</project>
3.配置application.yml
文件
service
的application.yml
如下
port: 8094
servlet:
context-path: /dubbo-service
spring:
http:
encoding:
charset: UTF-8
force: true
enabled: true
dubbo:
application: #应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
name: Provide
registry: #注册中心配置,用于配置连接注册中心相关信息。
address: zookeeper://192.168.3.38:2181
protocol: #协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
name: dubbo
port: 20880
scan: com.crossyf.dubbo.service.impl #服务暴露与发现消费所在的package
web
的application.yml
server:
port: 8092
servlet:
context-path: /dubbo-web
spring:
http:
encoding:
charset: UTF-8
force: true
enabled: true
multipart:
dubbo:
application: #应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
name: Customer
id: Customer
registry: #注册中心配置,用于配置连接注册中心相关信息。
address: zookeeper://192.168.3.38:2181
protocol:
name: dubbo
port: 20880
4.编写测试代码
在api
模块中创建一个接口DemoApi
,定义一个方法
* @author Created by YangFan.
* @date 2019/7/8
* 功能:
*/
public interface DemoApi {
/**
* 打招呼
* @param name 名称
* @return 结果
*/
String sayHello(String name);
}
然后在service中创建接口的实现类DemoApiImpl
/**
* @author Created by YangFan.
* @date 2019/7/8
* 功能:
*/
@Service
public class DemoApiImpl implements DemoApi {
@Override
public String sayHello(String name) {
return "Hello," + name + " from dubbo Demo2222";
}
}
在web
中创建测试控制器DemoController
/**
* @author Created by YangFan.
* @date 2019/7/8
* 功能:
*/
@RequestMapping(value = "/demo")
@RestController
public class DemoController {
@Reference
private DemoApi demoApi;
@RequestMapping(value = "/index")
public String index(){
return demoApi.sayHello("dubbo");
}
}
这里需要注意点地方是,注解@Service
是引入的com.alibaba.dubbo.config.annotation.Service
的包名。
5.运行测试项目
运行DubboWebApplication
和 DubboServiceApplication
可以在dubbo
后台中发现api
已经注册到后台
然后访问
http://localhost:8092/dubbo-web/demo/index
,可以返回相关内容image.png
把项目进行maven打包,
service
中得到dubbo-service.jar
包,将得到的 jar
包放在另一台机器中运行,我这里在虚拟机中运行的(拟机环境是centos7),运行后我们在进入dubbo-admin
后台中,可以看到DemoAPi
就有两个提供者了。image.png
现在修改当前项目的DemoApiImpl
打印的信息,然后重新运行项目,在地址栏输入http://localhost:8092/dubbo-web/demo/index
,不断的刷新,可以看到返回的结果是随机的展现出来的,基本完成了dubbo
与zookeeper
的项目。