SpringBoot集成redis案例分享

一、前言

 SpringBoot已经实现无配置化编程,有经验的开发人员,应该配置过Spring集成redis的过程,需要在Spring的配置文件,配置Bean的注入,为了解决Spring的臃肿的配置文件,基于“约定大于配置”的想法,在META-INF目录中会有spring.factories文件,在这个文件中约定了redis的自动装配的入口。

SpringBoot集成redis案例分享_第1张图片

 

二、环境准备:

     1、redis环境:

          利用docker  自动部署一个redis :

         具体的安装步骤,可以参考

     SpringBoot集成redis案例分享_第2张图片   

     设置缓存KEY:

     SpringBoot集成redis案例分享_第3张图片

 

    2、部署一套SpringBoot 环境:

       pom.xml:





  
    org.springframework.boot
    spring-boot-starter-parent
    2.2.6.RELEASE
     
  

  4.0.0
  org.example
  UserService
  1.0-SNAPSHOT

  UserService
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11
      test
    
    
      org.example
      order-api
      1.0-SNAPSHOT
    
    
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.springframework.boot
      spring-boot-starter-data-redis
    
  

  
    
      
        
        
          maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-jar-plugin
          3.0.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
        
        
          maven-site-plugin
          3.7.1
        
        
          maven-project-info-reports-plugin
          3.0.0
        
      
    
  

 设置redis的服务器IP:

  SpringBoot集成redis案例分享_第4张图片

jason.host=localhost
jason.port=8089

spring.application.name==user-service
server.port=8080

spring.redis.host=192.168.0.105

 缓存查看:

@RestController
public class TestController {
      @JasonReference
      private IOrderService  orderService;

      @Autowired
      private RedisTemplate redisTemplate;

      @GetMapping("/test")
      public String test()
      {
            System.out.println("读取缓存数据"+redisTemplate.opsForValue().get("rpc"));
            return orderService.queryOrderList();
      }
}

 SpringBoot集成redis案例分享_第5张图片

 

你可能感兴趣的:(SpringBoot)