SpringBoot integrates Redis, ApachSolr, and SpringSession examples, springbootredis

Source: Internet
Author: User
Tags solr

SpringBoot integrates Redis, ApachSolr, and SpringSession examples, springbootredis

This article describes how spring boot integrates Redis, ApachSolr, and SpringSession. The details are as follows:

I. Introduction

SpringBoot has been favored by developers for its convenient configuration since its launch. It provides various starter to simplify a lot of tedious configurations. SpringBoot integrates Druid and Mybatis, which are not described in detail here. Today we will introduce how to integrate Redis, ApacheSolr, and SpringSession with SpringBoot.

Ii. Integrate SpringBoot with Redis

Redis is one of the most commonly used caches. Generally, Redis sets up high availability (HA), Cluster, or Sentinel. For details about how to set up the service, refer to the official apsaradb for Redis documentation. Here we have used Sentinel as an example. RedisSentinel generally has three nodes, Redis generally has 6379 ports, and Sentinel generally has 26379 ports.

We need to use SpringBoot to integrate Redis. First, we need to add the corresponding Redis starter to the POM:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>

After the jar package is introduced, we can directly add the RedisSentinel configuration in the application. properties file to complete integration.

spring.redis.sentinel.master=mymasterspring.redis.sentinel.nodes=192.168.2.233:26379,192.168.2.234:26379,192.168.2.235:26379spring.redis.pool.max-active=1024spring.redis.pool.max-idle=200spring.redis.pool.min-idle=100spring.redis.pool.max-wait=10000

Sentinel. master is the name of the master. The default name mymaster is used when RedisSentinel is set up.

Sentinel. nodes is the sentinel node. Note that it is the sentinel node, not the redis node. Ip: PORT format. multiple nodes are separated by commas.

Below are some information about the connection pool:

  1. Pool. max-active: Maximum number of active instances
  2. Pool. max-idle: Maximum number of idle instances
  3. Pool. min-idle: Minimum number of idle instances
  4. Pool. max-wait: maximum wait time

In the program, we can directly inject redisTemplate to operate Redis.

@Autowiredprivate StringRedisTemplate stringRedisTemplate;

Now, Redis integration is complete.

3. Integrate SpringBoot with SpringSession

SpringSession provides cluster Session management without passing through the container. It can be connected to different storage layers, such as databases, Redis, and MongoDB. It can be seamlessly integrated with SpringBoot.

First, we will introduce SpringSession to the project and add the following configuration to POM:

<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId></dependency>

Then, specify the storage type of SpringSession in application. properties:

spring.session.store-type=redis

In this way, SpringSession is easily integrated. If you have special requirements on cookies, you can create a cookie Bean in the project to replace the bean automatically created by SpringBoot. The details are as follows:

    @Bean public DefaultCookieSerializer cookieSerializer(){ DefaultCookieSerializer cookie = new DefaultCookieSerializer(); cookie.setCookieName("springboot_id"); return cookie; }  

In the preceding example, we modified the cookie name. To modify other attributes, set the relevant attribute values.

4. SpringBoot integrated Solr

ApacheSolr is a common search engine. SpringBoot can also easily integrate solr for your development. For more information about the concept and usage of ApacheSolr, see related documents. When constructing solr, we usually use zookeeper to build SolrCloud to improve Solr availability. Here we will organize SolrCloud.

First, we introduce starter of ApacheSolr:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-solr</artifactId></dependency>

Add zookeeper information in application. properties as follows:

Copy codeThe Code is as follows:
Spring. data. solr. zk-host = 192.168.2.233: 2181,192.168 .2.234: 2181,192.168 .2.235: 2181

Separate multiple zookeepers with commas.

In this way, SpringBoot integrates ApacheSolr, which is very convenient. Next we can use Spring-data to access solr.

1. Write the data returned by solr corresponding to your own object class. The specific code is as follows:

@Setter@Getter@SolrDocument(solrCoreName = "xy_company")public class SolrCompany {  @Field("id")  private String id;                   @Field("companyName_txt")  private String companyName;          }

@ Setter @ Getter: These two annotations are common and used to generate get and set methods.

@ SolrDocument (solrCoreName = "xy_company") is used to specify the core or collection in solr. The core is the name in a single instance, and the collection is the name in SolrCloud.

@ Field ("id") is used to specify the Field in solr.

2. Write your own storage layer and inherit SolrCrudRepository as follows:

public interface CompanyRepository extends SolrCrudRepository<SolrCompany,String> {  List<SolrCompany> findByCompanyName(String companyName);}

In this way, the storage layer can access solr. If multiple storage layers share one entity, you can write multiple storage layers to inherit different Repository. For more information, see Spring-data.

3. Use solr in your own business

 public List<SolrCompany> getCompanyByName(String companyName){    return companyRepository.findByCompanyName(companyName);  }

So far, SpringBoot's integration with Solr is complete. It's easy.

Next, we will introduce the integration of some mainstream frameworks, such as MongoDB and Kafka. For detailed code, see my GitHub: https://github.com/bigbugliu/spring-boot-demo.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.