写在正文前的絮叨:

其实这个环境的搭建是很简单的,照着官网给的说明很快就可以搭建测试出来。为什么又要写出来呢?只是为了记录、保留、分享这其中遇到的坑。

这个环境之前在架构一个简单系统时,也曾经搭建过,是springsession+redis。这里先落下伏笔(是坑的来源之一)。

正文:

首先要快速搭建一个maven项目,并使用springboot ,可以去spring官网中的 https://start.spring.io/ 快速构建,添加web、redis  pom即可。这里注意选择的版本(也是坑的来源之一)

如图:目录结构。其中标红框中是要我们添加内容的。

  其中:1.DemoApplication.java内容

package com.example.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

public static void main(String[] args) {
  SpringApplication.run(DemoApplication.class, args);
 }
 @Autowired
 private StringRedisTemplate stringRedisTemplate;
 
 @RequestMapping("/age/{age}")
 public String testRedis(@PathVariable String age){
  ValueOperations<String, String> ops =stringRedisTemplate.opsForValue();
  
  ops.set("bbb", age);
  RedisConnection connection = stringRedisTemplate.getConnectionFactory().getConnection();
  List<String> config = connection.getConfig("hostname");
  return age; 
  
 }

}

  2.application.properties内容

# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0 
# Redis服务器地址
spring.redis.host=192.168.
# Redis服务器连接端口
spring.redis.port=6379 
# Redis服务器连接密码(默认为空)
spring.redis.password= 
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8 
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1 
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8 
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0 
# 连接超时时间(毫秒)
spring.redis.timeout=0
server.port = 9999

  3.pom.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
 <artifactId>demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

<name>demo</name>
 <description>Demo project for Spring Boot</description>

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.7.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository 2.0.0.BUILD-SNAPSHOT -->
 </parent>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>

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

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

<build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

<repositories>
  <repository>
   <id>spring-snapshots</id>
   <name>Spring Snapshots</name>
   <url>https://repo.spring.io/snapshot</url>
   <snapshots>
    <enabled>true</enabled>
   </snapshots>
  </repository>
  <repository>
   <id>spring-milestones</id>
   <name>Spring Milestones</name>
   <url>https://repo.spring.io/milestone</url>
   <snapshots>
    <enabled>false</enabled>
   </snapshots>
  </repository>
 </repositories>

<pluginRepositories>
  <pluginRepository>
   <id>spring-snapshots</id>
   <name>Spring Snapshots</name>
   <url>https://repo.spring.io/snapshot</url>
   <snapshots>
    <enabled>true</enabled>
   </snapshots>
  </pluginRepository>
  <pluginRepository>
   <id>spring-milestones</id>
   <name>Spring Milestones</name>
   <url>https://repo.spring.io/milestone</url>
   <snapshots>
    <enabled>false</enabled>
   </snapshots>
  </pluginRepository>
 </pluginRepositories>
</project>

  最后,启动你的redis服务端,并在application.properties中正确写host的值。

启动项目,这个时候就可以发现能够写数据到redis中了。

写在正文结尾的絮叨:

  上面有两处提到坑:

1.是之前有搭建过这样的环境,所以不可避免的会遗留一些配置。我的就是在本机的系统环境变量里添加了redis的连接属性配置,指向的是别的redis地址。这就覆盖了application.properties中的配置,导致我在我以为的目标redis库中永远看不到写的数据。

2.是提到的构建系统时注意版本。。我本着用新的原则,使用的是2.0.0.BUILD-SNAPSHOT版本构建的,同样的配置。启动会报找不到RedisConnectionFactory  这个Bean的错误。这个一般不会想到是版本的问题,但是我之前有搭建过,所以进行比较下,

我将原先的版本1.5.2.RELEASE进行替换,结果是成功的,然后是1.4.7.RELEASE也是可以的,但是1.3.1.RELEASE也会报错。

====总结:1.历史遗留 2.版本比较

祝好

SpringBoot+Redis环境搭建的更多相关文章

  1. Redis环境搭建和代码测试及与GIS结合的GEO数据类型预研

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 1.1传统MySQL+ Memcached架构遇到的问题 My ...

  2. redis环境搭建及一主二从三哨兵模式配置

    一.单机redis环境搭建 1.安装: OS:linux redhat6.5 下载redis 官网下载链接:https://redis.io/download 把安装包上传到服务器,进行解压 [roo ...

  3. nginx + lua +redis环境搭建

    环境搭建,其实主要是lua的环境,这个环境够麻烦的,在网上找了很多前辈的文章,终于完成了 ,安装redis wget http://download.redis.io/releases/redis-3 ...

  4. django+celery+redis环境搭建

    初次尝试搭建django+celery+redis环境,记录下来,慢慢学习~ 1.安装apache 下载httpd-2.0.63.tar.gz,解压tar zxvf httpd-2.0.63.tar. ...

  5. Redis环境搭建(Linux)

    1.简介       redis是一个开源的key-value数据库.它又经常被认为是一个数据结构服务器.因为它的value不仅包括基本的string类型还有 list,set ,sorted set ...

  6. 一.redis 环境搭建

    1.简介       redis是一个开源的key-value数据库.它又经常被认为是一个数据结构服务器.因为它的value不仅包括基本的string类型还有 list,set ,sorted set ...

  7. Redis环境搭建(MacOS)

    Redis是一个开源的key-value类型的存储系统,大部分数据存在于内存中,所有读写速度十分快.其支持的存储value数据类型有多种,如:strings.hashes.lists.sets.sor ...

  8. Redis环境搭建

    一.准备的安装包 windows虚拟机软件:VMware Workstation Pro 12 linux安装文件:CentOS-7-x86_64-Minimal-1511.iso 远程登录软件:pu ...

  9. 浅谈基于Linux的Redis环境搭建

    本篇文章主要讲解基于Linux环境的Redis服务搭建,Redis服务配置.客户端访问和防火强配置等技术,适合具有一定Linux基础和Redis基础的读者阅读. 一  Redis服务搭建 1.在根路径 ...

随机推荐

  1. Akka(29): Http:Server-Side-Api,Low-Level-Api

    Akka-http针对Connection的两头都提供了方便编程的Api,分别是Server-Side-Api和Client-Side-Api.通过这两个Api可以大大提高编程效率.当然,上期我们提到 ...

  2. 超级简单的retrofit使用自签名证书进行HTTPS请求的教程

    1. 前言 HTTPS越来越成为主流,谷歌从 2017 年起,Chrome 浏览器将也会把采用 HTTP 协议的网站标记为「不安全」网站:苹果从 2017 年 iOS App 将强制使用 HTTPS: ...

  3. Struts2运行流程-源码剖析

    本文为原创,如需转载,请标明出处:http://www.cnblogs.com/gudu1/p/7726172.html 首先说一下查看这些框架源码的感受,每一次深入探究 Spring.Struts ...

  4. Java8 新特性之Stream----java.util.stream

    这个包主要提供元素的streams函数操作,比如对collections的map,reduce. 例如: int sum = widgets.stream() .filter(b -> b.ge ...

  5. [Java Web 第一个项目]客户关系处理系统(CRM)项目总结

    一.table的应用: 1.表格的常用属性 基本属性有:width(宽度).height(高度).border(边框值).cellspacing(表格的内宽,即表格与tr之间的间隔).cellpadd ...

  6. 【ASP.NET MVC 学习笔记】- 20 ASP.NET Web API

    本文参考:http://www.cnblogs.com/willick/p/3441432.html 1.ASP.NET Web API(本文简称Web API),是基于ASP.NET平台构建REST ...

  7. Mybatis基本用法--中

    Mybatis基本用法--中 第四部分 动态 SQL 动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似.MyBatis 采用功能强大的基于 OGNL 的表达式来消除其他元素 ...

  8. 分布式学习(一)——基于ZooKeeper的队列爬虫

    zookeeper 一直琢磨着分布式的东西怎么搞,公司也没有相关的项目能够参与,所以还是回归自己的专长来吧--基于ZooKeeper的分布式队列爬虫,由于没什么人能够一起沟通分布式的相关知识,下面的小 ...

  9. chromium源码阅读--进程间通信(IPC)

    第一篇就有提到Chromium是目前默认是采用多进程架构,当然,chromium有singe-process的版本. 多进程与多线程的区别,确实有很多可以讲的,我的另一篇博客也讲了一些,这里是从浏览器 ...

  10. MFC程序使用GTest搭建测试框架

    一.起源 最近对单元测试比较感兴趣,之后就上网搜了一些测试的框架,C++项目使用的测试框架基本上都使用的GoogleTest,之后就开启了gtest的学习之路. 主要是根据<玩转Google开源 ...