java 部署环境_配置java redis开发环境以及部署环境

开发环境:win10,myeclipse

部署环境:cent OS6.5

配置开发环境

windows下安装redis数据库

下载地址

Redis 支持 32 位和 64 位。这个需要根据你系统平台的实际情况选择,这里我们下载 Redis-x64-xxx.zip压缩包到 C 盘,解压后,将文件夹重新命名为 redis。

打开一个 cmd 窗口 使用cd命令切换目录到 C:\redis 运行 redis-server.exe redis.windows.conf 。

0818b9ca8b590ca3270a3433284dd417.png

这时候另启一个cmd窗口,原来的不要关闭,不然就无法访问服务端了。

切换到redis目录下运行 redis-cli.exe -h 127.0.0.1 -p 6379 。

设置键值对 set myKey abc

取出键值对 get myKey

0818b9ca8b590ca3270a3433284dd417.png

eclipse配置并使用redis

首先你需要下载驱动包,下载 jedis.jar

在你的classpath中包含该驱动包(如果已经设置环境变量,直接拷贝到%JAVA_HOME%/lib下),也可以后面在自己的工程中设置user library添加。

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

eclipse配置好了。

按照上面的章节的流程,把redis运行起来。

新建一个java类。写个测试代码:

public class TestConnect {

public static void main(String[] args) {

// TODO Auto-generated method stub

Jedis jedis = new Jedis("localhost");

System.out.println("Server is running: " + jedis.ping());

//set and get

jedis.set("pony", "大帅哥");

System.out.println("pony is " + jedis.get("pony"));

}

}

运行,输出:

Server is running: PONG

pony is 大帅哥

redis持久化

上面的测试代码,对”pony”的设置是在内存中的,你把redis退出然后重新启动,再get就取不出”大帅哥”这个值了。

redis持久化其实用得比较少,大部分需要持久化的场景还是用老牌的mysql或者oracle。redis主要还是用来做缓存。

具体怎么持久化操作这里就不说了,有需要的可以自行谷歌。

配置部署环境

前面说到,部署服务器环境是cent OS 6.5,基本的步骤其实就是两个,一是在服务器上安装redis服务并启动,二是把刚才的java应用打包放到服务器上执行。

第一步,服务器安装配置redis

$ wget http://download.redis.io/redis-stable.tar.gz

$ tar -zxvf redis-stable.tar.gz

$ cd redis-stable

$ make

$ make install

$ sudo make install

经过上面的步骤后,在/usr/local/bin目录下,可以看到如下几个文件:

-rwxr-xr-x 1 root root 5580311 3月 29 18:47 redis-benchmark

-rwxr-xr-x 1 root root 22185 3月 29 18:47 redis-check-aof

-rwxr-xr-x 1 root root 7829978 3月 29 18:47 redis-check-rdb

-rwxr-xr-x 1 root root 5709179 3月 29 18:47 redis-cli

lrwxrwxrwx 1 root root 12 3月 29 18:47 redis-sentinel ->redis-server

-rwxr-xr-x 1 root root 7829978 3月 29 18:47 redis-server

改下几个目录结构,让redis的一些配置文件,日志等目录结构更清晰。

[root@localhost etc]# pwd

/etc

[root@localhost etc]# mkdir redis

[root@localhost etc]# cd /var/

[root@localhost var]# mkdir redis

[root@localhost var]# cd redis/

[root@localhost redis]# mkdir data log run

[root@localhost redis]# ll

总用量 12

drwxr-xr-x 2 root root 4096 3月 29 18:57 data

drwxr-xr-x 2 root root 4096 3月 29 18:57 log

drwxr-xr-x 2 root root 4096 3月 29 18:57 run

拷贝解压包下的redis.conf文件至/etc/redis,然后编辑修改一下几个地方:

# Creating a pid file is best effort: if Redis is not able to create it

# nothing bad happens, the server will start and run normally.

pidfile /var/redis/run/redis_6379.pid

...

# The working directory.

#

# The DB will be written inside this directory, with the filename specified

# above using the 'dbfilename' configuration directive.

#

# The Append Only File will also be created inside this directory.

#

# Note that you must specify a directory here, not a file name.

dir /var/redis/data

...

# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the

# internet, binding to all the interfaces is dangerous and will expose the

# instance to everybody on the internet. So by default we uncomment the

# following bind directive, that will force Redis to listen only into

# the IPv4 lookback interface address (this means Redis will be able to

# specified in the configuration. When the server is daemonized, the pid file

logfile /var/redis/log/redis.log

...

# By default Redis does not run as a daemon. Use 'yes' if you need it.

# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.

daemonize yes

最后你一个改动是让redis在后台运行不占用当前的session。

启动redis,

[root@localhost bin]# redis-server /etc/redis/redis.conf

[root@localhost bin]# ps -ef|grep redis

root 36572 1 0 21:35 ? 00:00:00 redis-server 127.0.0.1:6379

root 36616 36549 0 21:35 pts/0 00:00:00 grep redis

停止的话可以直接kill掉redis的进程。

启动后可以用客户连接测试下,

[root@localhost bin]# redis-cli

127.0.0.1:6379> set "pony" "aaa"

OK

127.0.0.1:6379> get "pony"

"aaa"

127.0.0.1:6379>

第二步,发布程序并测试

现在eclipse环境中导出jar包,注意一定要把依赖的jedis的jar也包含进来。

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

导出之后,可以通过ftp或者secureCRT等工具,把RedisDemo.jar拷贝到服务器目录中,然后执行,

$ java -jar RedisDemo.jar

Server is running: PONG

pony is 大帅哥

测试成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值