1. 程式人生 > >基於springboot2.x 的redis配置及使用

基於springboot2.x 的redis配置及使用

使用背景

  • 目前公司使用的都是springboot1.5.x + oauth2 + redis做許可權認證服務;鑑於學習oauth2的目的,搭建簡單的demo
  • demo工程使用springboot2.0.6 為基礎構建

問題

出現警告:無法連線到redis

2018-10-18 12:00:50.136  WARN 1996 --- [io-10110-exec-2] o.s.s.o.provider.endpoint.TokenEndpoint  : Handling error: RedisConnectionFailureException, Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 172.19.103.47:6379
2018-10-18 12:00:50.324  WARN 1996 --- [io-10110-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by handler execution: org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 172.19.103.47:6379

處理前pom檔案:

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.security.oauth</groupId>
			<artifactId>spring-security-oauth2</artifactId>
			<version>${oauth2.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>

再網上各種處理方式都試了一遍,修改redis初始化方式也沒解決問題,後面在一篇部落格上(https://blog.csdn.net/weixin_39723544/article/details/80743074?utm_source=blogxgwz2)找到解決方式,特此記錄備忘;

修改pom檔案配置。

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
			<!-- 1.5的版本預設採用的連線池技術是jedis  2.0以上版本預設連線池是lettuce, 在這裡採用jedis,所以需要排除lettuce的jar -->
			<exclusions>
				<exclusion>
					<groupId>redis.clients</groupId>
					<artifactId>jedis</artifactId>
				</exclusion>
				<exclusion>
					<groupId>io.lettuce</groupId>
					<artifactId>lettuce-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
<!--spring2.0整合redis所需common-pool2-->
        <!-- 必須加上,jedis依賴此  -->
        <!-- spring boot 2.0 的操作手冊有標註 大家可以去看看 地址是:https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.5.0</version>
        </dependency>

        <!-- 將作為Redis物件序列化器 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

application.yml配置:

#boot2.x配置
spring:
  redis:
    database: 0 # Database index used by the connection factory.
    timeout: 0 # Connection timeout in milliseconds.
    jedis:
      pool:
        max-active: 8 # 
        max-idle: 8 # 
        max-wait: -1 # 
        min-idle: 1 # 

# boot1.x配置
spring:
  datasource:
  redis:
    database: 0 # Database index used by the connection factory.
    timeout: 0 # Connection timeout in milliseconds.
    pool:
      max-active: 8 #
      max-idle: 8 # 
      max-wait: -1 # 
      min-idle: 1 #