84-springboot EhCache 集群(一),rmi 手动发现

ehcache 集群配置:
server1(192.168.1.6):




    
    
    

    


    
    

    
    
        
        

    

server2(192.168.1.5):




    
    
    

    


    
    

    
    
        
        

    

application.yml

server:
  port: 8082
  servlet:
    context-path: /

spring:
  devtools:
    restart:
      enabled: false

  cache:
    jcache:
      config: ehcache.xml

测试控制类:

package com.example.demo1125;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/dcs/ec")
public class EhcacheDcsController {


    private static final Logger logger = LoggerFactory.getLogger(EhcacheDcsController.class);


    @RequestMapping(value = "save")
    @Cacheable(value="dcsCache",key="#value")
    @ResponseBody
    public Object save(String value) {
        logger.info("logger:/dcs/ec/save...."+value);
        System.out.println("/dcs/ec/save...."+value);
        return value;
    }

    @RequestMapping(value="del")
    @CacheEvict(value="dcsCache",key="#value",beforeInvocation=true)
    @ResponseBody
    public String del(String  value){
        logger.info("logger:delete cache key...."+value);
        System.out.println("delete cache key:"+value);
        return "delete cache key:"+value;
    }

}

Demo1125Application:

package com.example.demo1125;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableCaching
@EnableScheduling
@SpringBootApplication
public class Demo1125Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo1125Application.class, args);
    }

}

pom.xml:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.0
         
    
    com.example
    demo1125
    0.0.1-SNAPSHOT
    demo1125
    Demo project for Spring Boot

    
        1.8
        1.8
        UTF-8
    

    
        
            org.springframework.boot
            spring-boot-starter-cache
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            net.sf.ehcache
            ehcache
            2.10.3
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


访问: http://192.168.1.5:8082/dcs/e...
然后再访问 http://192.168.1.6:8082/dcs/e...
会发现, 192.168.1.5 的有输出日志
192.168.1.6 的没有输出日志

目前在windows 测试通过。
在同一台电脑上,改变端口也测试通过。

测试不通过的情况:
1)一个部署在windows, 一个部署在 linux, windows的缓存可以同步到linux, linux上的缓存无法同步到 windows。

2) 两个都分别部署在两台 linux 上, 相互之间无法同步。

以上2点, 均在局域网, ping ip 是互通的。

你可能感兴趣的:(ehcache)