京东秒杀之项目搭建

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/204374.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Pytorch Lightning 完全攻略

Pytorch-Lightning这个库我“发现”过两次。第一次发现时&#xff0c;感觉它很重很难学&#xff0c;而且似乎自己也用不上。但是后面随着做的项目开始出现了一些稍微高阶的要求&#xff0c;我发现我总是不断地在相似工程代码上花费大量时间&#xff0c;Debug也是这些代码花的时…

【栈和队列(1)(逆波兰表达式)】

文章目录 前言什么是栈(Stack)栈方法栈的模拟实现链表也可以实现栈逆波兰表达式逆波兰表达式在栈中怎么使用 前言 什么是栈(Stack) 栈&#xff1a;一种特殊的线性表&#xff0c;其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶&#xff0…

C++不同平台下的RTTI实现

给定一个含有虚函数的对象的地址&#xff0c;找到对应的类名&#xff0c;不同平台下方法也不同&#xff0c;这是由于RTTI实现并没有统一的标准。 Linux&#xff1a; #include <iostream> #include <typeinfo>class Person { public:virtual void func(){std::cout…

人机交互2——任务型多轮对话的控制和生成

1.自然语言理解模块 2.对话管理模块 3.自然语言生成模块

【FGPA】Verilog:JK 触发器 | D 触发器 | T 触发器 | D 触发器的实现

0x00 JK 触发器 JK 触发器是 RS 触发器和 T 触发器的组合&#xff0c;有两个输入端 J 和 K&#xff0c;如果两个输入端都等于 1&#xff0c;则将当前值反转。 行为表 状态图 Timing Diagram Circuit JK 触发器的设计目的是防止 RS 触发器在输入 S 和 R 均等于 …

JAVA文件IO, File类, 字符流,字节流

文章目录 文件IO1. File2. IO流2.1 字符流2.1.1 Reader2.1.2 Writer 2.2 字节流2.2.1 InputStream2.2.2 FileInputStream2.2.3 利用Scanner进行字符读取2.2.4 OutputStream 文件IO I: Input, 从硬盘往内存读数据 O: Output, 从内存往硬盘输出数据 1. File Java 中通过 java…

java:jpa、Hibernate、Spring Data JPA、ORM以及和mybatis的区别

文章目录 Java连接数据库几种方式JPAHibernate和Spring Data JPAORM框架jpa和mybatis区别Spring Boot JPA使用例子1、创建库和表2、添加依赖3、配置数据源和Hibernate属性4、配置实体类5、创建一个继承JpaRepository的接口&#xff1a;6、创建一个控制器&#xff08;Controller…

SpringCloud原理-OpenFeign篇(四、请求原理)

文章目录 前言正文一、书接上回&#xff0c;从代理对象入手二、ReflectiveFeign.FeignInvocationHandler#invoke()三、SynchronousMethodHandler#invoke(...) 的实现原理3.1 invoke(...)源码3.2 executeAndDecode(...) 执行请求并解码 四、如何更换client 的实现 附录附1&#…

mac电脑下载Netflix Mac(奈飞客户端)安装教程

Netflix Mac&#xff0c;奈飞官方客户端&#xff0c;带给您无限的电影和剧集体验&#xff01;与朋友分享最新热门剧集、电影&#xff0c;与家人一起享受高品质的流媒体内容。 通过Netflix Mac&#xff0c;您可以轻松地搜索、浏览和观看各种类型的影片&#xff0c;包括剧情片、…

Java设计模式系列:单例设计模式

Java设计模式系列&#xff1a;单例设计模式 介绍 所谓类的单例设计模式&#xff0c;就是采取一定的方法保证在整个的软件系统中&#xff0c;对某个类只能存在一个对象实例&#xff0c;并且该类只提供一个取得其对象实例的方法&#xff08;静态方法&#xff09; 比如 Hiberna…

【TC3xx芯片】TC3xx芯片的Clock System功能详解

目录 前言 正文 1.时钟源 1.1 有源晶振和无源晶振 1.1.1 无源晶振 1.1.2 有源晶振 1.1.3 有源晶振和无源晶振的区别 1.1 振荡器电路&#xff08;OSC&#xff09; 1.1.1外部输入时钟模式 1.1.2 外部晶体 / 陶瓷谐振器模式 1.1.3 OSC控制寄存器 1.1.4 配置OSC 1.1.5…

Android Frameworks 开发总结之七

1.修改android 系统/system/下面文件时权限不够问题 下面提到的方式目前在Bobcat的userdebug image上测试可行,还没有在user上测试过. 修改前: leif@leif:~$ adb root restarting adbd as root leif@leif:~$ adb disable-verity verity is already disabled using overlayf…

Unity 关于SpriteRenderer 和正交相机缩放

float oldWidth 750f;float oldHeight 1334f;float newWidth Screen.width;float newHeight Screen.height;float oldAspect oldWidth / oldHeight;float newAspect newWidth / newHeight;//水平方向缩放float horizontalCompressionRatio newAspect / oldAspect;//垂直…

笔记十七、认识React的路由插件react-router-dom和基本使用

react-router 分类 web使用 react-router-dom native使用 react-router-native anywhere&#xff08;使用麻烦&#xff09; react-router 安装 yarn add react-router-dom main.jsx import React from "react"; import ReactDOM from "react-dom/client"…

基于可微分渲染器的相机位置优化【PyTorch3D】

在这个教程中&#xff0c;我们将使用可微渲染学习给定参考图像的相机的 [x, y, z] 位置。 我们将首先使用相机的起始位置初始化渲染器。 然后&#xff0c;我们将使用它来生成图像&#xff0c;使用参考图像计算损失&#xff0c;最后通过整个管道进行反向传播以更新相机的位置。…

C#,《小白学程序》第五课:队列(Queue)其一,排队的技术与算法

日常生活中常见的排队&#xff0c;软件怎么体现呢&#xff1f; 排队的基本原则是&#xff1a;先到先得&#xff0c;先到先吃&#xff0c;先进先出 1 文本格式 /// <summary> /// 《小白学程序》第五课&#xff1a;队列&#xff08;Queue&#xff09; /// 日常生活中常见…

申银万国期货通过ZStack Cube信创超融合一体机打造金融信创平台

信创是数字中国建设的重要组成部分&#xff0c;也是数字经济发展的关键推动力量。作为云基础软件企业&#xff0c;云轴科技ZStack产品矩阵全面覆盖数据中心云基础设施&#xff0c;ZStack信创云首批通过可信云《一云多芯IaaS平台能力要求》先进级&#xff0c;是其中唯一兼容四种…

Git使用基础总结(从小白到新手版)

(꒪ꇴ꒪ )&#xff0c;Hello我是祐言QAQ我的博客主页&#xff1a;C/C语言&#xff0c;数据结构&#xff0c;Linux基础&#xff0c;ARM开发板&#xff0c;网络编程等领域UP&#x1f30d;快上&#x1f698;&#xff0c;一起学习&#xff0c;让我们成为一个强大的攻城狮&#xff0…

C语言 移位操作符

<< 左移操作符>> 右移操作符 注&#xff1a;移位操作符的操作数只能是整数。 移位操作符移动的是二进制位。 整数的二进制表示有3种&#xff1a; 原码反码补码 正的整数的原码、反码、补码相同。 负的整数的原码、反码、补码是要计算的。 由负整数原码计算出反…

新王加冕,GPT-4V 屠榜视觉问答

当前&#xff0c;多模态大型模型&#xff08;Multi-modal Large Language Model, MLLM&#xff09;在视觉问答&#xff08;VQA&#xff09;领域展现了卓越的能力。然而&#xff0c;真正的挑战在于知识密集型 VQA 任务&#xff0c;这要求不仅要识别视觉元素&#xff0c;还需要结…