SpringBoot+Dubbo搭建简单小案例

13 篇文章 0 订阅
1 篇文章 0 订阅

##一、概述
由于公司项目使用了dubbo服务,但是对dubbo的原理和使用还不是很了解,仅以此片记录下dubbo的学习历程。

dubbo是阿里的一个开源分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。在实际应用场景中,可以将主要的业务分离出单独的服务,提供给消费方调用。

二、安装Zookeeper

Zookeeper是一个分布式的服务框架,是树型的目录服务的数据存储,能做到集群管理数据 ,这里能很好的作为Dubbo服务的注册中心。

Dubbo能与Zookeeper做到集群部署,当提供者出现断电等异常停机时,Zookeeper注册中心能自动删除提供者信息,当提供者重启时,能自动恢复注册数据,以及订阅请求

从官网下载安装包,解压。修改cong目录下的zoo-sample.cfg文件,此文件为示例文件,重命名为zoo.cfg。文件内容大致如下:

# 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=D:\\tools\\zookeeper-3.4.13\\tmp
dataLogDir=D:\\tools\\zookeeper-3.4.13\\tmp
# the port at which the clients will connect
clientPort=2181
server.1=192.168.75.129:2888:3888
# 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

1.2888 端口号是zookeeper服务之间通信的端口
2.3888 是zookeeper 与其他应用程序通信的端口
3.initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 52000=10 秒。
4.syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 2
2000=4 秒
5.server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的IP地址或/etc/hosts文件中映射了IP的主机名;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。
原文:https://blog.csdn.net/u012702547/article/details/77569325


在Linux系统下,需要注意修改的就是dataDir, dataLogDir路径,其他同windows相同。服务启动命令在bin目录下,linux是zkServer.sh, Windows是zkServer.cmd。

./zkServer.sh start

三、Dubbo服务端工程代码示例

在这里我使用SpringBoot框架搭建工程。对Dubbo的配置都是写在properties文件中,而不像官网提供的案例,是使用xml方式。个人更喜欢SpringBoot的配置方式,看着简单易懂。
首先看下pom依赖:


	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<dubbo-spring-boot>1.0.0</dubbo-spring-boot>
		<log4j.version>1.2.17</log4j.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>io.dubbo.springboot</groupId>
			<artifactId>spring-boot-starter-dubbo</artifactId>
			<version>${dubbo-spring-boot}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-tomcat</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

由于只是简单示例,pom文件也很简单。
项目的目录,我是直接使用start.spring.io网站生成的项目。
这里写图片描述

再来看一下配置文件

#Dubbo 服务提供者配置
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://192.168.152.128:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocal.port=20880
spring.dubbo.scan=cn.hzr0523.service

接着是简单的demo

@Service
public class DubboDemoServiceImpl implements IDubboDemoService {

    @Override
    public String helloDubbo() {
        return "hello dubbo, I'm server!";
    }
}

接口就不贴了,只贴了接口的实现类。这里注意的是,@Service注解,需要使用spring-boot-starter-dubbo提供的注解,不能够像平常一样使用Spring的注解。

运行:
这里写图片描述
到此,服务端示例代码就完成了。

四、Dubbo消费方工程代码示例

依赖与服务方工程一致:

# Dubbo配置
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://192.168.152.128:2181
spring.dubbo.scan=cn.hzr0523.service

server.port=8081

项目目录:
这里写图片描述

public interface IDemoService {
    String test();
}
public interface IDubboDemoService {
    String helloDubbo();
}

@Reference使用的是Dubbo的注解。

@Service
public class DemoServiceImpl implements IDemoService {
    @Reference
    public IDubboDemoService dubboDemoService;

    @Override
    public String test() {
        return dubboDemoService.helloDubbo();
    }
}

@Controller
@RequestMapping
public class DubboDemoController {

    @Autowired(required = false)
    private IDemoService demoService;

    @RequestMapping("/test.do")
    @ResponseBody
    public String test() {
        return demoService.test();
    }
}

实际项目中,会将接口剥离出来,打成jar包,消费方只需要添加相关依赖即可,减少了代码的耦合。

四、Dubbo控制台

https://github.com/apache/incubator-dubbo-ops,可以在github下载项目,进入admin中,运行mvn命令:mvn spring-boot:run,启动项目即可。注意,如果zookeeper部署到服务器或者虚拟机上,要修改properties配置文件中的注册中心地址。
这里写图片描述
在这里将会看到运行的服务提供者和消费者。

  • 5
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值