Springboot NoSQL Introduction Spring data provides other projects to help you use a wide variety of nosql technologies, including MongoDB, neo4j, Elasticsearch, SOLR, Redis,gemfire, Couchbase and Cassandra. Spring boot provides automatic configuration for Redis, MongoDB, Elasticsearch, SOLR, and GemFire. You can make the most of other projects, but you need to configure them yourself. 1.1, Redis is a cache, message middleware and a rich feature of the key-value storage System. Spring boot provides automatic configuration for the Jedis client library and the Jedis client-based abstraction provided by spring Data Redis. Spring-boot-starter-redis ' starter POM ' provides a convenient way to collect dependencies. Redis Add maven Dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-test</artifactid>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> Spring-boot-starter</artifactid>
<!--<version>1.3.5.RELEASE</version>-
</ Dependency>
<!--https://mvnrepository.com/artifact/org.springframework.data/spring-data-commons-- >
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactid >spring-boot-starter-redis</artifactId>
<!--<version>1.3.6.RELEASE</version>-- >
</dependency>
1.2 Connect to Redis you can inject an automatically configured redisconnectionfactory,stringredistemplate or an ordinary redistemplate instance that is the same as other spring beans. By default, this instance will attempt to use localhost:6379 to connect to the Redis server.
@Component public
class Mybean {
private stringredistemplate template;
@Autowired public
Mybean (stringredistemplate template) {
this.template = template;
}
// ...
}
If you add a @bean of any of your own automatic configuration types, it will replace the default (except for Redistemplate, which is excluded based on the Bean's name ' Redistemplate ' instead of its type). If commons-pool2 exists under the classpath path, you will get a connection pool factory by default. 1.3 Setting up multiple Redis connections using Redis's default configuration to connect to 0 libraries in Redis, if you need to configure INDEXDB for a specified library connection, and if you need to connect multiple Redis services, you also need to configure multiple data sources 1.3.1, The Application.yml file adds:
@Component public
class Mybean {
private stringredistemplate template;
@Autowired public
Mybean (stringredistemplate template) {
this.template = template;
}
// ...
}
1.3.2, creating Redisconfiguration
@Configuration public class Redis137_11configuration {@Bean (name = "Redis123template") public stringredistemplate Redi Stemplate (@Value ("${redis123.hostname}") String HostName, @Value ("${redis123.port}") int port, @Value ("${redis12 3.password} ") String password, @Value (" ${redis123.maxidle} ") int maxidle, @Value (" ${redis123.maxtotal} ") int Maxtota L, @Value ("${redis123.index}") int index, @Value ("${redis123.maxwaitmillis}") long Maxwaitmillis, @Value ("${redis
123.testOnBorrow} ") Boolean Testonborrow) {stringredistemplate temple = new Stringredistemplate (); Temple.setconnectionfactory (ConnectionFactory (hostName, port, password, maxidle, Maxtotal, index, Maxwaitmillis, test
Onborrow));
return temple; } Public redisconnectionfactory ConnectionFactory (string hostName, int port, string password, int maxidle, int maxtot AL, int index, Long maxwaitmillis, Boolean testonborrow) {jedisconnectionfactory Jedis = new Jedisconnectionfactory (
); Jedis.sEthostname (HostName);
Jedis.setport (port); if (!
Stringutils.isempty (password)) {Jedis.setpassword (password);
} if (Index! = 0) {jedis.setdatabase (index);
} jedis.setpoolconfig (Poolcofig (Maxidle, Maxtotal, Maxwaitmillis, Testonborrow));
Initialize Connection pool jedis.afterpropertiesset ();
Redisconnectionfactory factory = Jedis;
return factory; } public jedispoolconfig poolcofig (int maxidle, int maxtotal, Long Maxwaitmillis, Boolean testonborrow) {Jedispool
Config poolcofig = new Jedispoolconfig ();
Poolcofig.setmaxidle (Maxidle);
Poolcofig.setmaxtotal (maxtotal);
Poolcofig.setmaxwaitmillis (Maxwaitmillis);
Poolcofig.settestonborrow (Testonborrow);
return poolcofig;
}
}
1.3.3, declaring Redis abstract base class, how to create a Redis
Public abstract class Abredisconfiguration {
protected stringredistemplate Temple;
public void SetData (string key, String value) {
gettemple (). Opsforvalue (). Set (key, value);
}
Public String GetData (string key) {
return gettemple (). Opsforvalue (). get (key);
}
Public Stringredistemplate gettemple () {
return temple;
}
}
1.3.4, depending on the data source, create different subclasses @component
public class Redis123 extends Abredisconfiguration {
@Resource (name = "Redis123template")
private Stringredistemplate Temple;
Public Stringredistemplate gettemple () {
return temple;
}
}
PS: Both the Gettemple method and the Stringredistemple property are declared in the class and subclass, and the subclasses pass the Getteimple method of the parent class to the parent class by overriding their own stringredistemple property. The parent class uses the Stringredistemple data link to manipulate the cache by passing through the subclass. At this point, the parent class completes all the methods of operation, and when a database connection needs to be created, it is only necessary to create a subclass, declare its own stringredistemple, and pass it to the parent class.