mirror of
https://gitee.com/incloudcode/yexuejc-springboot.git
synced 2025-07-04 11:02:45 +08:00
2.0.0 基于springboot 1.x 2.x 分开维护
This commit is contained in:
parent
7ff9bb214c
commit
ff57ed324f
@ -1,5 +1,8 @@
|
||||
# yexuejc-springboot-base
|
||||
# yexuejc-springboot
|
||||
|
||||
### 说明
|
||||
>本分支基于 springboot 2.x 开发,版本号也将保持2.x
|
||||
>1.x分支继续基于 springboot 1.x开发
|
||||
|
||||
#### 项目介绍
|
||||
基于springboot maven 封装可继承基础工程
|
||||
@ -9,7 +12,7 @@ parent:版本封装<br/>
|
||||
base:功能封装
|
||||
|
||||
#### 最新版本
|
||||
>yexuejc.springboot.version=1.1.0 <br>
|
||||
>yexuejc.springboot.version=2.0.0 <br>
|
||||
>yexuejc.base.version=1.1.9
|
||||
|
||||
pom.xml
|
||||
|
13
UPDATE.md
13
UPDATE.md
@ -1,6 +1,19 @@
|
||||
yexuejc-springboot 更新内容
|
||||
-------------------
|
||||
|
||||
#### version :2.0.0
|
||||
**time:2018-9-26 16:55:00** <br/>
|
||||
**branch:** 2.x <br/>
|
||||
**关联工程:** <br/>
|
||||
```
|
||||
springboot-base:1.1.9
|
||||
spring-boot-starter-parent:2.0.5.RELEASE
|
||||
```
|
||||
**update:** <br/>
|
||||
1. 升级依赖
|
||||
2. 2.x分支将基于springboot 2.x 开发
|
||||
#
|
||||
|
||||
#### version :1.1.0
|
||||
**time:2018-9-23 12:49:36** <br/>
|
||||
**branch:** master <br/>
|
||||
|
37
pom.xml
37
pom.xml
@ -5,7 +5,7 @@
|
||||
|
||||
<groupId>com.yexuejc.springboot</groupId>
|
||||
<artifactId>yexuejc-springboot-parent</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>2.0.0</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
@ -15,7 +15,7 @@
|
||||
<!-- spring boot父级,包含了统一版本管理,统一插件管理等 -->
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.15.RELEASE</version>
|
||||
<version>2.0.5.RELEASE</version>
|
||||
<!-- <relativePath/> lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
@ -176,37 +176,6 @@
|
||||
<classifier>jar-with-dependencies</classifier>
|
||||
</dependency>
|
||||
|
||||
<!-- spring-boot-admin相关 -->
|
||||
<dependency>
|
||||
<groupId>de.codecentric</groupId>
|
||||
<artifactId>spring-boot-admin-starter-client</artifactId>
|
||||
<version>${spring-boot-admin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.codecentric</groupId>
|
||||
<artifactId>spring-boot-admin-server</artifactId>
|
||||
<version>${spring-boot-admin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.codecentric</groupId>
|
||||
<artifactId>spring-boot-admin-server-ui</artifactId>
|
||||
<version>${spring-boot-admin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.codecentric</groupId>
|
||||
<artifactId>spring-boot-admin-server-ui-hystrix</artifactId>
|
||||
<version>${spring-boot-admin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.codecentric</groupId>
|
||||
<artifactId>spring-boot-admin-server-ui-turbine</artifactId>
|
||||
<version>${spring-boot-admin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.codecentric</groupId>
|
||||
<artifactId>spring-boot-admin-server-ui-activiti</artifactId>
|
||||
<version>${spring-boot-admin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
@ -271,7 +240,9 @@
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<descriptors>
|
||||
<descriptor>${basedir}/assembly/assembly.xml</descriptor>
|
||||
</descriptors>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
|
@ -9,7 +9,7 @@
|
||||
<parent>
|
||||
<groupId>com.yexuejc.springboot</groupId>
|
||||
<artifactId>yexuejc-springboot-parent</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>2.0.0</version>
|
||||
<!-- 本地打包:使用相对关联路径 -->
|
||||
<!--<relativePath>../../yexuejc</relativePath>-->
|
||||
</parent>
|
||||
@ -70,8 +70,8 @@
|
||||
</dependency>
|
||||
<!-- 使用Redis -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-redis</artifactId>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -1,22 +1,19 @@
|
||||
package com.yexuejc.springboot.base.autoconfigure;
|
||||
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Cluster;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.data.redis.connection.RedisClusterConfiguration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.RedisNode;
|
||||
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
|
||||
import org.springframework.data.redis.connection.RedisPassword;
|
||||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
||||
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnection;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisOperations;
|
||||
@ -24,23 +21,23 @@ import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 多个database配置
|
||||
*
|
||||
* @author maxf
|
||||
* @PackageName com.yexuejc.springboot.base.autoconfigure
|
||||
* @Description
|
||||
* @date 2018/9/26 15:27
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({JedisConnection.class, RedisOperations.class, Jedis.class})
|
||||
@EnableConfigurationProperties(RedisProperties.class)
|
||||
@Order(1)
|
||||
public class MutiRedisAutoConfiguration {
|
||||
|
||||
public static final String BEAN_REDIS_FACTORY0 = "redisConnectionFactory";
|
||||
public static final String BEAN_REDIS_TEMPLATE0 = "redisTemplate";
|
||||
public static final String BEAN_REDIS_STRING_TEMPLATE0 = "stringRedisTemplate";
|
||||
@ -73,206 +70,93 @@ public class MutiRedisAutoConfiguration {
|
||||
public static final String BEAN_REDIS_TEMPLATE9 = "redis-template-9";
|
||||
public static final String BEAN_REDIS_STRING_TEMPLATE9 = "redis-string-template-9";
|
||||
|
||||
/**
|
||||
* Redis connection configuration.
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(GenericObjectPool.class)
|
||||
protected static class RedisConnectionConfiguration {
|
||||
|
||||
private final RedisProperties properties;
|
||||
|
||||
private final RedisSentinelConfiguration sentinelConfiguration;
|
||||
|
||||
private final RedisClusterConfiguration clusterConfiguration;
|
||||
|
||||
public RedisConnectionConfiguration(RedisProperties properties,
|
||||
ObjectProvider<RedisSentinelConfiguration> sentinelConfiguration,
|
||||
ObjectProvider<RedisClusterConfiguration> clusterConfiguration) {
|
||||
public RedisConnectionConfiguration(RedisProperties properties) {
|
||||
this.properties = properties;
|
||||
this.sentinelConfiguration = sentinelConfiguration.getIfAvailable();
|
||||
this.clusterConfiguration = clusterConfiguration.getIfAvailable();
|
||||
}
|
||||
|
||||
@Primary
|
||||
@Bean(BEAN_REDIS_FACTORY0)
|
||||
@ConditionalOnProperty(name = "yexuejc.redis.db0", matchIfMissing = true)
|
||||
public JedisConnectionFactory redisConnectionFactory0() throws UnknownHostException {
|
||||
return applyProperties(createJedisConnectionFactory(), 0);
|
||||
return createJedisConnectionFactory(0);
|
||||
}
|
||||
|
||||
@Bean(BEAN_REDIS_FACTORY1)
|
||||
@ConditionalOnProperty(name = "yexuejc.redis.db1")
|
||||
public JedisConnectionFactory redisConnectionFactory1() throws UnknownHostException {
|
||||
return applyProperties(createJedisConnectionFactory(), 1);
|
||||
return createJedisConnectionFactory(1);
|
||||
}
|
||||
|
||||
@Bean(BEAN_REDIS_FACTORY2)
|
||||
@ConditionalOnProperty(name = "yexuejc.redis.db2")
|
||||
public JedisConnectionFactory redisConnectionFactory2() throws UnknownHostException {
|
||||
return applyProperties(createJedisConnectionFactory(), 2);
|
||||
return createJedisConnectionFactory(2);
|
||||
}
|
||||
|
||||
@Bean(BEAN_REDIS_FACTORY3)
|
||||
@ConditionalOnProperty(name = "yexuejc.redis.db3")
|
||||
public JedisConnectionFactory redisConnectionFactory3() throws UnknownHostException {
|
||||
return applyProperties(createJedisConnectionFactory(), 3);
|
||||
return createJedisConnectionFactory(3);
|
||||
}
|
||||
|
||||
@Bean(BEAN_REDIS_FACTORY4)
|
||||
@ConditionalOnProperty(name = "yexuejc.redis.db4")
|
||||
public JedisConnectionFactory redisConnectionFactory4() throws UnknownHostException {
|
||||
return applyProperties(createJedisConnectionFactory(), 4);
|
||||
return createJedisConnectionFactory(4);
|
||||
}
|
||||
|
||||
@Bean(BEAN_REDIS_FACTORY5)
|
||||
@ConditionalOnProperty(name = "yexuejc.redis.db5")
|
||||
public JedisConnectionFactory redisConnectionFactory5() throws UnknownHostException {
|
||||
return applyProperties(createJedisConnectionFactory(), 5);
|
||||
return createJedisConnectionFactory(5);
|
||||
}
|
||||
|
||||
@Bean(BEAN_REDIS_FACTORY6)
|
||||
@ConditionalOnProperty(name = "yexuejc.redis.db6")
|
||||
public JedisConnectionFactory redisConnectionFactory6() throws UnknownHostException {
|
||||
return applyProperties(createJedisConnectionFactory(), 6);
|
||||
return createJedisConnectionFactory(6);
|
||||
}
|
||||
|
||||
@Bean(BEAN_REDIS_FACTORY7)
|
||||
@ConditionalOnProperty(name = "yexuejc.redis.db7")
|
||||
public JedisConnectionFactory redisConnectionFactory7() throws UnknownHostException {
|
||||
return applyProperties(createJedisConnectionFactory(), 7);
|
||||
return createJedisConnectionFactory(7);
|
||||
}
|
||||
|
||||
@Bean(BEAN_REDIS_FACTORY8)
|
||||
@ConditionalOnProperty(name = "yexuejc.redis.db8")
|
||||
public JedisConnectionFactory redisConnectionFactory8() throws UnknownHostException {
|
||||
return applyProperties(createJedisConnectionFactory(), 8);
|
||||
return createJedisConnectionFactory(8);
|
||||
}
|
||||
|
||||
@Bean(BEAN_REDIS_FACTORY9)
|
||||
@ConditionalOnProperty(name = "yexuejc.redis.db9")
|
||||
public JedisConnectionFactory redisConnectionFactory9() throws UnknownHostException {
|
||||
return applyProperties(createJedisConnectionFactory(), 9);
|
||||
return createJedisConnectionFactory(9);
|
||||
}
|
||||
|
||||
protected final JedisConnectionFactory applyProperties(JedisConnectionFactory factory, int database) {
|
||||
configureConnection(factory);
|
||||
if (this.properties.isSsl()) {
|
||||
factory.setUseSsl(true);
|
||||
}
|
||||
factory.setDatabase(database);
|
||||
if (this.properties.getTimeout() > 0) {
|
||||
factory.setTimeout(this.properties.getTimeout());
|
||||
}
|
||||
private JedisConnectionFactory createJedisConnectionFactory(int database) {
|
||||
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
|
||||
redisStandaloneConfiguration.setHostName(properties.getHost());
|
||||
redisStandaloneConfiguration.setPort(properties.getPort());
|
||||
redisStandaloneConfiguration.setDatabase(database);
|
||||
redisStandaloneConfiguration.setPassword(RedisPassword.of(properties.getPassword()));
|
||||
|
||||
JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
|
||||
|
||||
JedisConnectionFactory factory = new JedisConnectionFactory(redisStandaloneConfiguration,
|
||||
jedisClientConfiguration.build());
|
||||
return factory;
|
||||
}
|
||||
|
||||
private void configureConnection(JedisConnectionFactory factory) {
|
||||
if (StringUtils.hasText(this.properties.getUrl())) {
|
||||
configureConnectionFromUrl(factory);
|
||||
} else {
|
||||
factory.setHostName(this.properties.getHost());
|
||||
factory.setPort(this.properties.getPort());
|
||||
if (this.properties.getPassword() != null) {
|
||||
factory.setPassword(this.properties.getPassword());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void configureConnectionFromUrl(JedisConnectionFactory factory) {
|
||||
String url = this.properties.getUrl();
|
||||
if (url.startsWith("rediss://")) {
|
||||
factory.setUseSsl(true);
|
||||
}
|
||||
try {
|
||||
URI uri = new URI(url);
|
||||
factory.setHostName(uri.getHost());
|
||||
factory.setPort(uri.getPort());
|
||||
if (uri.getUserInfo() != null) {
|
||||
String password = uri.getUserInfo();
|
||||
int index = password.lastIndexOf(":");
|
||||
if (index >= 0) {
|
||||
password = password.substring(index + 1);
|
||||
}
|
||||
factory.setPassword(password);
|
||||
}
|
||||
} catch (URISyntaxException ex) {
|
||||
throw new IllegalArgumentException("Malformed 'spring.redis.url' " + url, ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected final RedisSentinelConfiguration getSentinelConfig() {
|
||||
if (this.sentinelConfiguration != null) {
|
||||
return this.sentinelConfiguration;
|
||||
}
|
||||
Sentinel sentinelProperties = this.properties.getSentinel();
|
||||
if (sentinelProperties != null) {
|
||||
RedisSentinelConfiguration config = new RedisSentinelConfiguration();
|
||||
config.master(sentinelProperties.getMaster());
|
||||
config.setSentinels(createSentinels(sentinelProperties));
|
||||
return config;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link RedisClusterConfiguration} if necessary.
|
||||
*
|
||||
* @return {@literal null} if no cluster settings are set.
|
||||
*/
|
||||
protected final RedisClusterConfiguration getClusterConfiguration() {
|
||||
if (this.clusterConfiguration != null) {
|
||||
return this.clusterConfiguration;
|
||||
}
|
||||
if (this.properties.getCluster() == null) {
|
||||
return null;
|
||||
}
|
||||
Cluster clusterProperties = this.properties.getCluster();
|
||||
RedisClusterConfiguration config = new RedisClusterConfiguration(clusterProperties.getNodes());
|
||||
|
||||
if (clusterProperties.getMaxRedirects() != null) {
|
||||
config.setMaxRedirects(clusterProperties.getMaxRedirects());
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
private List<RedisNode> createSentinels(Sentinel sentinel) {
|
||||
List<RedisNode> nodes = new ArrayList<RedisNode>();
|
||||
for (String node : StringUtils.commaDelimitedListToStringArray(sentinel.getNodes())) {
|
||||
try {
|
||||
String[] parts = StringUtils.split(node, ":");
|
||||
Assert.state(parts.length == 2, "Must be defined as 'host:port'");
|
||||
nodes.add(new RedisNode(parts[0], Integer.valueOf(parts[1])));
|
||||
} catch (RuntimeException ex) {
|
||||
throw new IllegalStateException("Invalid redis sentinel " + "property '" + node + "'", ex);
|
||||
}
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
private JedisConnectionFactory createJedisConnectionFactory() {
|
||||
JedisPoolConfig poolConfig = this.properties.getPool() != null ? jedisPoolConfig() : new JedisPoolConfig();
|
||||
|
||||
if (getSentinelConfig() != null) {
|
||||
return new JedisConnectionFactory(getSentinelConfig(), poolConfig);
|
||||
}
|
||||
if (getClusterConfiguration() != null) {
|
||||
return new JedisConnectionFactory(getClusterConfiguration(), poolConfig);
|
||||
}
|
||||
return new JedisConnectionFactory(poolConfig);
|
||||
}
|
||||
|
||||
private JedisPoolConfig jedisPoolConfig() {
|
||||
JedisPoolConfig config = new JedisPoolConfig();
|
||||
RedisProperties.Pool props = this.properties.getPool();
|
||||
config.setMaxTotal(props.getMaxActive());
|
||||
config.setMaxIdle(props.getMaxIdle());
|
||||
config.setMinIdle(props.getMinIdle());
|
||||
config.setMaxWaitMillis(props.getMaxWait());
|
||||
return config;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard Redis configuration.
|
||||
@ -459,5 +343,4 @@ public class MutiRedisAutoConfiguration {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,57 @@
|
||||
package com.yexuejc.springboot.base.test;
|
||||
|
||||
import com.yexuejc.base.util.JsonUtil;
|
||||
import com.yexuejc.springboot.base.ApplicationRun;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
/**
|
||||
* @author maxf
|
||||
* @PackageName com.yexuejc.springboot.base.test
|
||||
* @Description
|
||||
* @date 2018/9/26 16:28
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = ApplicationRun.class)
|
||||
public class RedisTest {
|
||||
@Autowired
|
||||
@Qualifier(RedisAutoConfiguration.BEAN_REDIS_TEMPLATE0)
|
||||
RedisTemplate redisTemplate;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("a","b");
|
||||
map.put("c",1);
|
||||
map.put("aac",true);
|
||||
map.put("q","asdb");
|
||||
redisTemplate.opsForHash().putAll("test.redis",map);
|
||||
redisTemplate.expire("test.redis",10, TimeUnit.MINUTES);
|
||||
}
|
||||
@Test
|
||||
public void get() {
|
||||
Map<String,Object> map = redisTemplate.opsForHash().entries("test.redis");
|
||||
System.out.println(JsonUtil.obj2Json(map));
|
||||
}
|
||||
@Test
|
||||
public void update() {
|
||||
Map<String,Object> map = redisTemplate.opsForHash().entries("test.redis");
|
||||
redisTemplate.opsForHash().put("test.redis","q","123456798");
|
||||
System.out.println(JsonUtil.obj2Json(map));
|
||||
}
|
||||
@Test
|
||||
public void del() {
|
||||
Boolean delete = redisTemplate.delete("test.redis");
|
||||
System.out.println(delete);
|
||||
}
|
||||
}
|
@ -94,4 +94,5 @@ public class IndexCtrl {
|
||||
return Resps.success().setSucc(map);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
server.port=8888
|
||||
logging.level.root=info
|
||||
|
||||
security.basic.enabled=false
|
||||
|
||||
yexuejc.http.filter.type=0
|
||||
|
||||
|
||||
@ -25,3 +23,9 @@ server.tomcat.uri-encoding=UTF-8
|
||||
|
||||
#是否开启HTTPS(SSL)请求证书验证忽略:默认false
|
||||
yexuejc.enable.ssl-ignore=true
|
||||
|
||||
yexuejc.redis.db1=true
|
||||
yexuejc.redis.db2=true
|
||||
spring.redis.host=121.42.165.89
|
||||
spring.redis.password=
|
||||
spring.redis.port=16379
|
||||
|
Loading…
x
Reference in New Issue
Block a user