zookeeper的安装与使用
•
大数据
一、下载zookeeper
- Zookeeper是Apacahe Hadoop的子项目,可以为分布式应用程序协调服务,适合作为Dubbo服务的注册中心,负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互。

使用百度网盘分享链接
链接:https://pan.baidu.com/s/1hddTGBzEQGMFQYh28PUAsw?pwd=md5u
提取码:md5u
解压后,进入bin目录 点击zkServer.cmd

然后可以看到自己的端口2181即可。
二、dubbox框架介绍
Dubbo(读音[ˈdʌbəʊ])是阿里巴巴公司开源的一个基于Java的高性能RPC(Remote Procedure Call)框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成。后期阿里巴巴停止了该项目的维护,于是当当网在这之上推出了自己的Dubbox。

节点角色说明:
- Provider: 暴露服务的服务提供方。
- Container: 服务运行容器。
- Registry: 服务注册与发现的注册中心。
- Consumer: 调用远程服务的服务消费方。
- Monitor: 统计服务的调用次调和调用时间的监控中心。
- 调用关系说明:
- 0. 服务容器负责启动,加载,运行服务提供者。
- 1. 服务提供者在启动时,向注册中心注册自己提供的服务。
- 2. 服务消费者在启动时,向注册中心订阅自己所需的服务。
- 3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
- 4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
- 5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
三、dubbox入门案例

3.1 创建父工程 ,引入依赖控制版本
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.2.RELEASE
com.by
dubbox_parent
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
com.alibaba.boot
dubbo-spring-boot-starter
0.1.0
com.101tec
zkclient
0.10
3.2 定义公共接口
里面只简单写一个service接口

- HeflloService接口
package com.by.service;
public interface HeflloService {
String hello();
}
3.3 创建服务提供方模块

①pom.xml
4.0.0
com.by
dubbox_parent
1.0-SNAPSHOT
dubbox_provider
8
8
UTF-8
com.by
dubbox_interface
1.0-SNAPSHOT
②创建service的实现类HelloServiceImpl.Java
package com.by.service;
import com.alibaba.dubbo.config.annotation.Service;
//import org.springframework.stereotype.Service;
@Service
public class HelloServiceImpl implements HeflloService{
@Override
public String hello() {
return "hello,Dubbox.......";
}
}
③application.properties
server.port=8080 #zookeeper?? dubbo.registry.address=zookeeper://127.0.0.1:2181 #???????,?????????????????? dubbo.application.name=dubbo-consumer #??, Dubbo??????Dubbo?RMI?http?WebService dubbo.protocol.name=dubbo
④创建启动类
package com.by;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboxConsumerApp {
public static void main(String[] args) {
SpringApplication.run(DubboxConsumerApp.class,args);
}
}
3.4 定义服务消费方

①pom.xml
4.0.0
com.by
dubbox_parent
1.0-SNAPSHOT
dubbox_consumer
8
8
UTF-8
com.by
dubbox_interface
1.0-SNAPSHOT
②定义定义controller
package com.by.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.by.service.HeflloService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@Reference
private HeflloService heflloService;
@RequestMapping("/hello")
@ResponseBody // 想给浏览器返回一个json
public String hello(){
return heflloService.hello();
}
}
③ application.properties
server.port=8081 #zookeeper?? dubbo.registry.address=zookeeper://127.0.0.1:2181 #???????,?????????????????? dubbo.application.name=dubbo-consumer #??, Dubbo??????Dubbo?RMI?http?WebService dubbo.protocol.name=dubbo
④定义启动类DubboxConsumerApp.java
package com.by;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboxConsumerApp {
public static void main(String[] args) {
SpringApplication.run(DubboxConsumerApp.class,args);
}
}
四、测试
首先打开zookeeper,然后先启动服务提供方,接着启动服务消费方(注意顺序)
然后在浏览器中搜索服务消费方的网址即可。

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/801ff1e6ba.html
