Project git URL: https://github.com/David-BIQI/manage.git (project using newer springboot2.0 and JDK8)
Referenced code specification: Https://github.com/xwjie/PLMCodeTemplate.git (This is a set of code specifications can be landed, followed by the wind Brother learning a lot)
Redis Configuration
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-redis</artifactid></dependency>
#redis设置 redis: host:localhost 6379 0
- Format conversion for Redis
There are also converted to JSON storage, written in the demo has stringredistemplate storage, the following is the use of serialization
PackageCom.common.redis;ImportOrg.springframework.core.convert.converter.Converter;ImportOrg.springframework.core.serializer.support.DeserializingConverter;ImportOrg.springframework.core.serializer.support.SerializingConverter;ImportOrg.springframework.data.redis.serializer.RedisSerializer;Importorg.springframework.data.redis.serializer.SerializationException;/*** Redis serialized Tool class*/ Public classRedisobjectserializerImplementsRedisserializer<object> { PrivateConverter<object,byte[]> serializer =NewSerializingconverter (); Privateconverter<byte[], object> deserializer =NewDeserializingconverter (); Static Final byte[] Empty_array =New byte[0]; @Override PublicObject Deserialize (byte[] bytes) { if(IsEmpty (bytes)) {return NULL; } Try { returnDeserializer.convert (bytes); } Catch(Exception ex) {Throw NewSerializationException ("Cannot deserialize", ex); }} @Override Public byte[] Serialize (Object object) {if(Object = =NULL) { returnEmpty_array; } Try { returnSerializer.convert (object); } Catch(Exception ex) {returnEmpty_array; } } Private BooleanIsEmpty (byte[] data) { return(Data = =NULL|| Data.length = = 0); }}
PackageCom.config;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.data.redis.connection.RedisConnectionFactory;Importorg.springframework.data.redis.connection.jedis.JedisConnectionFactory;Importorg.springframework.data.redis.core.RedisTemplate;ImportOrg.springframework.data.redis.serializer.StringRedisSerializer;Importcom.biqi.dto.UserDto;ImportCom.common.redis.RedisObjectSerializer; @Configuration Public classredisconfig {@Bean jedisconnectionfactory jedisconnectionfactory () {return Newjedisconnectionfactory (); } @Bean PublicRedistemplate<string, userdto>redistemplate (Redisconnectionfactory factory) {redistemplate<string, userdto> template =NewRedistemplate<string, userdto>(); Template.setconnectionfactory (Jedisconnectionfactory ()); Template.setkeyserializer (NewStringredisserializer ()); Template.setvalueserializer (NewRedisobjectserializer ()); returntemplate; }}
- Redis read and take, so that the simple read-write function in Redis can be realized
@AutowiredPrivatestringredistemplate stringredistemplate; @AutowiredPrivateRedistemplate<string, userdto>redistemplate; Publicstring Saveandget (string name) {Stringredistemplate.opsforvalue (). Set (name, name); String Temp=Stringredistemplate.opsforvalue (). get (name); returntemp; } PublicBoolean Saveuserbyserializer (Integer ID) {User user=Userservice.getuserbyid (ID); Notnull (User,"User information does not exist"); Userdto Temp=Newuserdto (); Temp.setname (User.getname ()); Temp.setphone (User.getphone ()); Redistemplate.opsforvalue (). Set (User.getname (), temp); return true; }
The perfect log,
- How to write a good log Xiaofeng Light Project AOP Modification
1. Do not rely on debug, more rely on the log.
Others face the object programming, you face debug programming. Some people, no matter what the language, eventually become the face of debug programming. Ha ha. This habit is very, very bad! Debug will let you write code when lazy do not log, and is a waste of time. Get rid of this bad habit.
2. After the code development test do not rush to submit, first run to see if the log can understand.
Log is for people to see, as long as the people who love programming can become qualified programmers, do not rush to write the functional test OK to submit code, the log is also part of the function. To have the Spirit of excellence Craftsman!
- Local how to use, improve the log, at the time of the filter to determine the user name of the login, put into the MD5, and then add the user information in the log logs
@Override Public voidDoFilter (ServletRequest srequest, Servletresponse sresponse, Filterchain filterchain)throwsIOException, servletexception {httpservletrequest request=(httpservletrequest) srequest; String URL=Request.getrequesturi (); //Login and skip it or go to the other if(Login_url.equals (URL)) {System.out.println ("This is the way to log in."); Filterchain.dofilter (Srequest, sresponse); } Else{HttpSession session= Request.getsession (true); if(Session = =NULL) { Throw Newunloginexception (); } //getting user information from the session into the tool classString usertoken =(String) Session.getattribute (Userutil.key_user); Userutil.setuser (usertoken); Filterchain.dofilter (Srequest, sresponse); } }
Public Static void SetUser (String userid) { tluser.set (userid); // Put user information into log4j mdc.put (Key_user, UserID); }
<!--console output-- class= "Ch.qos.logback.core.ConsoleAppender" > class= " Ch.qos.logback.classic.encoder.PatternLayoutEncoder "> <!--formatted output:%d for date,%thread for thread name,%- 5level: Level shows 5 character width from left%msg: Log message,%n is newline-- <pattern>%d{mm-dd HH:mm:ss}%x{user} [%thread]%-5level-% LOGGER{50}-%msg%n</pattern> </encoder> </appender>
The effect is as follows
So the log user information is basically completed, but to write a good log, there is a need to do, a look at the log to know the business implementation, the number of successful failures and so on, pick myself up!!!
Springboot Manual Build Project--redis Configuration & log Perfect + username