一、start组件的创建
1.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.tiger.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<name>redisson-spring-boot-starter</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.1.RELEASE</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.13.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>redisson-spring-boot-starter</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.创建 配置类 RedissonProperties.java
package com.tiger.redisson;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "tiger.redisson")
public class RedissonProperties {
private String host = "localhost";
private int port = 6379;
private String password = "123456";
private int timeout; //超时时间
private boolean ssl;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public boolean isSsl() {
return ssl;
}
public void setSsl(boolean ssl) {
this.ssl = ssl;
}
}
RedissonAutoConfiguration.java
package com.tiger.redisson;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Redisson 自动装配配置类
*
* @author tiger
*/
@ConditionalOnClass(Redisson.class) //条件装配
@EnableConfigurationProperties(RedissonProperties.class)
@Configuration
public class RedissonAutoConfiguration {
@Bean
RedissonClient redissonClient(RedissonProperties properties) {
Config config = new Config();
String prefix = "redis://";
if (properties.isSsl()) {
prefix = "rediss://";
}
config.useSingleServer().
setAddress(prefix + properties.getHost() + ":" + properties.getPort()).
setConnectTimeout(properties.getTimeout())
.setPassword(properties.getPassword());
return Redisson.create(config);
}
}
3.创建文件 META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tiger.redisson.RedissonAutoConfiguration
4.创建 META-INF/additional-spring-configuration-metadata.json
{
"properties": [
{
"name": "tiger.redisson.host",
"type": "java.lang.String",
"description": "redis的服务器地址",
"defaultValue": "localhost"
},
{
"name": "tiger.redisson.port",
"type": "java.lang.Integer",
"description": "redis服务器的端口",
"defaultValue": 6379
},
{
"name": "tiger.redisson.password",
"type": "java.lang.String",
"description": "redis服务密码",
"defaultValue": "123456"
}
]
}
5.打包
二、start组件的使用
1.创建demo工程
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.start.demo</groupId>
<artifactId>springboot-start-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-start-demo</name>
<description>Springboot Start Demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 引入自己创建的start组件-->
<dependency>
<groupId>com.tiger.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>springboot-start-demo</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.配置信息application.properties
# 配置redis IP和端口以及密码
tiger.redisson.host=127.0.0.1
tiger.redisson.port=6379
tiger.redisson.password=123456
3.测试类com.springboot.start.demo.controller.HelloController
package com.springboot.start.demo.controller;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
RedissonClient redissonClient;
@GetMapping("/test")
public String test(){
RBucket bucket=redissonClient.getBucket("redisson");
if(bucket.get()==null){
bucket.set("com.tiger.redisson");
}
return bucket.get().toString();
}
}
4.启动,测试 ...