Springboot整合Redis

SpringBoot集成redisson max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)max-idle: 10 # 连接池中的最大空闲连接。max-idle: 10 # 连接池中的最大空闲连接。//判断生成号码位数+剩余的补0(8位) 阅读详情

基本的学习流程请查看 泥瓦匠的blog
http://www.bysocket.com/?cat=72

他的各个小节的例子
https://github.com/JeffLi1993/springboot-learning-example

sms-server

POM.XML,由于项目主要是用来发短信服务的,所以其他依赖包加上也无妨,不影响
打war包的方式如下面所示

<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.xialeme</groupId>
    <artifactId>sms-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- 部署到tomcat中需要使用 -->
    <packaging>war</packaging>


    <properties>
        <start-class>com.Application</start-class><!-- main文件路径 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <repositories><!--ali 代码库 -->
        <repository>
            <id>maven-ali</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
            </snapshots>
        </repository>
    </repositories>



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



    <dependencies>
        <!-- 部署到tomcat中需要使用 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!-- <scope>provided</scope> -->
        </dependency>



        <!-- spring boot -->
        <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>

        <!-- 引入 spring-boot-starter-actuator 监控需要-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- 引入 spring-boot-starter-redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
        </dependency>

        <!-- alibaba fastjson httpclient   -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.8</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>

        <!-- jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>


    </dependencies>



    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <skipTests>true</skipTests>
                    <!-- 打成war包时名子 -->
                    <warName>sms-server</warName>
                    <!-- mave时启用的main路径(因为其他包里面测试时也加了main函数) -->
                    <mainClass>${start-class}</mainClass>
                    <skip>true</skip><!-- 跳过测试 -->
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

application.properties配置src/main/resources下面

# 监听端口
server.port=8081
#
#http://blog.youkuaiyun.com/xiaoyu411502/article/details/47865561


########################################################
### tomcat  配置
########################################################
# tomcat最大线程数,默认为200  
#server.tomcat.max-threads=800  
# tomcat的URI编码  
#server.tomcat.uri-encoding=UTF-8  
# 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹(如:C:\Users\Shanhy\AppData\Local\Temp)  
#server.tomcat.basedir=D:/springboot-log
# 打开Tomcat的Access日志,并可以设置日志格式的方法:  
#server.tomcat.access-log-enabled=true  
#server.tomcat.access-log-pattern=  
# accesslog目录,默认在basedir/logs  
#server.tomcat.accesslog.directory=  
# 日志文件目录  
logging.path=D:/springboot-log
# 日志文件名称,默认为spring.log  
logging.file=springboot.log  



########################################################
###datasource  配置MySQL数据源;
########################################################
spring.datasource.url=jdbc:mysql://192.168.10.10:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# Hibernate ddl auto (create, create-drop, update)  
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10



## Mybatis 配置 #由于使用的是jpa所以这里就注释掉mybatis
#mybatis.typeAliasesPackage=org.spring.springboot.domain
#mybatis.mapperLocations=classpath:mapper/*.xml


########################################################
### Java Persistence Api 自动进行建表
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sqlquery
spring.jpa.show-sql = true
# hibernate ddl auto (create,create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy =org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them tothe entity manager)
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect


########################################################
###REDIS (RedisProperties) redis基本配置;
########################################################
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=127.0.0.1
## 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

Application 不能放在java.main的根目录,必须建一个目录存放不然保错
它会自动扫描它所在目录下的子目录的相关注解

package com;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.common.filter.AuthFilter;


/*******
 * Springboot 默认不需要继承SpringBootServletInitializer
 * <p>如果部署到外部的TOMCAT则需要继承SpringBootServletInitializer,并且重写configure
 * pom中添加<packaging>war</packaging>和spring-boot-starter-tomcat.jar
 * </p>
 * @author bamboo
 *
 */
@RestController
@SpringBootApplication
public class Application  {


    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    @RequestMapping("/")
    //@ResponseBody
    String home() {

        return "sms server";
    }


    @RequestMapping("/city")
    //@ResponseBody
    String city(String  id) {
         // 从缓存中获取城市信息
        String key = "city_" + id;
        ValueOperations<String, String> operations = redisTemplate.opsForValue();

        // 缓存存在
        boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            String city = operations.get(key);

            System.out.println("CityServiceImpl.findCityById() : 从缓存中获取了城市 >> " + city.toString());
            return city;
        }

        // 从 DB 中获取城市信息
        String city = "空城";

        // 插入缓存
        operations.set(key, city, 120, TimeUnit.SECONDS);
        System.out.println("CityServiceImpl.findCityById() : 城市插入缓存 >> " + city.toString());

        return city;
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }



}

运行Application main方法

刷新两次浏览器,可以看到控制台打印如下
http://localhost:8081/city?id=1

CityServiceImpl.findCityById() : 城市插入缓存 >> 空城
CityServiceImpl.findCityById() : 从缓存中获取了城市 >> 空城

可以在redis desktop manager 刷新看到数据已经插入进来了

这里写图片描述

SpringBoot系列之集成Redission入门与实践教程 Redisson是一款基于java开发的开源项目,提供了很多企业级实践,比如分布式锁、消息队列、异步执行等功能。本文基于Springboot2版本集成redisson-spring-boot-starter实现redisson的基本应用 阅读详情

相关推荐

SpringBoot开发——整合Redis

SpringBoot整合Redis

bjzhang75的博客 1032

SpringBoot maven环境下整合redis

一 前期准备1.SpringBoot项目+maven环境2.安装好redis服务,并启动(本教程适合 单机,集群,哨兵模式的redis服务)二 配置依赖 &lt;!--redis依赖--&gt; &lt;dependency&gt; &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt; &lt;...

Mars 7972

手把手教你用Multisim搭建FM直接调频电路(附完整波形分析)

本文通过Multisim仿真软件,手把手教你搭建FM直接调频电路。详细介绍了从核心概念理解、压控振荡器(VCO)参数设置、电路搭建与波形观测,到带通滤波器设计与信号优化的完整流程。通过对比滤波前后的时域波形与频谱分析,帮助读者直观理解频率调制原理,并提供了常见问题的排查思路与参数调试方法,将抽象的通信理论转化为可操作的实践技能。

weixin_29230773的博客 214

Spring Boot 整合Redis :spring-boot-starter-data-redis

1.添加maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- redis依赖co...

allen_csdns的博客 4958

因果推断--PSM的原理和应用(二)

上一篇介绍了DID,本文介绍和DID可一起搭配使用的一种因果推断方法PSM。在正式介绍PSM之前,先来简单回顾一下为什么需要因果推断以及如何做因果推断。我们都知道相关非因果,比起相关我们更希望知道两个事件之间的因果关系,有了确切的因果关系,才能知道对A如何操作才能引起B的变化。解决因果推断问题,目前主要有两种框架,Rubin虚拟事实模型和Pearl因果图模型。站在数据分析的角度上,Rubin虚拟事实模型更容易理解,其核心思想是寻找恰当的对照组。

baidu_26137595的博客 1万+

Unity3D实战:5分钟搞定RTSP监控视频流播放(附完整代码)

本文详细介绍了如何在Unity3D中快速实现低延迟RTSP监控视频流播放,提供完整代码和优化方案。通过Video Player组件和FFmpeg集成,开发者可以轻松应对智能家居和安防监控领域的实时视频流挑战,显著降低延迟并提升性能。

weixin_29015051的博客 584

SpringBoot集成redis依赖包及步骤

springBoot 集成redis The following candidates were found but could not be injected

qq_31450641的博客 1522

IAR9.4报错之编码方式

IAR9.40编码方式错误导致的报错

qq_36081075的博客 2624

springboot 整合redis_Spring Boot 整合 Redis

点击上方“Java学习”,选择“置顶公众号”技术文章第一时间送达!作者 |艾神一不小心https://urlify.cn/zemY7z前言实际开发中缓存处理是必须的,不可能我们每次客户端去请求一次服务器,服务器每次都要去数据库中进行查找,为什么要使用缓存?说到底是为了提高系统的运行速度。将用户频繁访问的内容存放在离用户最近,访问速度最快的地方,提高用户的响应速度,今天先来讲下在 Spr...

weixin_39880328的博客 106

Gromacs GPU加速版安装全攻略:从依赖配置到性能优化

本文提供了Gromacs GPU加速版的完整安装与性能优化指南。详细介绍了从编译器、OpenMPI、FFTW到CUDA等关键依赖的配置,手把手指导编译支持CUDA的Gromacs,并深入解析了如何通过调整.mdp参数及平衡MPI与OpenMP线程分配,最大化GPU利用率,从而让分子动力学模拟效率获得数十倍提升。

y9z0a1b的博客 1149

【时空图神经网络 & 交通】相关模型3:STFGNN | 空间-时间依赖性 | 基于数据驱动的方法来生成“时间图” | 时空融合图模块,门控卷积模块 | 动态时间规整 | 顶会文章解析

STFGNN(时空融合图神经网络)在交通流量预测中的优势主要体现在以下几个方面:首先,它提出了一种数据驱动的方法生成“时间图”,弥补了传统空间图可能无法反映的某些相关性。其次,STFGNN通过并行处理不同时间段的空间图和时间图,有效地学习隐藏的时空依赖关系。此外,它将融合图模块与门控卷积模块整合到一个统一层中,通过层层堆叠处理长序列,进一步增强了模型的表达能力。实验结果表明,STFGNN在多个交通数据集上表现出色,优于其他基准方法,展示了其在实际应用中的强大潜力。

追光者♂:记录、分享、总结、提升,现象级专栏《Python从入门到人工智能》作者,无惧黑暗,坚信曙光 382

Spring Boot整合Redis

Redis是一个开源(BSD许可)的、内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件,并提供多种语言的API。Redis支持多种类型的数据结构,如 字符串(strings)、散列(hashes)、列表(lists)、集合(sets)、有序集合(sorted sets)与范围查询、bitmaps、 hyperloglogs 和 地理空间(geospatial)、索引半径查询。

梁辰兴的博客 2万+

静态代码审计插件 snyk 使用教程

静态代码审计插件 snyk 使用教程

SHELLCODE_8BIT的博客 2002

【Spring Boot】SpringBoot整合Redis

SpringBoot整合Redis 1.添加maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 2.配置Redis连接 spring.redis.host=192.168.157.11 spring.re

我们的目标是什么?没有蛀牙!!! 257

SDMA 如何实现设备对主机内存的访问

摘要:SDMA(System Direct Memory Access)是PCIe设备直接访问主机内存的核心机制,使外设无需CPU干预即可高效读写系统RAM。其实现原理包括:设备作为主控发起DMA请求,通过PCIe Memory TLP访问主机物理地址;完整通路涉及驱动分配DMA安全内存、设备配置、地址转换(IOMMU)和中断通知等步骤;关键技术涵盖地址类型(HPA/IOVA)、TLP格式、缓存一致性和安全防护机制。SDMA广泛应用于GPU、网卡、NVMe等高性能场景,是现代异构计算的基础技术,需软硬件协同

MHD0815的博客 239

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【SpringBoot】SpringBoot整合Redis

SpringBoot整合Redis 首先我是在Windows上开发的,因此需要在Windows安装Redis,安装的教程Windows下安装Redis服务,这个教程很容易懂,我按照教程一步一步安装下来,很久就可以运行Redis了。 这个教程在【JAVA】IDEA中SpringBoot整合M...

阳阳的博客 2510

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

SpringBoot整合Redis

今天我们简单使用下spring boot操作redis,废话不多说直接开始整; 环境准备 新建一个spring boot项目 选上这几个,我本地有sptingboot2.6.3版本,我就直接用那个了; 查看项目依赖是否装好 配置文件,其实也不用配,如果你没有配置你的redis,默认也是这个,像我这个默认就是这个都不用配 我们知道redis中有五大基本类型,今天我们就简单学学spri

hello_list的博客 9578

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

springboot整合redis

超详解springboot整合redis!!!

Mr_yu_shao的博客 5102

SpringBoot之SpringBoot整合Redis

SpringBoot整合Redis Spring Boot 集成Redis (1)添加redis的起步依赖 (2) 配置redis的连接信息 spring.redis.host=localhost spring.redis.port=6379 (3)注入RedisTemplate测试redis操作 @SpringBootTest class Demo06redisApplicationTests { @Autowired RedisTemplate<String, String

renVictory的博客 202

Spring Boot——整合Redis

Redis(Remote Dictionary Server)是一个开源的内存数据存储系统,它以键值对(key-value)的形式存储数据,并提供了多种数据结构的支持。Spring Boot简介。

秋书一叶的博客 8070

SpringBoot整合redis

步骤 https://redis.io/download 下载redis安装包,测试或生产环境用的都是linux,这里模拟linux环境 解压文件到指定目录 [root@hadoop03 soft]# tar -zxvf redis-5.0.5.tar.gz -C /usr/local 建立软连接 Linux下 ln 命令详解这是linux中一个非常重要命令。它的功能是为某一个文...

longzhutengyue的博客 2432

Redis常用操作及springboot整合redis

今天学习了redis的基本操作,通过今天的学习,对于redis的基本的环境的配置,和对于数据库的操作和使用已经可以掌握了,因为rdis这个数据类型,其实是和java中的数据类型是非常的相似的,并且对于其操作也是极其的相似,所以在理解和进行操作上是没有什么问题的。字符串操作的时候,当默认对于key的值进行设置都是更新的操作.当使用setnx进行设置其值的时候,会进行判断这个key时候存在。Redis中的有序集合也是String类型的集合,同样不允许有重复成员,不同的是每个元素都会关联double类型的分数.

m0_74205854的博客 1307

SpringBoot整合Redis完整篇

SpringBoot整合Redis完整篇

YIYIYI 2307

Redis安装与使用 + Springboot整合Redis

Redis(Remote Dictionary Server)是一个开源的内存数据库,遵守 BSD 协议,它提供了一个高性能的键值(key-value)存储系统,常用于缓存、消息队列、会话存储等应用场景。对于Redis的安装和使用,以及如何在 Springboot 中整合和使用Redis就介绍到这里啦,下一次我会为大家介绍一款操作 Redis 的可视化工具,让使用 Redis 更加方便。——👦[作者]:向阳256——⏳[更新]:2024.11.3。

一位想要成为优秀全栈工程师的有志青年! 1285
上一篇: 常用JVM命令参数
下一篇: SpringBoot&Swagger构建REST API并生成API文档
牧竹子
博客等级 码龄17年 572粉丝 233原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值