開發(fā)環(huán)境
??SpringBoot版本:2.0.5.RELEASE嘹裂、jdk版本:1.8+呻引、Maven版本:3.5.4
項目框架
├── pom.xml
├── springbootreidsrestfulapi.iml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── jenkin
│ │ │ └── springboot
│ │ │ ├── App.java
│ │ │ ├── controller
│ │ │ │ └── UserController.java
│ │ │ ├── pojo
│ │ │ │ ├── RedisConfigBean.java
│ │ │ │ └── User.java
│ │ │ ├── redis
│ │ │ │ └── RedisConfig.java
│ │ │ └── service
│ │ │ ├── UserService.java
│ │ │ └── impl
│ │ │ └── UserServiceImpl.java
│ │ └── resources
│ │ └── application.properties
配置文件
在SpringBoot里的application.properties文件中配置Redis信息
# REDIS (RedisProperties)
# Database index used by the connection factory.
spring.redis.database=0
# Redis server host.
spring.redis.host=localhost
# Login password of the redis server.
spring.redis.password=
# Redis server port.
spring.redis.port=6379
# Maximum number of connections that can be allocated by the pool at a given time. Use a negative value for no limit.
spring.redis.lettuce.pool.max-active=8
# Maximum number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.
spring.redis.lettuce.pool.max-idle=8
# Maximum amount of time a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.
spring.redis.lettuce.pool.max-wait=-1ms
# Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.
spring.redis.lettuce.pool.min-idle=0
# Shutdown timeout.
spring.redis.lettuce.shutdown-timeout=100ms
Redis有兩種連接方式:一個是Jedis冰更,另一個是Lettuce扶叉。它倆間的區(qū)別:使用Jedis湿酸,如果多線程使用同一個連接伍派,線程時不安全的,需要使用連接池屈张,為每一個Jedis實例分配一個連接擒权;而使用Lettuce袱巨,當多線程連接同一個連接實例時,是線程安全的碳抄。
SpringBoot2.0以后愉老,默認的連接方式是Lettuce,我這里使用的也是Lettuce剖效,首先添加Redis包:
<!--redis包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
如果使用的是Jedis連接嫉入,需要修改一下配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
建立連接
/**
* 使用Lettuce連接Redis
* @return
*/
@Bean
public LettuceConnectionFactory connectionFactory() {
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
lettuceConnectionFactory.setDatabase(redisConfigBean.getDatabase());
lettuceConnectionFactory.setHostName(redisConfigBean.getHost());
lettuceConnectionFactory.setPassword(redisConfigBean.getPassword());
lettuceConnectionFactory.setPort(redisConfigBean.getPort());
return lettuceConnectionFactory;
}
效果演示
-
POST請求:設置ContentType=application/json,添加RequestBody中的內容璧尸,格式為Json格式
POST請求 -
GET請求GET請求
-
PUT請求:設置ContentType=application/json咒林,修改RequestBody中的內容,格式為Json格式
PUT請求GET請求 -
DELETE請求DELETE請求GET請求
代碼地址
??https://github.com/JenkinWang/springboot-reids-restful-api