Springboot整合Redis
来源:互联网 发布:收看tvb电视软件 编辑:程序博客网 时间:2024/05/21 10:54
基本的学习流程请查看 泥瓦匠的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.csdn.net/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/testspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driver# Hibernate ddl auto (create, create-drop, update) spring.jpa.properties.hibernate.hbm2ddl.auto=updatespring.datasource.max-active=20spring.datasource.max-idle=8spring.datasource.min-idle=8spring.datasource.initial-size=10## Mybatis 配置 #由于使用的是jpa所以这里就注释掉mybatis#mybatis.typeAliasesPackage=org.spring.springboot.domain#mybatis.mapperLocations=classpath:mapper/*.xml########################################################### Java Persistence Api 自动进行建表######################################################### Specify the DBMSspring.jpa.database = MYSQL# Show or not log for each sqlqueryspring.jpa.show-sql = true# hibernate ddl auto (create,create-drop, update)spring.jpa.hibernate.ddl-auto = update# Naming strategyspring.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@SpringBootApplicationpublic 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 刷新看到数据已经插入进来了
0 0
- SpringBoot学习:整合Redis
- Springboot整合Redis
- springboot整合redis缓存
- springboot整合redis
- Springboot+redis 整合
- SpringBoot整合Redis
- Springboot整合Redis
- springBoot整合redis
- Springboot整合Redis缓存
- springboot整合redis集群
- springboot整合redis
- springBoot整合redis报错报错
- SpringBoot 整合redis
- 7.springboot整合redis
- springboot整合redis
- springboot整合redis
- Redis---------springboot整合Redis实例
- SpringBoot(三)整合Redis
- Android热修复框架Tinker的集成和遇到的问题
- MTK6573智能机平台系统文件夹文件详解4
- GetModuleFileName
- (二)碎碎念接口优化--fastjson版本兼容问题
- 判断一个数是否为 2 的 某 次方
- Springboot整合Redis
- HTTPS原理
- AS子句作为别名
- MySQL
- Java 二维码生成基础方法和代码
- Matrix Multiplication(zoj 2316)
- 无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用
- N皇后问题
- 51nod:1081 子段求和(树状数组)