在项目开发过程中,不可避免的需要用到数据库,本文就添加以非关系型数据库redis作为例子,为一个项目添加eredis依赖,实现“向服务器请求,服务器从redis中取数据并返回”。

一、说明

  • 本文环境:windows、idea、rebar3,搭建可以参考windows下idea使用rebar3
  • rebar3依赖相关文档连接
  • 本文使用的rebar3指令:rebar3 compile、rebar3 shell、rebar3 deps
  • 本文搭建内容为cowboy依赖后续,继续对test.app进行搭建,目的是使test.app能够便捷使用redis接口

二、添加eredis

1、eredis下载

  • eredis-1.2.0下载地址
  • 下载完压缩包,解压到deps文件夹下,改名为eredis

2、修改配置

  • 修改test项目的rebar.config,在deps列表中添加eredis
{erl_opts, [debug_info]}.
{deps, [lager, protobuff, cowboy, eredis]}. %% 添加eredis{shell, [% {config, "config/sys.config"},{apps, [test]}
]}.
  • 修改test.app.src,在application列表中添加eredis
{application, test,[{description, "An OTP application"},{vsn, "0.1.0"},{registered, []},{mod, {test_app, []}},{applications,[kernel,stdlib,lager,protobuff,cowboy,eredis]},{env,[]},{modules, []},{licenses, ["Apache-2.0"]},{links, []}]}.

3、修改代码

  • 修改test_path.erl,使得向服务器请求,服务器将redis中的数据取出并返回
-module(test_path).
-author("mechrevo").-export([init/2]).init(Req0, Opts) ->{ok, C} = eredis:start_link(),  %% 建立连接,默认ip127.0.0.1,默认端口6379Info = case eredis:q(C, ["GET", "info"]) of {ok, undefined} -> <<"redis no info">>;{ok, Value} -> Value;_ -> <<"redis get info error">>end,Req = cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain">>}, Info, Req0),{ok, Req, Opts}.

三、测试

1、下载redis,并启动redis

  • windows下使用redis,需要下载github下release的redis
  • 下载完,解压,启动redis-server.exe
  • 启动redis-cli.exe,添加set info test

2、执行rebar3 shell

"C:\Program Files\erl10.4\bin\escript.exe" E:\test\rebar3 shell
===> Analyzing applications...
===> Compiling gpb
===> Compiling rebar3_gpb_plugin
===> Verifying dependencies...
===> App ranch is a checkout dependency and cannot be locked.
===> App goldrush is a checkout dependency and cannot be locked.
===> App cowlib is a checkout dependency and cannot be locked.
===> App lager is a checkout dependency and cannot be locked.
===> App eredis is a checkout dependency and cannot be locked.
===> App cowboy is a checkout dependency and cannot be locked.
===> Analyzing applications...
===> Compiling cowlib
===> Compiling ranch
===> Compiling cowboy
===> Compiling goldrush
===> Compiling lager
===> Compiling eredis
===> Analyzing applications...
===> Compiling protobuff
===> Compiling test
Eshell V10.4  (abort with ^G)
1> 22:00:53.704 [info] test lager
1> 22:00:53.706 [info] person id 1 name "test" email "mahuateng@qq.com"
1> ===> Booted syntax_tools
1> ===> Booted compiler
1> ===> Booted goldrush
1> ===> Booted lager
1> ===> Booted protobuff
1> ===> Booted cowlib
1> ===> Booted ranch
1> ===> Booted cowboy
1> ===> Booted eredis
1> ===> Booted test
1>

3、cmd执行 curl http:/localhost:8080/path,可以看到返回test

4、在redis-cli中删除info,执行del info,再在cmd执行curl,可以看到返回redis no info

四、其他

eredis的搭建比较简单,但是在项目中,不会直接如此使用(每次请求来的时候才建立连接,会配合poolboy先建立连接池,有请求的时候直接调用连接池中已经建立的连接,可以节省频繁建立连接的开销。后面在实践完emysql依赖之后,会添加poolboy依赖,对数据库建立连接池,并对redis大部分指令进行封装,方便调用。
附上现在的目录结构:

【erlang】【rebar依赖】添加eredis依赖相关推荐

  1. [erlang] [rebar依赖] app使用lager依赖

    [erlang] [rebar3依赖] lager依赖 一.rebar3添加依赖 这里介绍一下rebar3依赖的声明和使用,其他更多的依赖项内容可以看官方文档rebar3依赖相关文档连接 1.声明依赖 ...

  2. erlang rebar 配置mysql_Erlang Rebar 使用指南之四:依赖管理

    Erlang Rebar 使用指南之四:依赖管理 全文目录: 本章链接: 1 rebar依赖定义 Rebar取得和构建符合OTP/Rebar规范的项目.如果项目包含子项目,Rebar会自动递归地构建它 ...

  3. Maven 手动添加第三方依赖包及编译打包和java命令行编译JAVA文件并使用jar命令打包...

    一,实例:新建了一个Maven项目,在eclipse中通过 build path –> configure path-.将依赖包添加到工程中后,eclipse不报错了.但是用Maven命令 mv ...

  4. java 手动编译打包_Maven 手动添加第三方依赖包及编译打包和java命令行编译JAVA文件并使用jar命令打包...

    一,实例:新建了一个Maven项目,在eclipse中通过 build path –> configure path-.将依赖包添加到工程中后,eclipse不报错了.但是用Maven命令 mv ...

  5. java手动编译jar包_Maven 手动添加第三方依赖包及编译打包和java命令行编译JAVA文件并使用jar命令打包...

    一,实例:新建了一个Maven项目,在eclipse中通过 build path –> configure path-.将依赖包添加到工程中后,eclipse不报错了.但是用Maven命令 mv ...

  6. Maven添加本地依赖

    在写本文的时候先来说明一下maven依赖的各种范围的意思 compile(编译范围)        compile 是默认的范围:如果没有提供一个范围,那该依赖的范围就是编译范围.编译范围依赖在所有的 ...

  7. android将项目添加到github,将github库作为依赖添加到Android-Studio项目中

    将github库作为依赖添加到Android-Studio项目中 我试图从https://github.com/chrisbanes/ActionBar-PullToRefresh/wiki/Quic ...

  8. Springboot2.0 集成 Elasticsearch 6.x 未添加 transport-netty4-client 依赖 启动时报错

    报错内容关键部分: Failed to instantiate [org.elasticsearch.client.transport.TransportClient]: Factory method ...

  9. JavaWeb编程中如果jar包存在但显示无法输出,就在IDEA的项目发布中添加lib依赖!

    JavaWeb编程中如果jar包存在但显示无法输出,就在IDEA的项目发布中添加lib依赖! 第一步:点击设置选择项目结构 第二步:选择工件artifact,选子模块,然后再WEB-INF目录下添加l ...

最新文章

  1. 矩阵二范数(norm)
  2. JZOJ 5660. 【HNOI2018D2T3】道路
  3. 函数的递推matlab,关于递推最小二乘法辨识参数的matlab编程(含注释)
  4. python元类_python中的元类 metaclass
  5. C#灰度图转伪彩色图
  6. python元组与列表的区别、简答题_细解python面试题(一)元组和列表的区别
  7. 女生学计算机未来出路,计算机真的已经烂大街了吗,女生学计算机没出路吗?...
  8. Qt工作笔记-Qt5中中文编码方面的笔记
  9. 利用Gtmetrix检测你的网站载入速度!
  10. [转]SSH框架搭建
  11. roboware studio教程_Roboware 常见操作和问题
  12. 透明图片下载求全透明png图片_微信“全透明”模式,让你的微信真正实现隐身效果!...
  13. 思科华为网络工程师必修-什么是VTP,vlan同步技术。
  14. 向量交点坐标公式_直线的交点坐标与距离公式
  15. 可用性及测试方法小介绍
  16. ObjectMapper忽略多余字段
  17. linux服务器新装hba卡,EmulexHBA卡在Linux下的安装方法
  18. 互联网大公司与创业公司
  19. 机器视觉硬件—相机镜头的选型
  20. 实体服务器搭建vps系统,vps系统和云服务器搭建

热门文章

  1. Three.js——天空盒
  2. 汉画轩深度结合区块链技术 让国学真正活起来
  3. mumu血压计母亲节“拼团”盛大开启,百度与厂商打造双赢局面
  4. Java中比较常见的主流框架及相关技术合集
  5. 区块链当前的发展现状
  6. 制作坦克大战,坦克移动代码
  7. 《漫画中国式项目管理》----蒋昕炜 ,读书笔记
  8. repos install.packages()安装镜像
  9. 《项目管理中国式漫画》读后感
  10. 多CPU/多核/多进程/多线程/并发/并行之间的关系