SpringBoot与Dubbo的集成、配置与部署(1)准备工作

前言

Dubbo是一款由阿里所开源的分布式服务框架,目前已提交至Apache。

Apache Dubbo:https://dubbo.apache.org/

Dubbo官方中文文档:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html

在Dubbo官网的文档中,主要介绍的仍然是以传统Spring方式进行配置和使用。对于目前以升级到2.3版本的SpringBoot来说,很多项目已经使用其它一些服务治理架构,例如Eureka。

SpringCloudEureka服务发现的集成、配置和部署(1):Eureka服务端

SpringCloudEureka服务发现的集成、配置和部署(2):Eureka客户端——服务提供者

SpringCloudEureka服务发现的集成、配置和部署(3):Eureka客户端——消费者

两者所围绕的核心主要功能其实颇为类似。但鉴于很多已有的项目可能仍大量存在使用Dubbo框架来处理分布式的任务和调度,因此在这里对此做一些简单的记录。

环境

操作系统:Ubuntu 18.04

集成开发环境:MyEclipse

Java版本:1.8

Dubbo版本:2.7.7

ZooKeeper版本:3.6.1

ZooKeeper

有意思的是,相交于Eureka,Dubbo框架可以在脱离注册中心的情况下,以“服务者-消费者”这样点对点的模式运行和使用。而如果需要对分布式服务有一个集中的管控,那么就需要引入ZooKeeper。

下载ZooKeeper:Apache ZooKeeper

Ummm...一言难尽的阿帕奇设计风格...

我们可以下载最近的版本的压缩包:

将它解压后,放置到一个合适的目录下。进入其中的“/conf”目录,将“zoo_sample.xml”重命名为“zoo.xml”,并且打开进行编辑:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# dataDir=/tmp/zookeeper
dataDir=/home/freezingxu/jobs/ZooKeeper/zooKeeperData
dataLogDir=/home/freezingxu/jobs/ZooKeeper/zooKeeperData/logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

在默认情况下,基本的配置项上都有英文的说明。需要提起注意的是,建议对默认的“dataDir”配置项作出修改,尽量不要使用默认配置下的“/tmp”路径。

另外,可以自行添加“dataLogDir”配置项,该配置项指明了ZooKeeper日志文件的所在。

另外,“clientPort”虽然名为“client”,但其实指的是ZooKeeper自身服务的端口。之所以起名叫做“client”,正如其上方的英文注释所表达的,是“客户端”可以通过这个端口来连接ZooKeeper。该端口默认为2181,可以无需修改。

保存该配置文件。

然后转至目录“/bin”下,该目录下有许多Bash脚本文件和cmd文件:

很显然,在Linux环境下,我们只需要关注.sh文件即可。

使用以下的命令来启动ZooKeeper服务:

./zkServer.sh start

启动成功后,会看到如下提示:

可以用下面的命令来查看服务状态:

./zkServer.sh status

以下是重启及关闭服务的基本命令:

./zkServer.sh restart
./zkServer.sh stop

我们还可以使用ZooKeeper Client来尝试连接服务,仍然是在目录“/bin”的位置,使用以下的命令进行连接:

./zkCli.sh -server 127.0.0.1:2181

经过一长串的信息输出后,最后得到以下的信息:

至此,ZooKeeper基本的准备已经完成。

关于ZooKeeper其它更多的使用方法,可以详见官方网站所提供的文档:

官方文档:ZooKeeper3.6.1

Dubbo Admin

Dubbo Admin是Dubbo团队所提供的一个可视化的dubbo管理控制台。在完成了ZooKeeper的安装、配置及启动后,就可以先准备好Dubbo Admin面板了。

下载地址:https://github.com/Radom7/dubbo-admin

下载得到一个压缩包文件,我们需要解压并提取其中的目录“dubbo-admin”:

将整个“dubbo-admin”复制到Tomcat的“webapps”目录下,并进入到其中的“/WEB-INF”目录,编辑其中的“dubbo.properties”文件:

dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

这个文件的内容非常简单,一共只有3行。我们仅需要修改ZooKeeper服务的地址和端口号即可。

完成之后,启动Tomcat,就能够在浏览器中访问Dubbo Admin了:

好了,一切准备工作都已就绪。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot可以很方便地与Dubbo进行集成,实现分布式服务的开发和部署。以下是整合步骤: 1. 引入DubboZookeeper的依赖 在pom.xml文件中添加DubboZookeeper的依赖: ``` <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2..</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> ``` 2. 配置Dubbo 在application.properties文件中添加Dubbo配置: ``` # Dubbo application name dubbo.application.name=your-application-name # Dubbo registry address dubbo.registry.address=zookeeper://127...1:2181 # Dubbo protocol name dubbo.protocol.name=dubbo # Dubbo protocol port dubbo.protocol.port=20880 ``` 3. 编写Dubbo服务 在Spring Boot应用中定义Dubbo服务接口和实现类: ``` public interface HelloService { String sayHello(String name); } @Service public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello " + name; } } ``` 4. 暴露Dubbo服务 在Spring Boot应用中暴露Dubbo服务: ``` @Service public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello " + name; } } @Configuration public class DubboConfig { @Bean public ApplicationConfig applicationConfig() { ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("your-application-name"); return applicationConfig; } @Bean public RegistryConfig registryConfig() { RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress("zookeeper://127...1:2181"); return registryConfig; } @Bean public ProtocolConfig protocolConfig() { ProtocolConfig protocolConfig = new ProtocolConfig(); protocolConfig.setName("dubbo"); protocolConfig.setPort(20880); return protocolConfig; } @Bean public ServiceConfig<HelloService> serviceConfig(HelloService helloService) { ServiceConfig<HelloService> serviceConfig = new ServiceConfig<>(); serviceConfig.setInterface(HelloService.class); serviceConfig.setRef(helloService); serviceConfig.setVersion("1.."); return serviceConfig; } } ``` 5. 调用Dubbo服务 在Spring Boot应用中调用Dubbo服务: ``` @RestController public class HelloController { @Reference(version = "1..") private HelloService helloService; @GetMapping("/hello") public String sayHello(@RequestParam String name) { return helloService.sayHello(name); } } ``` 以上就是Spring Boot整合Dubbo的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值