どのようにカスタムredisツールjarパッケージは他のSpringBootプロジェクトに直接使用されますか?
注:(最終redisデータベース接続情報は使用者プロジェクトモジュールの構成により提供される)
一、Redisはよく保存操作で実現されます。このmoduleは最後にjarに包装されて他のサービスに使用されます。
1.引用に関する依存
注:この時はまだredis-utilツールの開発段階にありますので、reidsの設定ファイルはやはり自分のモジュールで提供します。後期にjarに包装すると、redis-utilツールの中のredis接続情報をクリアします。そして、redis-utilツールを必要とするサービスモジュールでreidsの接続情報を提供します。
reids-utilのaplication.propertiesにredisデータベース接続情報を配置します。
運行test 1:
運行test 2:
他の方法は自分でテストします。ここでは一つ一つ展示しません。
6.redisデータベース接続情報をクリアする
これからredis-utilツールの開発が完了したら、他のサービスに利用できます。最後にredis-utilモジュールのaplication.properties内のredisデータベース接続情報をクリアします。その後の接続情報はユーザーモジュールによって提供され、これではじめてredis-utilの純粋なツールとしての定義に合致します。
二、consumerプロジェクトを作成して、redis-utilツールバッグを引用します。
1.consumerプロジェクトのpom.xmlにreids-utilsを追加する依存
2.consumerのaplication.propertiesプロファイルにredisデータの接続情報を追加する
4.ブートクラスにスキャンを追加する
その他の注釈は管理しなくてもいいです。redis-utilツールバッグbeanスキャンできない問題を解決するには、注釈@ComponentScanを追加するだけでいいです。
redis-utilsツールバッグのメソッドを呼び出すことに成功しました。
この記事は他のSpring Bootプロジェクトのためにどのようにカスタマイズしますか?redisツールjarパッケージの使い方について紹介します。以前の記事を検索してください。または以下の関連記事を見てください。これからもよろしくお願いします。
一、Redisはよく保存操作で実現されます。このmoduleは最後にjarに包装されて他のサービスに使用されます。
1.引用に関する依存
<!-- spring-boot-starter-parent, -->
<!-- Redis [start] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<!-- Redis [end] -->
2.reids接続情報の配置注:この時はまだredis-utilツールの開発段階にありますので、reidsの設定ファイルはやはり自分のモジュールで提供します。後期にjarに包装すると、redis-utilツールの中のredis接続情報をクリアします。そして、redis-utilツールを必要とするサービスモジュールでreidsの接続情報を提供します。
reids-utilのaplication.propertiesにredisデータベース接続情報を配置します。
#Redis
spring.redis.host=127.0.0.1
#Redis
spring.redis.port=6379
#Redis ( 0)
spring.redis.database=0
3.カスタムプログレッシブクラスは、Redisに格納されているオブジェクトをjson形式に並べ替える
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable;
@Configuration
@EnableAutoConfiguration
public class RedisConfig {
@Bean
public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory redisConnectionFactory){
RedisTemplate<String, Serializable> template = new RedisTemplate();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
4.適切なredis常用方法を開発する。
package com.gh.redis.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Repository
public class RedisUtil {
@Autowired
RedisTemplate<String, Serializable> redisTemplate; // key-value
public RedisUtil(){
}
/**
* key
* @param key
* @return true false
*/
public boolean hasKey(String key) {
return Boolean.TRUE.equals(redisTemplate.hasKey(key));
}
/**
* 、 Redis
* @param key
* @param value
*/
public void insertOrUpdate(String key, Serializable value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 、 Redis , ( )
* @param key
* @param value
* @param seconds ( )
*/
public void insertOrUpdateBySeconds(String key, Serializable value, long seconds) {
redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
}
/**
* 、 Redis , ( )
* @param key
* @param value
* @param minutes ( )
*/
public void insertOrUpdateByMinutes(String key, Serializable value, long minutes) {
redisTemplate.opsForValue().set(key, value, minutes, TimeUnit.MINUTES);
}
/**
* 、 Redis , ( )
* @param key
* @param value
* @param hours ( )
*/
public void insertOrUpdateByHours(String key, Serializable value, long hours) {
this.redisTemplate.opsForValue().set(key, value, hours, TimeUnit.HOURS);
}
/**
* 、 Redis , ( )
* @param key
* @param value
* @param days ( )
*/
public void insertOrUpdateByDays(String key, Serializable value, long days) {
this.redisTemplate.opsForValue().set(key, value, days, TimeUnit.DAYS);
}
/**
*
* @param key
* @return
*/
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* redis key pattern key
* @param pattern
* @return
*/
public Set<String> getPattern(String pattern) {
return redisTemplate.keys("*" + pattern + "*");
}
/**
* redis
* @param key
* @return
*/
public boolean remove(String key) {
return Boolean.TRUE.equals(redisTemplate.delete(key));
}
/**
*
* @param keys 1, 2,...
* @return
*/
public int removes(String... keys){
int count = 0;
List<String> deleteFails = new ArrayList<>();
for (String key : keys) {
if (Boolean.TRUE.equals(redisTemplate.delete(key))) {
++count;
} else {
deleteFails.add(key);
}
}
if (!CollectionUtils.isEmpty(deleteFails)) {
System.err.println("======> Redis key:" + deleteFails.toString());
}
return count;
}
/**
*
* @return
*/
public int removeAll(){
Set<String> keys = redisTemplate.keys("*");
Long delete = 0L;
if (keys != null) {
delete = redisTemplate.delete(keys);
}
return delete != null ? delete.intValue() : 0;
}
}
5.工具カバンの開発が完了したら、テストしてください。
import com.gh.common.toolsclass.ResultData;
import com.gh.redis.util.RedisUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Set;
@SpringBootTest
class RedisApplicationTests {
@Autowired
private RedisUtil redisUtil;
@Test
void test1() {
ResultData resultData = new ResultData();
resultData.setCode(0);
resultData.setMessage("redis ");
resultData.setData("666666");
redisUtil.insertOrUpdate("demo", resultData);
System.err.println(redisUtil.hasKey("demo"));
Object demo = redisUtil.get("demo");
ResultData bo = (ResultData) demo;
System.err.println(bo.toString());
}
@Test
void test2() {
Set<String> list = redisUtil.getPattern("l");
for (String s: list) {
System.err.println(s);
}
}
}
ResultDataは情報を返すためのオブジェクトであり、他のオブジェクトで代替できますが、オブジェクトはSerializableインターフェースを実現する必要があります。運行test 1:
運行test 2:
他の方法は自分でテストします。ここでは一つ一つ展示しません。
6.redisデータベース接続情報をクリアする
これからredis-utilツールの開発が完了したら、他のサービスに利用できます。最後にredis-utilモジュールのaplication.properties内のredisデータベース接続情報をクリアします。その後の接続情報はユーザーモジュールによって提供され、これではじめてredis-utilの純粋なツールとしての定義に合致します。
二、consumerプロジェクトを作成して、redis-utilツールバッグを引用します。
1.consumerプロジェクトのpom.xmlにreids-utilsを追加する依存
<!-- redis [start] -->
<dependency>
<groupId>com.gh</groupId>
<artifactId>redis-util</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- redis [end] -->
pomはどうやってカスタマイズされたjarバッグを引用して自分に依存しますか?もし同じ親工程モジュールの下であれば、直接にこのように引用できます。同じ父の工事ではなく、jarを先にmaven倉庫に入れておく必要があります。2.consumerのaplication.propertiesプロファイルにredisデータの接続情報を追加する
#Redis
spring.redis.host=127.0.0.1
#Redis
spring.redis.port=6379
#Redis ( 0)
spring.redis.database=0
3.cunsumerでredis-utilツールバッグを使用できるかどうかをテストする方法
package com.gh.consumer;
import com.gh.common.toolsclass.ResultData;
import com.gh.redis.util.RedisUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ConsumerApplicationTests {
// ,
final RedisUtil redisUtil;
@Autowired
public ConsumerApplicationTests(RedisUtil redisUtil){
this.redisUtil = redisUtil;
}
@Test
void test1() {
// demo ,
if (redisUtil.hasKey("demo")) {
System.err.println(redisUtil.remove("demo"));
}
// demo
ResultData resultData = new ResultData();
resultData.setCode(0);
resultData.setMessage("redis -2");
resultData.setData("888888");
redisUtil.insertOrUpdate("demo", resultData);
Object demo = redisUtil.get("demo");
ResultData bo = (ResultData) demo;
System.err.println(bo.toString());
}
@Test
void test2() {
redisUtil.insertOrUpdate("test", "redis ");
System.err.println(redisUtil.get("test"));
}
}
test 1を実行していますが、この時は、操作台からRedisUtilのbeanが見つからないように指示があります。4.ブートクラスにスキャンを追加する
その他の注釈は管理しなくてもいいです。redis-utilツールバッグbeanスキャンできない問題を解決するには、注釈@ComponentScanを追加するだけでいいです。
package com.gh.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.scheduling.annotation.EnableScheduling;
//@EnableDiscoveryClient eureka
@EnableFeignClients(basePackages = "com.gh.consumer.feign")
//@ComponentScan(basePackages = "com.gh.consumer.*")
@ComponentScans(value = {
@ComponentScan(value = "com.gh.consumer.*")
,@ComponentScan(value = "com.gh.redis.*")
})
@EnableScheduling //
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
5.再試験redis-utilsツールバッグのメソッドを呼び出すことに成功しました。
この記事は他のSpring Bootプロジェクトのためにどのようにカスタマイズしますか?redisツールjarパッケージの使い方について紹介します。以前の記事を検索してください。または以下の関連記事を見てください。これからもよろしくお願いします。