diff --git a/springboot-async/pom.xml b/springboot-async/pom.xml index e52ebaa..bea94aa 100644 --- a/springboot-async/pom.xml +++ b/springboot-async/pom.xml @@ -15,7 +15,7 @@ org.springframework.boot spring-boot-starter-parent - 1.5.10.RELEASE + 2.0.4.RELEASE @@ -50,7 +50,6 @@ maven-compiler-plugin 3.6.1 - 1.8 1.8 diff --git a/springboot-batch/pom.xml b/springboot-batch/pom.xml index e70d8a9..2e92786 100644 --- a/springboot-batch/pom.xml +++ b/springboot-batch/pom.xml @@ -15,7 +15,7 @@ org.springframework.boot spring-boot-starter-parent - 1.5.10.RELEASE + 2.0.4.RELEASE diff --git a/springboot-cache/README.md b/springboot-cache/README.md index cca42cb..03c42ed 100644 --- a/springboot-cache/README.md +++ b/springboot-cache/README.md @@ -2,9 +2,15 @@ 基于注解的声明式缓存 +SpringBoot 2.0的写法有些改变,参考: + +https://3dot141.com/blogs/20329.html + +https://my.oschina.net/u/3773384/blog/1795296 + ## 运行 -初始化sql文件在`resources/sql/t_user.sql`中 +本地安装好MySQL 5.7,并执行初始化sql脚本:`resources/sql/t_user.sql` 另外还需要安装Redis,配置好`application.yml`文件中的redis地址 diff --git a/springboot-cache/pom.xml b/springboot-cache/pom.xml index 9144bf3..6c03c78 100644 --- a/springboot-cache/pom.xml +++ b/springboot-cache/pom.xml @@ -15,7 +15,7 @@ org.springframework.boot spring-boot-starter-parent - 1.5.10.RELEASE + 2.0.4.RELEASE @@ -34,6 +34,16 @@ org.springframework.boot spring-boot-starter-data-redis + + redis.clients + jedis + 2.9.0 + + + com.fasterxml.jackson.core + jackson-databind + 2.9.6 + org.springframework.boot spring-boot-starter-test diff --git a/springboot-cache/src/main/java/com/xncoding/trans/config/RedisCacheConfig.java b/springboot-cache/src/main/java/com/xncoding/trans/config/RedisCacheConfig.java index c288235..ceeff0a 100644 --- a/springboot-cache/src/main/java/com/xncoding/trans/config/RedisCacheConfig.java +++ b/springboot-cache/src/main/java/com/xncoding/trans/config/RedisCacheConfig.java @@ -3,14 +3,30 @@ package com.xncoding.trans.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizer; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext; +import org.springframework.data.redis.serializer.StringRedisSerializer; import java.lang.reflect.Method; +import java.time.Duration; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; /** * RedisCacheConfig @@ -21,17 +37,73 @@ import java.util.Arrays; */ @Configuration @EnableCaching -public class RedisCacheConfig { +public class RedisCacheConfig extends CachingConfigurerSupport { private Logger logger = LoggerFactory.getLogger(this.getClass()); - /** - * 重新配置RedisCacheManager - */ - @Autowired - public void configRedisCacheManger(RedisCacheManager rd) { - rd.setDefaultExpiration(100L); + @Value("${spring.redis.host}") + private String host; + + @Value("${spring.redis.port}") + private String port; + + @Bean + public RedisStandaloneConfiguration getRedisClient() { + RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(host, Integer.parseInt(port)); + return redisStandaloneConfiguration; } + @Bean + public JedisConnectionFactory redisConnectionFactory(RedisStandaloneConfiguration RedisStandaloneConfiguration) { + JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(RedisStandaloneConfiguration); + return jedisConnectionFactory; + } + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory cf) { + RedisTemplate redisTemplate = new RedisTemplate(); + redisTemplate.setConnectionFactory(cf); + return redisTemplate; + } + + @Bean + public RedisCacheConfiguration redisCacheConfiguration() { + return RedisCacheConfiguration + .defaultCacheConfig() + .serializeKeysWith( + RedisSerializationContext + .SerializationPair + .fromSerializer(new StringRedisSerializer())) + .serializeValuesWith( + RedisSerializationContext + .SerializationPair + .fromSerializer(new GenericJackson2JsonRedisSerializer())) + .entryTtl(Duration.ofSeconds(600L)); + } + + @Bean + public CacheManager cacheManager(RedisConnectionFactory cf) { + //RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(cf); + //RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, RedisCacheConfiguration.defaultCacheConfig()); + RedisCacheManager cm = RedisCacheManager.builder(cf).cacheDefaults(redisCacheConfiguration()).build(); + return cm; + } + + // @Bean + // public KeyGenerator keyGenerator() { + // return new KeyGenerator() { + // @Override + // public Object generate(Object o, Method method, Object... objects) { + // StringBuilder sb = new StringBuilder(); + // sb.append(o.getClass().getName()); + // sb.append(method.getName()); + // for (Object obj : objects) { + // sb.append(obj.toString()); + // } + // return sb.toString(); + // } + // }; + // } + /** * 自定义缓存key的生成类实现 */ diff --git a/springboot-cache/src/main/java/com/xncoding/trans/service/UserService.java b/springboot-cache/src/main/java/com/xncoding/trans/service/UserService.java index 328be99..cedcd24 100644 --- a/springboot-cache/src/main/java/com/xncoding/trans/service/UserService.java +++ b/springboot-cache/src/main/java/com/xncoding/trans/service/UserService.java @@ -26,7 +26,7 @@ public class UserService { * @param id * @return */ - @Cacheable(cacheNames = "user1", key = "#id") + @Cacheable(key = "#id") public User getById(int id) { logger.info("获取用户start..."); return userMapper.selectById(id); @@ -39,7 +39,7 @@ public class UserService { * @param id * @return */ - @Cacheable(cacheNames = "user1", key = "#id", sync = true) + @Cacheable(key = "#id", sync = true) public User getById2(int id) { logger.info("获取用户start..."); return userMapper.selectById(id); @@ -55,7 +55,7 @@ public class UserService { * @param id * @return */ - @Cacheable(cacheNames = "user1", keyGenerator = "myKeyGenerator") + @Cacheable(keyGenerator = "myKeyGenerator") public User queryUserById(int id) { logger.info("queryUserById,id={}", id); return userMapper.selectById(id); @@ -66,9 +66,9 @@ public class UserService { * * @param user */ - @CachePut(cacheNames = "user1", key = "#user.id") + @CachePut(key = "#user.id") public void createUser(User user) { - logger.info("创建用户start..."); + logger.info("创建用户start..., user.id=" + user.getId()); userMapper.insert(user); } @@ -77,7 +77,7 @@ public class UserService { * * @param user */ - @CachePut(cacheNames = "user1", key = "#user.id") + @CachePut(key = "#user.id") public void updateUser(User user) { logger.info("更新用户start..."); userMapper.updateById(user); @@ -86,7 +86,7 @@ public class UserService { /** * 对符合key条件的记录从缓存中book1移除 */ - @CacheEvict(cacheNames = "user1", key = "#id") + @CacheEvict(key = "#id") public void deleteById(int id) { logger.info("删除用户start..."); userMapper.deleteById(id); @@ -95,7 +95,7 @@ public class UserService { /** * allEntries = true: 清空user1里的所有缓存 */ - @CacheEvict(cacheNames="user1", allEntries=true) + @CacheEvict(allEntries=true) public void clearUser1All(){ logger.info("clearAll"); } diff --git a/springboot-cache/src/main/resources/application.yml b/springboot-cache/src/main/resources/application.yml index 18a7adc..3938cb3 100644 --- a/springboot-cache/src/main/resources/application.yml +++ b/springboot-cache/src/main/resources/application.yml @@ -10,9 +10,9 @@ spring: profiles: active: dev datasource: - url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 + url: jdbc:mysql://123.207.66.156:3306/test?useSSL=false&autoReconnect=true&tinyInt1isBit=false&useUnicode=true&characterEncoding=utf8 username: root - password: 123456 + password: _EnZhi123 ################### mybatis-plus配置 ################### mybatis-plus: @@ -43,20 +43,14 @@ spring: cache: type: REDIS redis: - host: 127.0.0.1 + host: 123.207.66.156 port: 6379 - timeout: 0 database: 0 - pool: - max-active: 100 - max-wait: -1 - max-idle: 8 - min-idle: 0 logging: level: ROOT: INFO com: xncoding: DEBUG - file: E:/logs/trans.log + file: D:/logs/springboot-cache.log diff --git a/springboot-cxf/pom.xml b/springboot-cxf/pom.xml index eb7e706..01fcfe3 100644 --- a/springboot-cxf/pom.xml +++ b/springboot-cxf/pom.xml @@ -15,7 +15,7 @@ org.springframework.boot spring-boot-starter-parent - 1.5.10.RELEASE + 2.0.4.RELEASE diff --git a/springboot-cxf/src/main/resources/application.yml b/springboot-cxf/src/main/resources/application.yml index dafde37..4312020 100644 --- a/springboot-cxf/src/main/resources/application.yml +++ b/springboot-cxf/src/main/resources/application.yml @@ -30,4 +30,4 @@ logging: ROOT: INFO com: xncoding: DEBUG - file: E:/logs/app.log + file: D:/logs/app.log diff --git a/springboot-cxf/src/test/java/com/xncoding/webservice/ApplicationTests.java b/springboot-cxf/src/test/java/com/xncoding/webservice/ApplicationTests.java index 5a23140..0b6def4 100644 --- a/springboot-cxf/src/test/java/com/xncoding/webservice/ApplicationTests.java +++ b/springboot-cxf/src/test/java/com/xncoding/webservice/ApplicationTests.java @@ -9,8 +9,8 @@ import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; import static org.hamcrest.MatcherAssert.assertThat; @@ -98,15 +98,14 @@ public class ApplicationTests { } } - - /** - * 方式4. 客户端代码生成方式 - */ - @Test - public void cl4() { - CommonService_Service c = new CommonService_Service(); - com.xncoding.webservice.client.User user = c.getCommonServiceImplPort().getUser("Tom"); - assertThat(user.getName(), is("Tom")); - } + // /** + // * 方式4. 客户端代码生成方式 + // */ + // @Test + // public void cl4() { + // CommonService_Service c = new CommonService_Service(); + // com.xncoding.webservice.client.User user = c.getCommonServiceImplPort().getUser("Tom"); + // assertThat(user.getName(), is("Tom")); + // } }