第五章:简单springcloud微服务项目,redis简单集成

本章注意讲解spring cloud微服务集成redis,本章节依赖前面章节。

讲解开始前的准备如下:

    一、先新建一个公共的Maven Project项目,项目名称为module-common,参考第一章节步骤创建。

    二、右键module-common,在该项目下创建一个Maven Module项目,项目名称为module-common-api。

项目结构图如下:

第五章:简单springcloud微服务项目,redis简单集成_第1张图片

1)module-common项目下pom.xml配置:



  4.0.0
  
  com.maven.xm
  module-common
  0.0.1-SNAPSHOT
  pom
  
  
      org.springframework.boot
      spring-boot-starter-parent
      1.5.2.RELEASE
  
  
  
      UTF-8
      UTF-8
      1.8
      Camden.SR7
      2.7.0
  
  
  
       
           
               org.springframework.cloud
               spring-cloud-dependencies
               ${spring.cloud.dependencies.version}
               pom
               import
           
           
       
   
   
   
	    
	        
	            org.apache.maven.plugins
	            maven-surefire-plugin
	            
	            	${java.version}
                  	${java.version}
                  	UTF-8
                  	true
	            
	        
	    
	
    
    
	    
	      	central
	      	Central Repository
	      	https://repo.maven.apache.org/maven2
	      	default
	      	
	        	false
	      	
	    
	 
	 
    
    	module-common-api
    

2)module-common-api项目下pom.xml文件配置:



  4.0.0
  
  
    com.maven.xm
    module-common
    0.0.1-SNAPSHOT
  
  module-common-api
  jar
  
  	
  		
        
           org.springframework.boot
           spring-boot-starter-data-redis
        
        
	
  

3)module-common-api项目下RedisConfiguration.java文件内容:

注意:该文件为redis操作的主要工具类

package com.maven.xm.redis;

import java.net.UnknownHostException;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * redis 工具类
 * @author ouyangjun
 * 
 */
@Configuration
public class RedisConfiguration {
	
	@Bean
	@ConditionalOnMissingBean(name="redisTemplate")
	public RedisTemplate redisTemplate(
			RedisConnectionFactory redisConnectionFactory) 
					throws UnknownHostException{
		RedisTemplate template = new RedisTemplate();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}
	
	@Bean
	@ConditionalOnMissingBean(StringRedisTemplate.class)
	public StringRedisTemplate stringRedisTemplate(
			RedisConnectionFactory redisConnectionFactory) 
					throws UnknownHostException{
		StringRedisTemplate template = new StringRedisTemplate();
		template.setConnectionFactory(redisConnectionFactory);
		return template;
	}
	
}

4)module-common-api项目下RedisStringUtils.java文件内容:

注意:该文件为redis字符操作工具类,其它工具类就不一一展示了,有需要请下载源码观看

package com.maven.xm.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

/**
 * redis string操作工具类
 * @author ouyangjun
 *
 */
@Component
public class RedisStringUtils {
	
    @Autowired
    private StringRedisTemplate template;
 
    public void setKey(String key,String value){
        ValueOperations ops = template.opsForValue();
        ops.set(key,value);
    }
 
    public String getValue(String key){
        ValueOperations ops = template.opsForValue();
        return ops.get(key);
    }
    
}

5)调整module-oauth-resources下pom.xml文件,新增以下内容:


    com.maven.xm
    module-common-api
    0.0.1-SNAPSHOT

6)调整module-oauth-resources下applicaion.properties文件,新增以下内容:

#redis
spring.redis.database = 0
spring.redis.host = 127.0.0.1
spring.redis.port = 6379
spring.redis.password = 
spring.redis.encode=UTF-8
spring.redis.timeout = 6000
spring.redis.pool.max-active = 100
spring.redis.pool.max-wait = -1
spring.redis.pool.max-idle = 10
spring.redis.pool.min-idle = 5

7)调整module-oauth-resources下DemoController类,调整之后的代码如下:

package com.maven.xm.oauth.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.maven.xm.redis.RedisStringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "module-oauth模块,用户Demo Controller")
@RestController
@RequestMapping(value = "/demo")
public class DemoController {
	
	@Autowired
	private RedisStringUtils redisString;
	
	@ApiOperation(value="module模块,Test方法", notes="Test")
	@RequestMapping(value = "/hello", method = RequestMethod.GET)
	public String hello(){
		// redis操作
		redisString.setKey("hello", "Hello Redis!");
		
		return "Hello Demo ouyangjun!";
	}
	
}

8) 依次启动EurekaServerApplication、WebApplication、OAuthApplication模块

       注意:启动前,一定要先启动redis-server.exe,否则会访问报错。

      在浏览器中输入:http://localhost:8762/xm/demo/ribbon/hello

                                   或

                                   http://localhost:8762/xm/demo/fegin/hello

      展示成功界面之后,想判断redis是否已经存在当前的key了,

      请参考redis简单命令操作,地址:https://blog.csdn.net/p812438109/article/details/80961169

本章功能已实现完,待续!


源码下载地址: https://gitee.com/ouyangjun_xm/springcloud/attach_files下chapter-five.rar压缩包

                      码云账户: [email protected]     密码: [email protected]

                      请勿恶意操作,谢谢!

本文说明:该文章属于原创,如需转载,请标明文章转载来源


你可能感兴趣的:(springcloud)