shop-parent [pom] (商品父模块)
1 创建maven项目
2 配置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.yy.shop</groupId><artifactId>shop-parent</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>eureka-server</module><module>config-server</module><module>zuul-server</module></modules><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement><dependencies><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.8.5</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.4.RELEASE</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Greenwich.SR1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement></project>
3 删除src文件夹
eureka-server [jar] (注册中心)
1 创建maven模块
2 配置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.yy.shop</groupId><artifactId>shop-parent</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>eureka-server</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies></project>
3 创建启动类
创建com.yy.shop文件夹
在该文件夹下创建EurekaServerApp启动类
@SpringBootApplication
@EnableEurekaClient
public class EurekaServerApp {public static void main(String[] args) {SpringApplication.run(EurekaServerApp.class, args);}}
在resources文件夹下创建application.yml文件
server:port: 8761 #设置Eureka服务器的端口号为8761
eureka:instance:hostname: localhost #指定Eureka实例的主机名为localhostclient:fetch-registry: false #禁用Eureka客户端从服务注册表中获取信息register-with-eureka: false # 禁止Eureka客户端向Eureka服务器注册自身service-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka # 设置Eureka客户端连接到Eureka服务器的默认URL
启动启动类进行测试
启动成功
config-server [jar] (配置中心)
1 创建maven模块
2 配置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.yy.shop</groupId><artifactId>shop-parent</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>config-server</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency></dependencies>
</project>
3 创建启动类
创建com.yy.shop文件夹
在该文件夹下创建ConfigServerApp启动类
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer //启用配置中心
public class ConfigServerApp {public static void main(String[] args) {SpringApplication.run(ConfigServerApp.class, args);}
}
在resources文件夹下创建application.yml文件
server:port: 9100
spring:application:name: config-servercloud:config:server:git:uri: https://gitee.com/shanyue_jianguang/microservices.git #此处放置自己的gitee仓库username: 2755469212@qq.compassword: xxxxxxx
eureka:client:service-url:defaultZone: http://localhost:8761/eureka
在自己的gitee仓库下提交zuul-server.yml
编辑zuul-server.yml
server:port: 9000ribbon:ReadTimeout: 6000000ConnectTimeout: 6000000hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 6000000
zuul:host:connect-timeout-millis: 600000socket-timeout-millis: 6000000force-original-query-string-encoding: trueignored-patterns: /*-server/**routes:member-server-route:path: /member/**service-id: member-serversensitive-headers:custom-sensitive-headers: trueribbon:MaxAutoRetriesNextServer: 0OkToRetryOnAllOperations: falseConnectTimeout: 10000ReadTimeout: 10000good-server-route:path: /good/**service-id: good-serversensitive-headers:custom-sensitive-headers: trueribbon:MaxAutoRetriesNextServer: 0OkToRetryOnAllOperations: falseConnectTimeout: 10000ReadTimeout: 10000seckill-server-route:path: /seckill/**service-id: seckill-serversensitive-headers:custom-sensitive-headers: trueribbon:MaxAutoRetriesNextServer: 0OkToRetryOnAllOperations: falseConnectTimeout: 10000ReadTimeout: 10000
zuul-server [jar] (网关)
1 创建maven模块
2 配置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.yy.shop</groupId><artifactId>shop-parent</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>zuul-server</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-zuul</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-client</artifactId></dependency></dependencies>
</project>
3 创建启动类
创建com.yy.shop文件夹
在该文件夹下创建ZuulServerApp启动类
@SpringBootApplication
@EnableZuulProxy //启用网关
public class ZuulServerApp {public static void main(String[] args) {SpringApplication.run(ZuulServerApp.class,args);}
}
在resources文件夹下创建bootstrap.yml文件
eureka:client:service-url:defaultZone: http://localhost:8761/eureka #配置 Eureka 注册中心
spring:application:name: zuul-server #设置应用程序的名称为 "zuul-server"。这个名称将在 Eureka 中注册和发现服务时使用cloud:config:name: zuul-server #指定要获取的配置文件的名称label: master #确定要获取的配置文件的版本,当前文件在master分支discovery:service-id: config-server #指定配置服务的服务IDenabled: true #启用配置中心的服务发现功能
4 测试网关
打开eureka-server启动类
打开config-server启动类
打开zuul-server启动类
网关成功通过config来访问到gitee中zuul-server.yml的9000端口
若显示8080,则说明没有访问到
shop-common [jar]
1 创建maven模块
2 配置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.yy.shop</groupId><artifactId>shop-parent</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>shop-common</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties>
</project>
shop-provider-api [pom]
1 创建maven模块
2 配置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.yy.shop</groupId><artifactId>shop-parent</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>shop-provider-api</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>com.yy.shop</groupId><artifactId>shop-common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies>
</project>
3 删除src文件夹
member-api [jar]
1 创建maven模块
2 配置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.yy.shop</groupId><artifactId>shop-provider-api</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>member-api</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties></project>
good-api [jar]
1 创建maven模块
2 配置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.yy.shop</groupId><artifactId>shop-provider-api</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>good-api</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties></project>
seckill-api [jar]
1 创建maven模块
2 配置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.yy.shop</groupId><artifactId>shop-provider-api</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>seckill-api</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties></project>
shop-provider [pom]
1 创建maven模块
2 配置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.yy.shop</groupId><artifactId>shop-parent</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>shop-provider</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>com.yy.shop</groupId><artifactId>member-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency></dependencies>
</project>
3 删除src文件夹
member-server [jar]
1 创建maven模块
2 配置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.yy.shop</groupId><artifactId>shop-provider</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>member-server</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!--后续自定义完 redis 启动器后解除注释<dependency><groupId>com.liushao.shop</groupId><artifactId>myreids-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency>--><dependency><groupId>com.yy.shop</groupId><artifactId>member-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.33</version></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.2.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId><version>2.0.1.RELEASE</version></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.10</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.8</version></dependency><!--swagger依赖--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency></dependencies>
</project>
good-server [jar]
1 创建maven模块
2 配置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.yy.shop</groupId><artifactId>shop-provider</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>good-server</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>com.yy.shop</groupId><artifactId>good-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>com.yy.shop</groupId><artifactId>myredis-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.2.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.9</version></dependency><!--swagger依赖--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency></dependencies></project>
3 创建bootstrap.yml
eureka:client:service-url:defaultZone: http://localhost:8761/eureka #配置 Eureka 注册中心
spring:application:name: good-server #设置应用程序的名称为 "good-server"。这个名称将在 Eureka 中注册和发现服务时使用cloud:config:name: good-server#指定要获取的配置文件的名称label: master #确定要获取的配置文件的版本,当前文件在master分支discovery:service-id: config-server #指定配置服务的服务IDenabled: true #启用配置中心的服务发现功能
seckill-server [jar]
1 创建maven模块
2 配置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.yy.shop</groupId><artifactId>shop-provider</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>seckill-server</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>com.yy.shop</groupId><artifactId>seckill-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>com.yy.shop</groupId><artifactId>good-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>com.yy.shop</groupId><artifactId>member-api</artifactId><version>1.0-SNAPSHOT</version></dependency><!--<dependency><groupId>com.yy.shop</groupId><artifactId>myreids-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version></dependency>--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.2.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency><dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.0.9</version></dependency><!--swagger依赖--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency></dependencies>
</project>
3 创建bootstrap.yml
eureka:client:service-url:defaultZone: http://localhost:8761/eureka #配置 Eureka 注册中心
spring:application:name: seckill-server #设置应用程序的名称为 "good-server"。这个名称将在 Eureka 中注册和发现服务时使用cloud:config:name: seckill-server#指定要获取的配置文件的名称label: master #确定要获取的配置文件的版本,当前文件在master分支discovery:service-id: config-server #指定配置服务的服务IDenabled: true #启用配置中心的服务发现功能
frontend-server [jar]
1 创建maven模块
2 添加前端页面
mybatis-spring-boot-starter [jar] (自定义的redis启动器)
1 创建maven模块
创建包
2 配置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.yy.shop</groupId><artifactId>shop-parent</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>myredis-spring-boot-starter</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
</project>
3 创建 KeyPrefix接口 (操作redis到的接口【前缀和过期时间】)
public interface KeyPrefix {/*** 前缀 xxxxxxx:用户信息过期时间* @return*/public String getPrefix();/*** 有效期时间* @return*/public int getExpireSeconds();
}
4 创建 MyRedisAutoConfiguration(自动装配类)
@Configuration
@EnableConfigurationProperties({RedisProperties.class})
@ConditionalOnProperty(prefix = "redis", value = "host")
public class MyRedisAutoConfiguration {@Beanpublic MyRedisTemplate myRedisTemplate(){return new MyRedisTemplate();}@Beanpublic JedisPool jedisPool(RedisProperties redisProperties){JedisPoolConfig config = new JedisPoolConfig();//设置参数//config.setMinIdle(null);return new JedisPool(config, redisProperties.getHost(), redisProperties.getPort(), redisProperties.getTimeout(), redisProperties.getPassword());}
}
5 创建 MyRedisTemplate(redis操作类)
public class MyRedisTemplate {//使用redis连接池private static JedisPool jedisPool;static {JedisPoolConfig config = new JedisPoolConfig();//设置信息config.setMaxIdle(500);config.setMaxTotal(500);config.setMinIdle(100);//指定本地jedisPool = new JedisPool(config, "127.0.0.1", 6379, 10000000);}/*** 将数据以 JSON 格式序列化后存储到 Redis 中* @param keyPrefix* @param key* @param data* @param <T>*/public <T> void set(KeyPrefix keyPrefix, String key, T data){//set key valueJedis jedis = null;try {//获取redisjedis = jedisPool.getResource();//给key加上前缀String realkey = keyPrefix.getPrefix() + key;if (keyPrefix.getExpireSeconds() > 0 ){//没有过期,设置失效时间jedis.setex(realkey, keyPrefix.getExpireSeconds(), JSON.toJSONString(data));}else {jedis.set(realkey, JSON.toJSONString(data));}}catch (Exception e){e.printStackTrace();}finally {jedis.close();}}/*** 根据键名和前缀拼接出真正的键,然后从 Redis 中获取对应的值* @param keyPrefix* @param key* @param clazz* @return* @param <T>*/public <T> T get(KeyPrefix keyPrefix,String key,Class<T> clazz){try(Jedis jedis = jedisPool.getResource()){//获取一下 keyString realKey = keyPrefix.getPrefix()+key;String s = jedis.get(realKey);if(StringUtils.isEmpty(s)){return null;}return JSON.parseObject(s,clazz);}catch (Exception ex){ex.printStackTrace();throw new RuntimeException(ex.getMessage());}}/*** 元素是否存在* @param keyPrefix* @param key* @return*/public boolean exists(KeyPrefix keyPrefix,String key){try(Jedis jedis = jedisPool.getResource()){//获取一下 keyString realKey = keyPrefix.getPrefix()+key;return jedis.exists(realKey);}catch (Exception ex){ex.printStackTrace();throw new RuntimeException(ex.getMessage());}}/*** 将数据以 JSON 格式序列化后存储到 Redis 哈希数据类型中* @param keyPrefix* @param key* @param field* @param data* @param <T>*/public <T> void hset(KeyPrefix keyPrefix,String key,String field,T data){try(Jedis jedis = jedisPool.getResource()){//获取一下 keyString realKey = keyPrefix.getPrefix()+key;jedis.hset(realKey,field,JSON.toJSONString(data));}catch (Exception ex){ex.printStackTrace();throw new RuntimeException(ex.getMessage());}}/*** 根据键名、字段名和前缀拼接出真正的键,然后从 Redis 哈希数据类型中获取对应的值* @param keyPrefix* @param key* @param field* @param clazz* @return* @param <T>*/public <T> T hget(KeyPrefix keyPrefix,String key,String field,Class<T> clazz){try(Jedis jedis = jedisPool.getResource()){//获取一下 keyString realKey = keyPrefix.getPrefix()+key;String obj = jedis.hget(realKey, field);if(obj==null){return null;}return JSON.parseObject(obj,clazz);}catch (Exception ex){ex.printStackTrace();throw new RuntimeException(ex.getMessage());}}/*** 遍历哈希数据类型中的所有字段和值,并将它们反序列化为指定的数据类型后存储到一个 Map 对象中返回* @param keyPrefix* @param key* @param clazz* @return* @param <T>*/public <T> Map<String,T> hgetAll(KeyPrefix keyPrefix, String key, Class<T> clazz){try(Jedis jedis = jedisPool.getResource()){//获取一下 keyString realKey = keyPrefix.getPrefix()+key;Map<String, String> map = jedis.hgetAll(realKey);if(map==null){return null;}Map<String,T> resultMap = new HashMap<>();for(Map.Entry<String,String> entry:map.entrySet()){resultMap.put(entry.getKey(),JSON.parseObject(entry.getValue(),clazz));}return resultMap;}catch (Exception ex){ex.printStackTrace();throw new RuntimeException(ex.getMessage());}}/*** 根据键名和前缀拼接出真正的键,对该键对应的值进行增加操作* @param keyPrefix* @param key* @return*/public Long incr(KeyPrefix keyPrefix,String key){try(Jedis jedis = jedisPool.getResource()){//获取一下 keyString realKey = keyPrefix.getPrefix()+key;return jedis.incr(realKey);}catch (Exception ex){ex.printStackTrace();throw new RuntimeException(ex.getMessage());}}/*** 根据键名和前缀拼接出真正的键,对该键对应的值进行减少操作* @param keyPrefix* @param key* @return*/public Long decr(KeyPrefix keyPrefix,String key){try(Jedis jedis = jedisPool.getResource()){//获取一下 keyString realKey = keyPrefix.getPrefix()+key;return jedis.decr(realKey);}catch (Exception ex){ex.printStackTrace();throw new RuntimeException(ex.getMessage());}}/*** 根据键名和前缀拼接出真正的键,设置该键的过期时间为指定的秒数* @param keyPrefix* @param key* @param expireSeconds* @return*/public Long expire(KeyPrefix keyPrefix,String key,int expireSeconds) {try (Jedis jedis = jedisPool.getResource()) {//获取一下 keyString realKey = keyPrefix.getPrefix() + key;return jedis.expire(realKey, expireSeconds);} catch (Exception ex) {ex.printStackTrace();throw new RuntimeException(ex.getMessage());}}
}
6 创建 RedisProperties(redis的链接参数类)
@Setter
@Getter
@ConfigurationProperties(prefix = "redis")
public class RedisProperties {//主机private String host;//端口private int port;private int timeout;private String password;
}
7 创建 META-INF文件下的spring.factories文件(定义自动装配类以及实现类)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.yy.shop.MyRedisAutoConfiguration