Wednesday, June 16, 2021

Spring boot vs Redis Sentinel

 Để Spring boot có thể kết nối được đến Redis sentinel thì cần phải cấu hình trong file  application.yml như sau:



application.yml

spring:
  cache:
    type: redis  
 
  redis:
    sentinel:
      master: mymaster
      nodes:
        - redis-sentinel:26379
        - redis-sentinel-1:26380
    timeout: 1s
 
    lettuce:
      cluster:
        refresh:
          adaptive: true
          period: 5s

Trong class Application cần thêm vào annotation @EnableCaching:
@SpringBootApplication
@EnableCaching
public class CustomerApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(CustomerApplication.class, args);
	}
 
}

Trong class Service sử dụng các annotation @CacheEvict, @Cacheable để chỉ ra việc ghi
và đọc dữ liệu vào redis cache:

  1. @Service
  2. public class CustomerServiceImpl implements CustomerService {
  3.  
  4. @Autowired
  5. CustomerRepository customerRepository;
  6.  
  7. @CacheEvict(value= "employeeCache", allEntries= true)
  8. @Override
  9. public boolean insertEmployee(CustomerVO customer) {
  10.  
  11. Customer temp = new Customer();
  12. temp.setName(customer.getName());
  13. temp.setAddress(customer.getAddress());
  14. Customer cust = customerRepository.save(temp);
  15. if(cust == null) {
  16. return false;
  17. }
  18. return true;
  19. }
  20.  
  21. @CacheEvict(value= "employeeCache", allEntries= true)
  22. @Override
  23. public boolean updateEmployee(CustomerVO customer) {
  24.  
  25. Customer temp = new Customer();
  26. temp.setName(customer.getName());
  27. temp.setAddress(customer.getAddress());
  28. temp.setId(Long.parseLong(customer.getId()));
  29.  
  30. Customer cust = customerRepository.saveAndFlush(temp);
  31. if(cust == null) {
  32. return false;
  33. }
  34. return true;
  35. }
  36.  
  37. @Cacheable(value= "employeeCache", key= "#customerId")
  38. @Override
  39. public Customer getEmployee(String customerId) {
  40. Optional<Customer> cust = customerRepository.findById(Long.parseLong(customerId));
  41. if(cust.isPresent()) {
  42. return cust.get();
  43. }
  44. return null;
  45. }
  46.  
  47. @Cacheable(value= "employeeCache", unless= "#result.size() == 0")
  48. @Override
  49. public List<Customer> getAllEmployee() {
  50. return customerRepository.findAll();
  51. }
  52.  
  53. }

Chú ý rằng class Customer phải implement interface Serializable:
  1. @Entity
  2. @Table(name="customer")
  3. @EntityListeners(AuditingEntityListener.class)
  4. @JsonIgnoreProperties(allowGetters = true, ignoreUnknown = true)
  5. public class Customer implements Serializable {
  6.  
  7. private static final long serialVersionUID = 1L;
  8.  
  9. @Id()
  10. @GeneratedValue(strategy=GenerationType.IDENTITY)
  11. @Column(name="id")
  12. private long id;
  13.  
  14. @Column(name="name")
  15. private String name;
  16.  
  17. @Column(name="address")
  18. private String address;
  19.  
  20.  
  21. public long getId() {
  22. return id;
  23. }
  24. public void setId(long id) {
  25. this.id = id;
  26. }
  27. public String getName() {
  28. return name;
  29. }
  30. public void setName(String name) {
  31. this.name = name;
  32. }
  33. public String getAddress() {
  34. return address;
  35. }
  36. public void setAddress(String address) {
  37. this.address = address;
  38. }
  39.  
  40. }
Full source code tại đây: https://github.com/WeLook-team/nguyen-giang-tip.blogspot.com/tree/main/Redis/redis-sentinel-master



No comments:

Post a Comment

So sánh các GitFlow model và áp dụng với CICD

https://medium.com/oho-software/so-s%C3%A1nh-c%C3%A1c-gitflow-model-v%C3%A0-%C3%A1p-d%E1%BB%A5ng-v%E1%BB%9Bi-cicd-b6581cfc893a