在分布式系统中,国内常用zookeeper+dubbo的组合,简单的概念看一下,接下来就直接动手吧,以下资料在文章中会用到。
1.配置环境
环境:SpringBoot 2.0.3
安装zookeeper:
Docker
安装zookeeper 输入指令docker pull zookeeper:3.4.12
运行zookeeper 输入指令docker run --name myzookeeper -p 2181:2181 --restart always -d zookeeper
SpringBoot无法连接,未知原因
Windows
- 下载zookeeper:下载地址
- 安装zookeeper:将conf文件下的zoo_sample.cfg改名为zoo.cfg,运行bin下的zkServer,访问地址为localhost:2181
zookeeper暴露了三个端口:
- 2181:client端口。
- 2888:follower端口,用于集群。
- 3888:election端口,用于集群。
这里先科普一下zookeeper集群中的一个概念,zookeeper集群中具有两个关键的角色:Leader和Follower。集群中所有的结点作为一个整体对分布式应用提供服务,集群中每个结点之间都互相连接,所以,在配置的zooKeeper集群的时候,每一个结点的host到IP地址的映射都要配置上集群中其它结点的映射信息。
zookeeper采用一种称为Leaderelection的选举算法。在整个集群运行过程中,只有一个Leader,其他的都是Follower,如果zooKeeper集群在运行过程中Leader出了问题,系统会采用该算法重新选出一个Leader。因此,各个结点之间要能够保证互相连接,必须配置上述映射。
zookeeper 集群启动的时候,会首先选出一个Leader,在Leaderelection过程中,某一个满足选举算的结点就能成为Leader。
2.代码配置
a.工程结构
根据官方示例代码,主要分为三部分。
- provider
- consumer
- api
b.Provider
Maven中导入Dubbo-zookeeper启动配置模块
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
接口实现类,接口统一在api中管理,此处的Service为dubbo中的注释,表示发布服务。
import com.alibaba.dubbo.config.annotation.Service;
import com.example.dubboapi.consumer.DemoService;
/**
*
* @auther: BaoZhou
* @date : 2018/6/28 14:47
*/
@Service(
version = "${demo.service.version}",
application = "${dubbo.application.id}",
protocol = "${dubbo.protocol.id}",
registry = "${dubbo.registry.id}"
)
public class DefaultDemoService implements DemoService {
@Override
public String sayHello(String name) {
return "Hello, " + name + " (from Spring Boot)";
}
}
配置文件,设置实现类的扫描路径以及注册地址。
# Spring boot application
spring.application.name = dubbo-provider-demo
server.port = 9090
management.port = 9091
# Service version
demo.service.version = 1.0.0
# Base packages to scan Dubbo Components (e.g @Service , @Reference)
dubbo.scan.basePackages =com.example.dubboserver.service
# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-provider-demo
dubbo.application.name = dubbo-provider-demo
## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345
## RegistryConfig Bean
dubbo.registry.id = my-registry
dubbo.registry.address = zookeeper://127.0.0.1:2181
c.Consumer
Maven中导入Dubbo-zookeeper启动配置模块
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
Controller调用远程方法,DemoService依旧引用于Api。使用Reference注解来实现远程调用,其中url代表你所要调用方法的地址。
package com.example.dubboconsumer.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.example.dubboapi.consumer.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoConsumerController {
@Reference(version = "${demo.service.version}",
application = "${dubbo.application.id}",
url = "dubbo://localhost:12345")
private DemoService demoService;
@RequestMapping("/sayHello")
public String sayHello(@RequestParam String name) {
return demoService.sayHello(name);
}
}
配置文件
# Spring boot application
spring.application.name = dubbo-consumer-demo
server.port = 8080
management.server.port = 8081
# DemoService service version
demo.service.version = 1.0.0
# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo
## Legacy QOS Config
dubbo.qos.port = 22223
## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345
# Dubbo Endpoint (default status is disable)
endpoints.dubbo.enabled = true
# Dubbo Health
## StatusChecker Name defaults (default : "memory", "load" )
management.health.dubbo.status.defaults = memory
# Enables Dubbo All Endpoints
management.endpoint.dubbo.enabled = true
management.endpoint.dubbo-shutdown.enabled = true
management.endpoint.dubbo-configs.enabled = true
management.endpoint.dubbo-services.enabled = true
management.endpoint.dubbo-references.enabled = true
management.endpoint.dubbo-properties.enabled = true
# Exposes all web endpoints
management.endpoints.web.exposure.include = *
至此,配置完毕。
访问http://localhost:8080/sayHello?name=HelloWorld
如果有如下返回代表成功完成配置
3.DubboAdmin
Dubbo提供了管理平台,访问DubboAdmin工程Github,运行其中的dubbo-admin工程,即可启动对于dubbo的监控。
有关于dubbo+zookeeper的初步使用就介绍到这里了,只能说远程调用是个好东西!