SpringBoot整合Redis開發RESTful API介面

lvxfcjf發表於2021-09-09

開發環境

  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

配置檔案

在裡的application.properties檔案中配置資訊

# 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

有兩種連線方式:一個是,另一個是。它倆間的區別:使用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格式
    圖片描述
  • GET請求圖片描述
  • PUT請求:設定ContentType=application/json,修改RequestBody中的內容,格式為Json格式
    圖片描述圖片描述
  • DELETE請求圖片描述圖片描述

程式碼地址

  

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/3486/viewspace-2815380/,如需轉載,請註明出處,否則將追究法律責任。

相關文章