Docker的安装

Docker的三大核心概念:镜像、容器、仓库
  • 镜像:类似虚拟机的镜像、用俗话说就是安装文件。
  • 容器:类似一个轻量级的沙箱,容器是从镜像创建应用运行实例,可以将其启动、开始、停止、删除、而这些容器都是相互隔离、互不可见的。
  • 仓库:类似代码仓库,是Docker集中存放镜像文件的场所。
1.安装docker

1.检查linux内核版本,返回的值大于3.10即可。

uname -r

2.安装docker

sudo wget -qO- http://get.docker.com|sh

3.查看docker版本

docker version

4.启动docker

service docker start

redis安装

1.搜索镜像(如果docker指令错误,说明docker安装的第4步没有成功)

docker search redis

SRS docker修改配置 docker安装srs_docker


2.拉取镜像

docker pull redis

第一个redis是官方最新的版本,如果想下载低版本的镜像可以

docker pull  redis:3.2

3.查看所有镜像

docker images

4.运行镜像

docker run -p 6379:6379 --name myredis -d redis:latest  --requirepass "123456"
  • -p 6379:6379 : 将容器的6379端口映射到主机的6379端口
  • –name myredis:取名
  • -d redis:latest:运行redis最后一个版本的镜像,比如redis:3.2是运行3.2版本的redis镜像
  • –requirepass “123456” :指定链接redis-server的密码

5.启动镜像(myredis是镜像名):

docker start myredis

6.查看运行的镜像:

docker ps

如果有redis的镜像,说明redis启动成功,用redis管理工具可以连接上

SRS docker修改配置 docker安装srs_springboot_02


6.其他指令

#关闭镜像
docker  stop  myredis
#移除镜像(先关闭再移除,移除后可以通过步骤4开启)
docker rm myredis
#删除镜像文件(先关闭再删除,可以通过镜像ID或者版本号删除,可以通过步骤2重新下载)
docker rmi latest
#查看本地运行和关闭的容器
docker ps -a
打开redis客户端(镜像ID可以根据docker ps查看)
docker exec -it 镜像ID redis-cli
输入密码(没有设置密码就不用输入)
auth 123456
退出客户端(exit指令也可以)
quit
退出客户端并关闭redis服务
shutdown
重启客户端
docker start redis-server
问题分析
1.docker ps指令找不到redis镜像,输入docker ps -a发现存在

【分析】说明redis服务被开启后自动关闭了,在redis的配置文件中注释掉:daemonize yes就可以,可是docker拉取的redis是没有配置文件的,我们可以随便找一个配置文件,然后注释掉这行,并且修改requirepass参数为自己的密码
【解决】
1.进入根目录

cd /

2.新建一个docker文件夹

mkdir /docker

3.在docker文件夹下新建redis文件夹

mkdir /docker/redis

4.在redis文件夹下新建conf\data文件夹

mkdir /docker/redis/conf
mkdir /docker/redis/data

5.创建空的redis.conf配置文件并复制已经修改好的文件内容在上面,保存后退出。或者通过工具将redis.conf文件放在conf文件夹里。
6.这些路径docker和redis是不知道的,以后docker拉取各种中间件、容器都没有配置文件,所以也可以在这些路径下创建对应文件夹方便以后找到以及修改。我们先移除镜像

#移除镜像(先关闭再移除,移除后可以通过步骤4开启)
docker rm myredis

然后回到第4步运行镜像(先执行cd /指令进入根目录)

docker run -p 6379:6379 --name myredis -v $PWD/docker/redis/conf:/etc/redis/conf/redis.conf -v $PWD/docker/redis/data:/data -d redis:latest redis-server /etc/redis/conf/redis.conf --requirepass "123456" --appendonly yes
  • -p 6379:6379 : 将容器的6379端口映射到主机的6379端口
  • –name myredis:取名
  • -d redis:latest:运行redis最后一个版本的镜像,比如redis:3.2是运行3.2版本的redis镜像
  • –requirepass “123456” :指定链接redis-server的密码
  • –appendonly yes:开启数据持久化
  • -v $PWD/docker/redis/conf:/etc/redis/conf/redis.conf: 将主机中当前目录下的redis.conf映射成redis的启动配置文件
  • -v $PWD /docker/redis/data:/data: 将主机中当前目录下 /docker/reids/data挂载到容器的/data
  • redis-server /etc/redis/conf/redis.conf:指定配置文件启动redis-server进程

7.启动镜像(myredis是镜像名):

docker start myredis

8.查看运行的镜像,这样就有了

docker ps
2.远程redis工具或者IDEA无法连接

【分析】IP或者密码输入错误,端口错误都会出现这样的情况
【解决】如果是阿里或者腾讯这种云服务器,需要进入官网控制台→安全组,开发相对应的端口号。如果已经开发,还无法连接,有可能是 运行redis时缺少这句话-p 6379:6379,没有暴露端口

springboot整合redis

1.依赖(我的项目都是springboot2.*的)

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.配置redis

# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.55.26
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=123456
# 连接超时时间(毫秒)
spring.redis.timeout=5000

3.配置Redis序列号类型(可以跳过),配置成json格式,这样redis工具查看的时候都是json格式

SRS docker修改配置 docker安装srs_中间件_03

package com.xiaoyu.client.conf;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;


/**
 * Created by hasee on 2019/11/5.
 */
@Configuration
public class RedisConf{
   @Bean
   @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(jackson2JsonRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(jackson2JsonRedisSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

4.应用Redis

package com.xiaoyu;

import com.xiaoyu.client.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class WallpaintApplicationTests {

	@Autowired
	RedisTemplate redisTemplate;

	@Test
	public void testRedis() {
		User user = new User();
		user.setName("那好");
		user.setUsername("123456789");
		user.setBalance(100);
		redisTemplate.opsForValue().set("user2",user);
		User user2 = (User)redisTemplate.opsForValue().get("user1");
		System.out.println(user2.toString());
	}

}
问题

1.运行时报错,一直报错缺少jar包 找不到类InternalLoggerFactory

Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with
 name 'loginController': Unsatisfied dependency expressed through field 'userService'; nested 
 exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
 creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 
 'redisTemplate'; nested exception is 
 
 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with 
 name 'redisTemplate' defined in class path resource 
 [org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.class]: Unsatisfied 
 dependency expressed through method 'redisTemplate' parameter 0; nested exception is 
 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with 
 name 'redisConnectionFactory' defined in class path resource 
 
 [org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]: 
 Unsatisfied dependency expressed through method 'redisConnectionFactory' parameter 0; 
 nested exception is org.springframework.beans.factory.BeanCreationException: Error creating 
 bean with name 'lettuceClientResources' defined in class path resource 
 [org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]: 
 
 Bean instantiation via factory method failed; nested exception is 
 org.springframework.beans.BeanInstantiationException: Failed to instantiate 
 [io.lettuce.core.resource.DefaultClientResources]: Factory method 'lettuceClientResources' 
 threw exception; nested exception is java.lang.NoClassDefFoundError: io/netty/util/internal/logging/InternalLoggerFactory

【解决】
我的springboot版本说2.1.7,版本太高导致spring-boot-starter-data-redis的依赖需要加入以下依赖

<dependency>
	<groupId>io.netty</groupId>
	<artifactId>netty-all</artifactId>
	<version>4.1.42.Final</version>
</dependency>
<dependency>
	<groupId>io.projectreactor</groupId>
	<artifactId>reactor-core</artifactId>
	<version>3.3.0.RELEASE</version>
</dependency>

虽然问题都解决了,但是很多教程和视频都没有加入这些依赖觉得很不爽。所有可以把springboot版本改为2.0.3就可以,也就不需要加入刚刚那些依赖。

SRS docker修改配置 docker安装srs_中间件_04

redisTemplate.opsForValue()的常用API
0、redis事务
 redisTemplate.multi();//开启事务
 redisTemplate.opsForValue().set(“user2”,user);
 redisTemplate.opsForValue().set(“name”,“aaa”);
 User user2 = (User)redisTemplate.opsForValue().get(“user1”);
 redisTemplate.discard();//提交事务
 1、set(K key, V value)
 新增一个字符串类型的值,key是键,value是值。
 redisTemplate.opsForValue().set(“stringValue”,“bbb”);
 set(K key, V value,Long l,TimeUnit t)
 新增一个字符串类型的值,key是键,value是值,l是过期时间,t是时间单位。
 2、get(Object key)
 获取key键对应的值。
 String stringValue = redisTemplate.opsForValue().get(“key”)
 3、append(K key, String value)
 在原有的值基础上新增字符串到末尾。redisTemplate.opsForValue().append(“key”, “appendValue”);
 String stringValueAppend = redisTemplate.opsForValue().get(“key”);
 System.out.println(“通过append(K key, String value)方法修改后的字符串:”+stringValueAppend);
 4、get(K key, long start, long end)
 截取key键对应值得字符串,从开始下标位置开始到结束下标的位置(包含结束下标)的字符串。String cutString = redisTemplate.opsForValue().get(“key”, 0, 3);
 System.out.println(“通过get(K key, long start, long end)方法获取截取的字符串:”+cutString);
 5、getAndSet(K key, V value)
 获取原来key键对应的值并重新赋新值。String oldAndNewStringValue = redisTemplate.opsForValue().getAndSet(“key”, “ccc”);
 System.out.print(“通过getAndSet(K key, V value)方法获取原来的值:” + oldAndNewStringValue );
 String newStringValue = redisTemplate.opsForValue().get(“key”);
 System.out.println(“修改过后的值:”+newStringValue);
 6、setBit(K key, long offset, boolean value)
 key键对应的值value对应的ascii码,在offset的位置(从左向右数)变为value。redisTemplate.opsForValue().setBit(“key”,1,false);
 newStringValue = redisTemplate.opsForValue().get(“key”)+"";
 System.out.println(“通过setBit(K key,long offset,boolean value)方法修改过后的值:”+newStringValue);
 7、getBit(K key, long offset)
 判断指定的位置ASCII码的bit位是否为1。boolean bitBoolean = redisTemplate.opsForValue().getBit(“key”,1);
 System.out.println(“通过getBit(K key,long offset)方法判断指定bit位的值是:” + bitBoolean);
 8、size(K key)获取指定字符串的长度
Long stringValueLength = redisTemplate.opsForValue().size(“key”);
 System.out.println(“通过size(K key)方法获取字符串的长度:”+stringValueLength);
 9、increment(K key, double delta)以增量的方式将double值存储在变量中。
double stringValueDouble = redisTemplate.opsForValue().increment(“doubleKey”,5);
 System.out.println(“通过increment(K key, double delta)方法以增量方式存储double值:” + stringValueDouble);
 10、increment(K key, long delta)
 以增量的方式将long值存储在变量中。double stringValueLong = redisTemplate.opsForValue().increment(“longKey”,6);
 System.out.println(“通过increment(K key, long delta)方法以增量方式存储long值:” + stringValueLong);
 11、setIfAbsent(K key, V value)如果键不存在则新增,存在则不改变已经有的值。
boolean absentBoolean = redisTemplate.opsForValue().setIfAbsent(“absentKey”,“fff”);
 System.out.println(“通过setIfAbsent(K key, V value)方法判断变量值absentValue是否存在:” + absentBoolean);
 if(absentBoolean){
 String absentValue = redisTemplate.opsForValue().get(“absentKey”)+"";
 System.out.print(",不存在,则新增后的值是:"+absentValue);
 boolean existBoolean = redisTemplate.opsForValue().setIfAbsent(“absentKey”,“eee”);
 System.out.print(",再次调用setIfAbsent(K key, V value)判断absentValue是否存在并重新赋值:" + existBoolean);
 if(!existBoolean){
 absentValue = redisTemplate.opsForValue().get(“absentKey”)+"";
 System.out.print(“如果存在,则重新赋值后的absentValue变量的值是:” + absentValue);
 12、set(K key, V value, long timeout, TimeUnit unit)
 设置变量值的过期时间。redisTemplate.opsForValue().set(“timeOutKey”, “timeOut”, 5, TimeUnit.SECONDS);
 String timeOutValue = redisTemplate.opsForValue().get(“timeOutKey”)+"";
 System.out.println(“通过set(K key, V value, long timeout, TimeUnit unit)方法设置过期时间,
 过期之前获取的数据:”+timeOutValue);
 Thread.sleep(5*1000);
 timeOutValue = redisTemplate.opsForValue().get(“timeOutKey”)+"";
 System.out.print(",等待10s过后,获取的值:"+timeOutValue);
 13、set(K key, V value, long offset)
 覆盖从指定位置开始的值。redisTemplate.opsForValue().set(“absentKey”,“dd”,1);
 String overrideString = redisTemplate.opsForValue().get(“absentKey”);
 System.out.println(“通过set(K key, V value, long offset)方法覆盖部分的值:”+overrideString);
 14、multiSet(Map<? extends K,? extends V> map)
 设置map集合到redis。Map valueMap = new HashMap();
 valueMap.put(“valueMap1”,“map1”);
 valueMap.put(“valueMap2”,“map2”);
 valueMap.put(“valueMap3”,“map3”);
 redisTemplate.opsForValue().multiSet(valueMap);
 15、multiGet(Collection keys)
 根据集合取出对应的value值。//根据List集合取出对应的value值
 List paraList = new ArrayList();
 paraList.add(“valueMap1”);
 paraList.add(“valueMap2”);
 paraList.add(“valueMap3”);
 List valueList = redisTemplate.opsForValue().multiGet(paraList);
 for (String value : valueList){
 System.out.println(“通过multiGet(Collection keys)方法获取map值:” + value);
 }
 16、multiSetIfAbsent(Map<? extends K,? extends V> map)
 Map valueMap = new HashMap();
 valueMap.put(“valueMap1”,“map1”);
 valueMap.put(“valueMap2”,“map2”);
 valueMap.put(“valueMap3”,“map3”);
 redisTemplate.opsForValue().multiSetIfAbsent(valueMap);

晋江小说封面,先放一放

SRS docker修改配置 docker安装srs_springboot_05