1. 方法远程调用代码的对比
// 使用远程方法调用框架
@Controller
public class HelloController {
// 将封装了远程服务功能的接口类型的bean装配到当前组件
@Autowired
private EmpRemoteService empRemoteService;
@RequestMapping("/aaa/bbb/ccc")
public String doXxx() {
// 像调用本地方法一样调用远程方法
List<Emp> list = empRemoteService.getEmpList();
// ...
}
}
// 不使用远程方法调用框架
// 1.创建HttpClient实例
CloseableHttpClient client = HttpClientBuilder.create().build();
try {
// 2.声明服务器端URL地址
String url = "http://[服务器端实际IP]:54321/bookManager/book/getBook/23?requestParam=AAA";
// 3.创建具体请求方式实例
HttpGet get = new HttpGet(url);
// 4.调用客户端对象执行请求,进而获得响应对象
CloseableHttpResponse response = client.execute(get);
// 5.从响应结果中获取封装响应数据的Entity对象
HttpEntity entity = response.getEntity();
// 6.借助工具类将HttpEntity中包含的数据转换成可识别的字符串
String responseData = EntityUtils.toString(entity, "UTF-8");
// 7.处理响应结果
System.out.println(responseData);
//Ensures that the entity content is fully consumed and the content stream, if exists, is closed.
//确认entity的内容已经完全被consume了,而且如果内容的流存在,确认其已经关闭了。
EntityUtils.consume(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 8.释放连接,无论操作是否成功都必须释放连接
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. 远程方法调用框架和注册中心工作机制
2.1 技术方案一
Dubbo作为远程方法调用框架+Zookeeper作为注册中心
2.2 技术方案二
SpringBoot+SpringCloud
- →Eureka作为注册中心
- →Feign作为远程方法调用框架
3. Zookeeper
3.1 简介
ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them, which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed.
3.2 树形目录结构
ZooKeeper使用树形结构管理数据。而且以“/”作为树形结构的根节点。树形结构中的每一个节点都称为“znode”。文件系统中的目录可以存放其他目录和文件,znode中可以存放其他znode,也可以对应一个具体的值。znode和它对应的值之间是键值对的关系。
每一个znode上同时还有一套状态信息,称为:stat。
3.3 异步通知机制
在分布式项目中随着业务功能越来越多,具体的功能模块也会越来越多,一个大型的电商项目能达到几十个模块甚至更多。这么多业务模块的工程有可能需要共享一些信息,这些信息一旦发生变化,在各个相关模块工程中手动逐一修改会非常麻烦,甚至可能发生遗漏,严重的可能导致系统崩溃,造成经济损失。
使用ZooKeeper的通知机制后,各个模块工程在特定znode上设置Watcher(观察者)来监控当前节点上值的变化。一旦Watcher检测到了数据变化就会立即通知模块工程,从而自动实现“一处修改,处处生效”的效果。
3.4 leader-follower集群
4. Zookeeper 安装
4.1 环境准备
Zookeeper需要在JVM虚拟机上运行,所以一定要保证有JDK支持。
[root@rich redis]# java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
4.2 上传 Zookeeper
zookeeper-3.4.9.tar.gz
4.3 解压
tar -zxvf /opt/zookeeper-3.4.9.tar.gz
4.4 准备配置文件
cp /opt/zookeeper-3.4.9/conf/zoo_sample.cfg /opt/zookeeper-3.4.9/conf/zoo.cfg
Zookeeper要求配置文件的文件名必须是:zoo.cfg
4.5 创建数据目录
mkdir /opt/zookeeper-3.4.9/data
4.6 在zoo.cfg中配置数据目录的位置
dataDir=/opt/zookeeper-3.4.9/data
待续---