Springboot Integration Redis

Source: Internet
Author: User
Tags redis tmp folder redis desktop manager redis server

Basic Learning process Please check the Mason's blog
http://www.bysocket.com/?cat=72

Examples of his various subsections
Https://github.com/JeffLi1993/springboot-learning-example

Sms-server

POM. XML, because the project is mainly used to send text messaging services, so the other dependency package plus it does not hurt
The way the war package is played is as follows

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.xialeme</groupId> <artifactId> Sms-server</artifactid> <version>0.0.1-SNAPSHOT</version> <!--deployed to Tomcat requires the use of--> ;p ackaging>war</packaging> <properties> <start-class>com. application</start-class><!--main file path--> <project.build.sourceencoding>utf-8</project.buil
            d.sourceencoding> </properties> <repositories><!--Ali code base--> <repository> <id>maven-ali</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</u Rl> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> <up Datepolicy>always</updatepolicy> <checksumPolicy>fail</checksumPolicy> &L T;/snapshots> </repository> </repositories> <!--spring boot--> <parent&gt
        ; <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-parent</ artifactid> <version>1.3.0.RELEASE</version> <relativepath/> <!--lookup parent F Rom Repository--> </parent> <dependencies> <!--deployment to Tomcat requires the use of--> <de Pendency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot -starter-tomcat</artifactid> <!--<scope>provided</scope>--> </dependency&



  Gt      <!--spring boot--> <dependency> <groupid>org.springframework.boot</groupi d> <artifactId>spring-boot-starter-web</artifactId> </dependency> &LT;DEP Endency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot- Starter-test</artifactid> <scope>test</scope> </dependency> <!--primer --> <dependency> &LT;GROUPID&GT;ORG.SPRINGFRAMEWORK.BOOT&L required for spring-boot-starter-actuator monitoring

        T;/groupid> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--introduced Spring-boot-starter-redis--> <dependency> <groupid>org.springframew Ork.boot</groupid> <artifactId>spring-boot-starter-redis</artifactId> </dependenc Y> <!--ALibaba Fastjson httpclient--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.8</version> </depend Ency> <dependency> <groupId>org.apache.httpcomponents</groupId> <

        Artifactid>httpclient</artifactid> <version>4.5.3</version> </dependency>
            <!--JPA--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <depe Ndency> <groupId>mysql</groupId> &LT;ARTIFACTID&GT;MYSQL-CONNECTOR-JAVA&LT;/ARTIFAC tid> </dependency> </dependencies> <build> <plugins> &
          Lt;plugin>      <groupId>org.springframework.boot</groupId> &LT;ARTIFACTID&GT;SPRING-BOOT-MAVEN-PLUGIN&L t;/artifactid> </plugin> <plugin> &LT;GROUPID&GT;ORG.APACHE.MAVEN.P Lugins</groupid> <artifactId>maven-war-plugin</artifactId> &LT;VERSION&G T;2.1.1</version> <configuration> <encoding>${project.build.sourceen  Coding}</encoding> <skipTests>true</skipTests> <!--The name of the war package --> <warName>sms-server</warName> <!--Mave enabled when the main path (because the other package inside the test The main function is also added)--> <mainClass>${start-class}</mainClass> <skip>t
                rue</skip><!--Skip Test--> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> </plugins> </build> </project> 

Application.properties configuration src/main/resources below

# Listening Port server.port=8081 # #http://blog.csdn.net/xiaoyu411502/article/details/47865561 ############################# ########################### ### Tomcat Configuration ######################################################## # Tomcat maximum number of threads, The default is #server. tomcat.max-threads=800 # Tomcat's URI encoding #server. Tomcat.uri-encoding=utf-8 # Temporary folders that store Tomcat logs, dumps, and other files , the default is the system's TMP folder (such as: C:\Users\Shanhy\AppData\Local\Temp) #server. Tomcat.basedir=d:/springboot-log # Opens Tomcat's access log, And you can set the method of logging format: #server. tomcat.access-log-enabled=true #server. tomcat.access-log-pattern= # Accesslog directory, default in basedir/ Logs #server. tomcat.accesslog.directory= # log file directory Logging.path=d:/springboot-log # log file name, default to Spring.log logging.fi Le=springboot.log ######################################################## # # #datasource Configure the MySQL data source; #############
########################################### Spring.datasource.url=jdbc:mysql://192.168.10.10:3306/test Spring.datasource.username=root Spring.datasource.password=root SPRINg.datasource.driver-class-name=com.mysql.jdbc.driver # Hibernate DDL auto (Create, Create-drop, update)
Spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.datasource.max-active=20 spring.datasource.max-idle=8 Spring.datasource.min-idle=8 spring.datasource.initial-size=10 # # MyBatis Configuration #由于使用的是jpa所以这里就注释掉mybatis # Mybatis.typealiasespackage=org.spring.springboot.domain #mybatis. Mapperlocations=classpath:mapper/*.xml ######## ################################################ ### Java Persistence Api to automatically build tables ################################### ##################### # Specify the DBMS spring.jpa.database = MYSQL # show or ' not ' log for each sqlquery spring.jpa.show-s QL = true # Hibernate DDL auto (create,create-drop, update) Spring.jpa.hibernate.ddl-auto = update # naming strategy Sprin G.jpa.hibernate.naming-strategy =org.hibernate.cfg.improvednamingstrategy # stripped before adding them tothe entity Manager) Spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.mysql5dialeCT ######################################################## # # #REDIS (redisproperties) redis basic configuration; #################
####################################### # # Redis configuration # Redis Database index (default 0) spring.redis.database=0 # # Redis server address spring.redis.host=127.0.0.1 # Redis Server connection Port spring.redis.port=6379 # Redis Server connection password (default is empty) spring.redis.password= # Connection pool Maximum connections (using a negative value to indicate no limit) Spring.redis.pool.max-active=8 # Connection pool maximum blocking wait time (using negative values to indicate no limit) Spring.redis.pool.max-wait=-1 #
Maximum idle connections in a connection pool spring.redis.pool.max-idle=8 # minimum idle connection in connection pool spring.redis.pool.min-idle=0 # # Connection Timeout (ms)

 Spring.redis.timeout=0

Application cannot be placed in the root directory of the Java.main, you must build a directory to store it or make a mistake
it automatically scans for relevant annotations in subdirectories under its directory

Package com;
Import java.util.ArrayList;
Import java.util.List;

Import Java.util.concurrent.TimeUnit;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.boot.builder.SpringApplicationBuilder;
Import Org.springframework.boot.context.embedded.FilterRegistrationBean;
Import Org.springframework.boot.context.web.SpringBootServletInitializer;
Import Org.springframework.context.annotation.Bean;
Import Org.springframework.data.redis.core.RedisTemplate;
Import org.springframework.data.redis.core.ValueOperations;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.ResponseBody;

Import Org.springframework.web.bind.annotation.RestController; Import Com.common.filter.


AuthFilter; /******* * Springboot default does not need to inherit Springbootservletinitializer * <p> If you deploy to an external tomcat, you need to inherit Springbootservletinitializer and rewrite Configure * Pom to add <packaging>war</packaging>  and Spring-boot-starter-tomcat.jar * </p> * @author Bamboo * */@RestController @SpringBootApplication public class

    Application {@Autowired private redistemplate redistemplate; @Override protected Springapplicationbuilder Configure (Springapplicationbuilder application) {return applicat
    Ion.sources (Application.class);
    @RequestMapping ("/")//@ResponseBody String Home () {return "SMS Server"; @RequestMapping ("/city")//@ResponseBody string city (string id) {//Get urban information from cache string K
        EY = "City_" + ID;

        Valueoperations<string, string> operations = Redistemplate.opsforvalue ();
        Cache exists Boolean haskey = Redistemplate.haskey (key); if (haskey) {String city = Operations.get (key);
            System.out.println ("Cityserviceimpl.findcitybyid (): Get the city from the cache >>" + city.tostring ());
        return to City;

        ///Get the city information from DB String cities = "Empty City";
        Insert Cache Operations.set (key, city, Timeunit.seconds, etc.);

        System.out.println ("Cityserviceimpl.findcitybyid (): City Insert Cache >>" + city.tostring ());
    return to City;
    public static void Main (string[] args) throws Exception {Springapplication.run (application.class, args);

 }



}

Run Application Main method

Refresh the browser two times to see the console print as follows
Http://localhost:8081/city?id=1

Cityserviceimpl.findcitybyid (): City Insert Cache >> empty city
Cityserviceimpl.findcitybyid (): From the cache to get the city >> empty city

Redis Desktop Manager Refresh to see that the data is plugged in.

Related Article

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.