Spring cloud hystrix缓存使用

一 新建过滤器,初始化HystrixRequestContext

package org.crazyit.cloud.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

@WebFilter(urlPatterns = "/*", filterName = "hystrixFilter")
public class MyFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
        
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HystrixRequestContext ctx = HystrixRequestContext.initializeContext();
        try {
            chain.doFilter(request, response);
        } catch (Exception e) {
            
        } finally {
            ctx.shutdown();
        }
    }

    public void destroy() {
        
    }

}

二 启动类中加上@ServletComponentScan注解,使得过滤器生效

package org.crazyit.cloud;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@ServletComponentScan
public class SaleApp {
    
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
    
    public static void main(String[] args) {
        new SpringApplicationBuilder(SaleApp.class).web(true).run(args);
    }

}

三 新建缓存控制器

package org.crazyit.cloud.cache;

import org.crazyit.cloud.Member;
import org.crazyit.cloud.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CacheController {
    
    @Autowired
    private CacheService cacheService;

    @RequestMapping(value = "/cache", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public String cache() {
        for(int i = 0; i < 3; i++) {
            cacheService.cacheMember(1);
        }
        return "";
    }
    
    @RequestMapping(value = "/rc", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public String testRemoveCache() {
        cacheService.getCache(1);
        cacheService.getCache(1);
        
        cacheService.removeCache(1);
        System.out.println("#########  分隔线   ###########");
        cacheService.getCache(1);
        return "";
    }
}

四 新建缓存服务

package org.crazyit.cloud.cache;

import org.crazyit.cloud.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;

@Service
public class CacheService {

    @Autowired
    private RestTemplate restTpl;
    
    @CacheResult
    @HystrixCommand
    public Member cacheMember(Integer id) {
        System.out.println("调用 cacheMember 方法");
        return null;
    }
    
    @CacheResult
    @HystrixCommand(commandKey = "cacheKey")
    public String getCache(Integer id) {
        System.out.println("执行查询方法");
        return null;
    }
    
    @CacheRemove(commandKey = "cacheKey")
    @HystrixCommand
    public void removeCache(Integer id) {
        System.out.println("删除缓存方法");
    }
}

五 启动服务

六 测试缓存是否生效

调用 cacheMember 方法

只输出一次,说明缓存生效

七 测试删除缓存

执行查询方法

删除缓存方法

#########  分隔线   ###########

执行查询方法

测试结果符合预期,说明删除缓存成功

八 缓存注解

Spring cloud hystrix缓存使用_第1张图片

你可能感兴趣的:(微服务)