¸üÐÂʱ¼ä:2020Äê04ÔÂ07ÈÕ17ʱ50·Ö À´Ô´:ÀÖÓã²¥¿Í ä¯ÀÀ´ÎÊý:
1.¸ÅÊö
Ëæ×Å»¥ÁªÍø¼¼ÊõµÄ·¢Õ¹£¬¶Ô¼¼ÊõÒªÇóÒ²Ô½À´Ô½¸ß£¬ËùÒÔÔÚµ±ÆÚÇé¿öÏÂÏîÄ¿µÄ¿ª·¢ÖжÔÊý¾Ý·ÃÎʵÄЧÂÊÒ²ÓÐÁ˺ܸߵÄÒªÇó£¬ËùÒÔÔÚÏîÄ¿¿ª·¢Öлº´æ¼¼ÊõʹÓõÄÒ²Ô½À´Ô½¶à£¬ÒòΪËü¿ÉÒÔ¼«´óµÄÌá¸ßϵͳµÄ·ÃÎÊËÙ¶È£¬¹ØÓÚ»º´æµÄ¿ò¼ÜÒ²ÖÖÀà·±¶à£¬±ÈÈç Redis¡¢Ehchahe¡¢JBoss Cache¡¢Voldemort¡¢Cacheonix µÈµÈ£¬½ñÌìÖ÷Òª½éÉܵÄÊÇʹÓÃÏÖÔڷdz£Á÷ÐÐµÄ NoSQL Êý¾Ý¿â(Redis)À´ÊµÏÖÎÒÃǵĻº´æÐèÇó¡£ÍƼöÁ˽âjavaÅàѵ¿Î³Ì¡£
2.SpringBoot¼ò½é
Spring Boot ÊÇÓÉ Pivotal ÍŶÓÌṩµÄȫпò¼Ü£¬ÆäÉè¼ÆÄ¿µÄÊÇÓÃÀ´¼ò»¯Ð Spring Ó¦Óõijõʼ´î½¨ÒÔ¼°¿ª·¢¹ý³Ì¡£¸Ã¿ò¼ÜʹÓÃÁËÌØ¶¨µÄ·½Ê½À´½øÐÐÅäÖ㬴ӶøÊ¹¿ª·¢ÈËÔ±²»ÔÙÐèÒª¶¨ÒåÑù°å»¯µÄÅäÖá£Í¨¹ýÕâÖÖ·½Ê½£¬Spring Boot ÖÂÁ¦ÓÚÔÚÅ·¢Õ¹µÄ¿ìËÙÓ¦Óÿª·¢ÁìÓò(rapid application development)³ÉΪÁìµ¼Õß¡£
Ö÷ÒªÌØµã£º
1)´´½¨¶ÀÁ¢µÄ Spring Ó¦ÓóÌÐò
2)ǶÈëµÄ Tomcat£¬ÎÞÐ貿Êð WAR Îļþ
3)¼ò»¯ Maven ÅäÖÃ
4)×Ô¶¯ÅäÖà Spring
5)ÌṩÉú²ú¾ÍÐ÷Ð͹¦ÄÜ£¬ÈçÖ¸±ê£¬½¡¿µ¼ì²éºÍÍⲿÅäÖÃ
6)¾ø¶ÔûÓдúÂëÉú³ÉºÍ¶Ô XML ûÓÐÒªÇóÅäÖÃ
3.Redis¼ò½é
Redis ÊÇÒ»¸ö¿ªÔ´(BSD Ðí¿É)µÄ£¬ÄÚ´æÖеÄÊý¾Ý½á¹¹´æ´¢ÏµÍ³£¬Ëü¿ÉÒÔÓÃ×÷Êý¾Ý¿â¡¢»º´æºÍÏûÏ¢Öмä¼þ£¬Redis µÄÓÅÊÆ°üÀ¨ËüµÄËÙ¶È¡¢Ö§³Ö·á¸»µÄÊý¾ÝÀàÐÍ¡¢²Ù×÷Ô×ÓÐÔ£¬ÒÔ¼°ËüµÄͨÓÃÐÔ¡£
4.ÏÂÃæ¾ÍÊÇ SpringBoot ÕûºÏ Redis ¾ßÌåʵÏÖ²½Öè
4.1 ÔÚ MavenµÄpom.xmlÎļþÖмÓÈëRedis°ü
<!—ÅäÖà redis ÒÀÀµ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>${boot.version}</version>
</dependency>
4.2 SpringBootÅäÖÃÎļþÖÐÅäÖÃRedisÁ¬½Ó
spring:
application:
name: spring-boot-redis
redis:
host: 192.168.12.62
port: 6379
timeout: 20000
pool:
max-active: 8
min-idle: 0
max-idle: 8
max-wait: -1
4.3 RedisÅäÖÃÀà
@Configuration
public class RedisApplication {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
//ʹÓà Jackson2JsonRedisSerializer À´ÐòÁл¯ºÍ·´ÐòÁл¯ redis µÄ value Öµ
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
//ʹÓà StringRedisSerializer À´ÐòÁл¯ºÍ·´ÐòÁл¯ redis µÄ key Öµ
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
4.4 Service ²ãÓ¦Óûº´æ
@Service
public class TestService {
@Autowired
private PersonRepo personRepo;
/**
* @Cacheable Ó¦Óõ½¶ÁÈ¡Êý¾ÝµÄ·½·¨ÉÏ£¬ÏÈ´Ó»º´æÖжÁÈ¡£¬Èç¹ûûÓÐÔÙ´Ó DB »ñÈ¡Êý¾Ý£¬È»ºó°ÑÊý¾ÝÌí¼Óµ½»º´æÖÐ
* unless ±íʾÌõ¼þ±í´ïʽ³ÉÁ¢µÄ»°²»·ÅÈ뻺´æ
*/
@Cacheable(value = "user", key = "#root.targetClass + #username", unless = "#result eq null")
public Person getPersonByName(String username) {
Person person = personRepo.getPersonByName(username);
return person;
}
/**
* @CachePut Ó¦Óõ½Ð´Êý¾ÝµÄ·½·¨ÉÏ£¬ÈçÐÂÔö/Ð޸ķ½·¨£¬µ÷Ó÷½·¨Ê±»á×Ô¶¯°ÑÏàÓ¦µÄÊý¾Ý·ÅÈ뻺´æ
*/
@CachePut(value = "user", key = "#root.targetClass + #result.username", unless = "#person eq null")
public Person savePerson(Person person) {
return personRepo.savePerson(person);
}
/**
* @CacheEvict Ó¦Óõ½É¾³ýÊý¾ÝµÄ·½·¨ÉÏ£¬µ÷Ó÷½·¨Ê±»á´Ó»º´æÖÐɾ³ý¶ÔÓ¦ key µÄÊý¾Ý
*/
@CacheEvict(value = "user", key = "#root.targetClass + #username", condition = "#result eq true")
public boolean removePersonByName(String username) {
return personRepo.removePersonByName(username) > 0;
}
public boolean isExistPersonName(Person person) {
return personRepo.existPersonName(person) > 0;
}
}
4.5 Êý¾Ý·ÃÎÊ×ÊÔ´Àà
@Component
@Path("personMgr")
public class PersonMgrResource {
@Autowired
private PersonService personService;
@GET
@Path("getPersonByName")
@Produces(MediaType.APPLICATION_JSON)
public JsonResp getPersonByName(@QueryParam("username") String username) {
Person person = personService.getPersonByName(username);
return JsonResp.success(person);
}
@POST
@Path("removePersonByName")
@Produces(MediaType.APPLICATION_JSON)
public JsonResp removePersonByName(@QueryParam("username") String username) {
if (personService.removePersonByName(username)) {
return JsonResp.success();
}
return JsonResp.fail("ϵͳ´íÎó£¡");
}
@POST
@Path("savePerson")
@Produces(MediaType.APPLICATION_JSON)
public JsonResp savePerson(Person person) {
if (personService.isExistPersonName(person)) {
return JsonResp.fail("Óû§ÃûÒÑ´æÔÚ£¡");
}
if (personService.savePerson(person).getId() > 0) {
return JsonResp.success();
}
return JsonResp.fail("ϵͳ´íÎó£¡");
}
}
5.ͨ¹ý postman ¹¤¾ßÀ´²âÊÔ»º´æÊÇ·ñÉúЧ
µÚÒ»´Î·ÃÎʲéÕÒÓû§£º
µÚÒ»´Îͨ¹ýÓû§Ãû³ÆÀ´²éÕÒÓû§¿ÉÒÔ¿´µ½ÊÇ´Ó¿âÖвéѯµÄÊý¾Ý£¬ÎÒÃÇ¿ÉÒÔͨ¹ý RedisClient ¹¤¾ßÀ´²é¿´Êý¾ÝÒÑ·ÅÈëÁË»º´æ¡£
µÚ¶þ´Î²éÕÒÓû§£º·¢ÏÖ·þÎñ¶Ë²¢Î´´òÓ¡ÈκÎÊý¾Ý¿â²éѯÈÕÖ¾£¬¿ÉÒÔÖªµÀµÚ¶þ´Î²éѯÊÇ´Ó»º´æÖвéѯµÃµ½µÄÊý¾Ý¡£
×ܽá
±¾ÎĽéÉÜÈçºÎͨ¹ý SpringBoot À´Ò»²½²½¼¯³É Redis »º´æ£¬¹ØÓÚ Redis µÄʹÓÃËü²»½ö¿ÉÒÔÓÃ×÷»º´æ£¬»¹¿ÉÒÔÓÃÀ´¹¹½¨¶ÓÁÐϵͳ£¬Pub/Sub ʵʱÏûϢϵͳ£¬·Ö²¼Ê½ÏµÍ³µÄµÄ¼ÆÊýÆ÷Ó¦Ó㬹ØÓÚ Redis ¸ü¶àµÄ½éÉÜ£¬ÇëǰÍù¹ÙÍø²éÔÄ¡£
²ÂÄãϲ»¶
ʲôÊÇjavaËÄ´óÒýÓÃ?JavaÒýÓýéÉÜ
±±¾©Ð£Çø