前提条件:Redisクラスター環境を構築します。建設方法:https://www.vevb.com/article/143749.htmをご覧ください
1.新しいプロジェクトを作成し、pom.xmlファイルにRedisサポートを追加します
<Dependency> groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-data-redis </artifactid> </dependency>
2。Application.Propertiesを構成します
spring.redis.cluster.nodes = 127.0.0.1:6380,127.0.1:6381,127.0.0.1:6382,127.0.0.1:6383,127.0 .0.1:6384,127.0.0.1:6385SPRING.REDIS.CLUSTER.TIMEOUT = 1000SPRING.REDIS.CLUSTER.MAX-REDIRECTS = 3
3.次の2つのクラスを作成します
@configurationPublic Class RedisconFiguration {@Resource Private LettuceConnectionFactory MyLetTuceConnectionFactory; @bean public redistemplate <string、serializable> redistemplate(){redistemplate <string、serializable> template = new Redistemplate <>(); template.setKeyserializer(new Stringredisserializer()); template.setValueserializer(new genericJackson2jsonredisserializer()); template.setConnectionFactory(mylettuceconnectionFactory);テンプレートを返します。 }} @configurationPublic Class RedisfactoryConfig {@Autowiredプライベート環境環境; @bean public redisconnectionfactory mylettuceconnectionfactory(){map <string、object> source = new hashmap <string、object>(); source.put( "spring.redis.cluster.nodes"、Environment.getProperty( "spring.redis.cluster.nodes")); source.put( "spring.redis.cluster.timeout"、Environment.getProperty( "spring.redis.cluster.timeout")); source.put( "spring.redis.cluster.max-redirects"、Environment.getProperty( "spring.redis.cluster.max-redirects")); RedisclusterConfiguration RedisclusterConfiguration; redisclusterConfiguration = new RedisclusterConfiguration(new MappRopertySource( "redisclusterConfiguration"、source));新しいlettuceconnectionFactory(redisclusterConfiguration)を返します。 }}4.テストを実行します
@springboottest@runwith(springrunner.class)public class redisconfigurationtest {@autowiredprivate redistemplate redistemplate; @testpublic void redistemplate()スロー{redistemplate.opsforvalue()。 5. Redisデスクトップマネージャーを使用してRedisノードに接続し、内部のデータが著者として存在するかどうかを確認します。以下はそれを示しており、成功していることが証明されています。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。