第五章:简单springcloud微服务项目,redis简单集成
本章注意讲解spring cloud微服务集成redis,本章节依赖前面章节。讲解开始前的准备如下: 一、先新建一个公共的Maven Project项目,项目名称为module-common,参考第一章节步骤创建。 二、右键module-common,在该项目下创建一个Maven Module项目,项目名称为module-common-api。项目结构图如下:1)module-comm
本章注意讲解spring cloud微服务集成redis,本章节依赖前面章节。
讲解开始前的准备如下:
一、先新建一个公共的Maven Project项目,项目名称为module-common,参考第一章节步骤创建。
二、右键module-common,在该项目下创建一个Maven Module项目,项目名称为module-common-api。
项目结构图如下:
1)module-common项目下pom.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maven.xm</groupId>
<artifactId>module-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring.cloud.dependencies.version>Camden.SR7</spring.cloud.dependencies.version>
<io.springfox>2.7.0</io.springfox>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<modules>
<module>module-common-api</module>
</modules>
</project>
2)module-common-api项目下pom.xml文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.maven.xm</groupId>
<artifactId>module-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>module-common-api</artifactId>
<packaging>jar</packaging>
<dependencies>
<!-- redis集成jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</project>
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<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException{
RedisTemplate<Object, Object> template = new RedisTemplate<Object,Object>();
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<String, String> ops = template.opsForValue();
ops.set(key,value);
}
public String getValue(String key){
ValueOperations<String, String> ops = template.opsForValue();
return ops.get(key);
}
}
5)调整module-oauth-resources下pom.xml文件,新增以下内容:
<dependency>
<groupId>com.maven.xm</groupId>
<artifactId>module-common-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
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压缩包
码云账户: oyj812438109@163.com 密码: oyj812438109@163.com
请勿恶意操作,谢谢!
本文说明:该文章属于原创,如需转载,请标明文章转载来源
更多推荐
所有评论(0)