重视王有志,一个共享硬核Java技术的互金摸鱼侠
欢迎你参加Java人的提桶跑路群:共同富裕的Java人
上一篇咱们一同认识了Dubbo与RPC,今日咱们就来一同学习怎么运用Dubbo,并将Dubbo集成到Spring Boot的项目中。咱们来看下今日要运用到的软件及版别:
软件 | 版别 | 阐明 |
---|---|---|
Java | 11 | |
Spring Boot | 2.7.13 | Spring Boot 3.0版别开端,最低支持到Java 17 |
Dubbo | 3.2.2 | |
Zookeeper | 3.8.1 | 作为Dubbo的注册中心 |
注册中心的选择没有什么强制要求,咱们这儿以Zookeeper为例来做示范。
TIps:今日咱们只处理Spring Boot,Dubbo与Zookeeper的集成,不会涉及到其他框架的集成。
布置Zookeeper
咱们先来布置一个Zookeeper,这儿我供给Windows和Linux的布置教程,假如你现已布置成功,能够跳过这部分内容。
首要是下载Zookeeper,这儿咱们选择Zookeeper-3.8.1版别。
Linux下能够运用wget
命令下载:
wget https://archive.apache.org/dist/zookeeper/zookeeper-3.8.1/apache-zookeeper-3.8.1-bin.tar.gz
Tips: 留意,这儿咱们下载的是apache-zookeeper-3.8.1-bin.tar.gz这个文件。
Linux布置Zookeeper
解压Zookeeper:
tar -zxvf apache-zookeeper-3.8.1-bin.tar.gz
Zookeeper的装备文件,装备文件坐落../Zookeeper/conf
目录下,仿制zoo_sample.cfg文件,并命名为zoo.cfg:
cp zoo_sample.cfg zoo.cfg
在Dubbo的学习中,咱们运用Zookeeper的默认装备即可,所以此处咱们不需求再修正zoo.cfg。
修正profile文件:
vim /etc/profile
将Zookeeper的装备添加到profile文件中:
export ZOOKEEPER_HOME=/opt/opt/apache-zookeeper-3.8.1-bin
export PATH=$ZOOKEEPER_HOME/bin:$PATH
export PATH
刷新profile文件:
source profile
发动Zookeeper:
zkServer.sh start
正常情况下会输出日志:
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.8.1-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
能够运用命令来检查Zookeeper的状态:
zkServer.sh status
单机状态输出如下日志:
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.8.1-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone
到这儿咱们就在Linux服务器上成功的布置了一个简略可用的单机版Zookeeper了。
Windows布置Zookeeper
参照Linux上布置Zookeeper的过程来到修正装备文件的部分,Windows上咱们要修正dataDir的途径,并新增dataLogDir的途径:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=D:\\Apache\\Apache Zookeeper 3.8.1\\data
dataLogDir=D:\\Apache\\Apache Zookeeper 3.8.1\\logs
clientPort=2181
假如系统中没有装备环境变量JAVA_HOME,咱们要修正zkEnv.cmd中的JAVA_HOME装备,该文件坐落../Apache Zookeeper/bin
目录下:
@echo off
REM Licensed to the Apache Software Foundation (ASF) under one or more
REM contributor license agreements. See the NOTICE file distributed with
REM this work for additional information regarding copyright ownership.
REM The ASF licenses this file to You under the Apache License, Version 2.0
REM (the "License"); you may not use this file except in compliance with
REM the License. You may obtain a copy of the License at
REM
REM http://www.apache.org/licenses/LICENSE-2.0
REM
REM Unless required by applicable law or agreed to in writing, software
REM distributed under the License is distributed on an "AS IS" BASIS,
REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
REM See the License for the specific language governing permissions and
REM limitations under the License.
set ZOOCFGDIR=%~dp0%..\conf
set ZOO_LOG_DIR=%~dp0%..\logs
REM for sanity sake assume Java 1.6
REM see: http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html
REM add the zoocfg dir to classpath
set CLASSPATH=%ZOOCFGDIR%
REM make it work in the release
SET CLASSPATH=%~dp0..\*;%~dp0..\lib\*;%CLASSPATH%
REM make it work for developers
SET CLASSPATH=%~dp0..\build\classes;%~dp0..\build\lib\*;%CLASSPATH%
set ZOOCFG=%ZOOCFGDIR%\zoo.cfg
@REM setup java environment variables
if not defined JAVA_HOME (
echo Error: JAVA_HOME is not set.
goto :eof
)
set JAVA_HOME=%JAVA_HOME%
if not exist "%JAVA_HOME%"\bin\java.exe (
echo Error: JAVA_HOME is incorrectly set: %JAVA_HOME%
echo Expected to find java.exe here: %JAVA_HOME%\bin\java.exe
goto :eof
)
REM strip off trailing \ from JAVA_HOME or java does not start
if "%JAVA_HOME:~-1%" EQU "\" set "JAVA_HOME=%JAVA_HOME:~0,-1%"
set JAVA="%JAVA_HOME%"\bin\java
咱们将第41行的set JAVA_HOME=%JAVA_HOME%
提前到第36行前,在在判别环境变量中是否存在JAVA_HOME前进行设置,并运用完好的途径,如:set JAVA_HOME=D:\Java\jdk11.0.18
。
上述内容修正完结后直接双击执行zkServer.cmd就能够发动Zookeeper了。留意,Windows下发动或许出现各种各样的过错,最常见的如发动闪退,这时能够在PowerShell中发动zkServer.cmd,来检查日志解决问题:
cd D:
PS D:\> cd '.\Apache\Apache Zookeeper 3.8.1\bin\'
PS D:\Apache\Apache Zookeeper 3.8.1\bin> .\zkServer.cmd
好了,到这儿你应该现已完结了Zookeeper的布置工作了,接下来咱们在Spring Boot运用中集成Dubbo。
Dubbo的XML方法集成
首要咱们准备两个工程,DubboProviderXML和DubboConsumerXML,仿照服务供给方和服务运用方,创建工程的部分咱们就直接跳过了,信任这一步咱们都没问题。
另外Dubbo官方供给了Dubbo Initializer项目脚手架,方便咱们快速构建Dubbo项目:
运用方法类似于Spring Initializr:
Tips:
-
当然你也能够在同一个工程中模仿供给方和运用方;
-
Spring Initializr严厉意义上是一个错别字,但咱们也都接受了这种方法,V2EX上有关于名字的讨论。
DubboProviderXML工程
在DubboProviderXML项目下创建了两个子工程:
-
provider-api,声明RPC接口
-
provider-service,核心事务逻辑的完结
此刻工程结构如下:
现在修正DubboProviderXML工程的POM文件:
<modelVersion>4.0.0</modelVersion>
<groupId>com.wyz</groupId>
<artifactId>DubboProviderXML</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>DubboProviderXML</name>
<description>DubboProviderXML</description>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>provider-api</module>
<module>provider-service</module>
</modules>
一般我会在DubboProviderXML工程中添加一切子项目都会用到的依靠,例如:lombok,commons-lang3等,在此咱们忽略这部分内容。
装备provider-api
接着处理子工程provider-api中的POM文件:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.wyz</groupId>
<artifactId>DubboProviderXML</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>provider-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider-api</name>
<packaging>jar</packaging>
<description>provider-api</description>
然后在provider-api中声明RPC接口:
package com.wyz.api;
/**
* @author wyz
* @version 1.0
* @date 2023/6/28
*/
public interface DubboDemoXMLService {
String say(String message);
}
这样provider-api就装备完结了,DubboProviderXML就有了对外供给RPC服务的入口。
Tips:需求将provider-api打包成jar,以便DubboConsumerXML运用。
装备provider-service
下面咱们装备子工程provider-service的POM文件:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.wyz</groupId>
<artifactId>DubboProviderXML</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>provider-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider-service</name>
<description>provider-service</description>
<properties>
<spring.boot.version>2.7.13</spring.boot.version>
<dubbo.version>3.2.2</dubbo.version>
<zookeeper.version>3.8.1</zookeeper.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<!-- dubbo-spring-boot-starter引入了dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- DUbbo与Zookeeper的连接器 -->
<!-- curator5自身引入了Zookeeper的依靠,因此无需额外引入 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.wyz</groupId>
<artifactId>provider-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
接下来咱们在provider-service中完结这个接口:
package com.wyz.service.impl;
import com.wyz.DubboDemoXMLService;
/**
* @author wyz
* @version 1.0
* @date 2023/7/3
*/
public class DubboDemoXMLServiceImpl implements DubboDemoXMLService {
@Override
public String say(String message) {
return "XML Provider say : " + message;
}
}
现在,咱们有了接口,也有了接口的完结,只需求装备成Dubbo的服务即可,新增dubbo-provider.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- Dubbo的根本装备-->
<dubbo:application name="DubboProviderXML" qos-port="2222"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo"/>
<!-- 装备供给的服务dubboDemoXMLServiceImpl -->
<bean id="dubboDemoXMLServiceImpl" class="com.wyz.service.impl.DubboDemoXMLServiceImpl"/>
<dubbo:service interface="com.wyz.api.DubboDemoXMLService" ref="dubboDemoXMLServiceImpl"/>
</beans>
一切装备完结后,咱们导入dubbo-provider.xml文件:
package com.wyz.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
/**
* @author wyz
* @version 1.0
* @date 2023/7/3
*/
@SpringBootApplication
@ImportResource(locations = "classpath:dubbo-provider.xml")
public class ProviderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderServiceApplication.class, args);
}
}
到这儿咱们的DubboProviderXML工程就装备完了,此刻工程的结构如下:
DubboConsumerXML工程
咱们继续装备服务运用方DubboConsumerXML工程,全体流程和DubboProviderXML的装备根本一致,咱们直接快进到consumer-service中的装备部分。
装备consumer-service
consumer-service的POM文件与DubboProviderXML工程的子工程provider-service完全一致,咱们也直接跳过这部分内容。
接下来完结对DubboDemoXMLService#say
的调用:
package com.wyz.service.impl;
import com.wyz.DubboDemoXMLService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
* @author wyz
* @version 1.0
* @date 2023/7/3
*/
@Component
public class DubboConsumerXMLService implements CommandLineRunner {
@Autowired
DubboDemoXMLService dubboDemoXMLServiceImpl;
@Override
public void run(String... args) {
String message = dubboDemoXMLServiceImpl.say("wyz");
System.out.println(message);
}
}
接着咱们新增dubbo-consumer.xml文件,装备运用方要调用的服务:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- Dubbo的根本装备-->
<dubbo:application name="DubboConsumerXML" qos-port="2223"/>
<dubbo:registry address="zookeeper://106.75.33.232:2181"/>
<dubbo:protocol name="dubbo"/>
<!-- 装备需求调用的服务 -->
<dubbo:reference id="DubboDemoXMLService" interface="com.wyz.api.DubboDemoXMLService"/>
</beans>
相同的,咱们导入dubbo-consumer.xml文件:
package com.wyz.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
/**
* @author wyz
* @version 1.0
* @date 2023/7/3
*/
@SpringBootApplication
@ImportResource(locations = "classpath:dubbo-consumer.xml")
public class ConsumerServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerServiceApplication.class, args);
}
}
最后,咱们发动程序,控制台应该输出如下日志:
此刻DubboConsumerXML的工程结构如下:
Dubbo的注解方法集成
除了运用XML外,咱们还能够运用注解的方法装备Dubbo(毕竟有很多人不喜欢XML)。注解方法的装备非常简略,相同的咱们需求先创建两个工程DubboProviderAnnotation和DubboConsumerAnnotation。
DubboProviderAnnotation工程
DubboProviderAnnotation工程与DubboProviderXML的装备过程根本一致,咱们快进到接口的完结部分,这次咱们需求运用@DubboService
注解声明这是一个Dubbo服务:
package com.wyz.service.impl;
import com.wyz.api.DubboProviderAnnotationService;
import org.apache.dubbo.config.annotation.DubboService;
/**
* @author wyz
* @version 1.0
* @date 2023/7/3
*/
@DubboService
public class DubboProviderAnnotationServiceImpl implements DubboProviderAnnotationService {
@Override
public String say(String message) {
return "DubboProviderAnnotationService say : " + message;
}
}
接口有了,完结也有了,咱们来装备Dubbo的相关内容,这次咱们运用YAML装备:
dubbo:
application:
name: DubboProviderAnnotation
qos-port: 2222
protocol:
name: dubbo
registry:
address: zookeeper://${zookeeper.address:127.0.0.1}:2181
接着,咱们在发动类上添加@EnableDubbo
注解,敞开Dubbo的主动注入:
package com.wyz.service;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author wyz
* @version 1.0
* @date 2023/7/3
*/
@SpringBootApplication
@EnableDubbo
public class ProviderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderServiceApplication.class, args);
}
}
此刻DubboProviderAnnotation的结构如下:
最后咱们就能够正常发动DubboProviderAnnotation项目了。
DubboConsumerAnnotation工程
有了前面的根底,信任你一定能够想到DubboConsumerAnnotation该怎么装备,咱们先来写调用DubboDemoAnnotationService#say的调用逻辑,此刻通过@DubboReference
注入接口:
package com.wyz.service.impl;
import com.wyz.api.DubboProviderAnnotationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
* @author wyz
* @version 1.0
* @date 2023/7/3
*/
@Component
public class DubboConsumerAnnotationService implements CommandLineRunner {
@DubboReference
DubboProviderAnnotationService dubboProviderAnnotationService;
@Override
public void run(String... args) {
String message = dubboProviderAnnotationService.say("wyz");
System.out.println(message);
}
}
接着来装备YAML文件:
dubbo:
application:
name: DubboConsumerAnnotation
qos-port: 2223
protocol:
name: dubbo
registry:
address: zookeeper://${zookeeper.address:127.0.0.1}:2181
相同的,咱们需求在服务运用方添加@EnableDubbo
注解,敞开Dubbo的主动注入:
package com.wyz.service;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author wyz
* @version 1.0
* @date 2023/7/3
*/
@SpringBootApplication
@EnableDubbo
public class ConsumerServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerServiceApplication.class, args);
}
}
此刻DubboConsumerAnnotation的结构如下:
最后咱们就能够正常发动DubboConsumerAnnotation项目了。
结语
到这儿咱们就完结了两种方法在Spring Boot中集成Dubbo 3.X。一般来说我会选择运用XML的方法来装备Dubbo供给的服务,但会选择将Dubbo的根本信息,如:协议类型,注册中心地址等装备到YAML文件中,这是为了统一管理对外供给的服务和运用的服务,能够一目了然的看到项目供给了哪些才能,和依靠了哪些外部接口。
好了,今日就到这儿结束了。假如本文对你有帮助的话,请多多点赞支持。最后欢迎咱们重视共享硬核技术的金融摸鱼侠王有志,咱们下次再会!