zoukankan      html  css  js  c++  java
  • spring cloud 实践之hystrix注意事项

    当我们写类似下面代码时

    package demo1.demo1;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
    
    @RestController
    public class TestRest {
    
        protected final static Logger logger = LoggerFactory.getLogger(TestRest.class);
        @HystrixCommand(commandProperties = {
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "20000") }, threadPoolProperties = {
                        @HystrixProperty(name = "coreSize", value = "64") }, threadPoolKey = "test1")
        @GetMapping("/testdemo1")
        public long getStringtest2() {
            logger.info("我收到了其他服务调用");
            // 返回当前时间毫秒 来发现访问变化
            
            return System.currentTimeMillis();
        }
        
    
    }

    如果方法之间有嵌套(注意不要像下面那样写

    package demo1.demo1;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
    
    @RestController
    public class TestRest {
    
        protected final static Logger logger = LoggerFactory.getLogger(TestRest.class);
        @HystrixCommand(commandProperties = {
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "20000") }, threadPoolProperties = {
                        @HystrixProperty(name = "coreSize", value = "64") }, threadPoolKey = "test1")
        @GetMapping("/testdemo1")
        public long getStringtest2() {
            logger.info("我收到了其他服务调用");
            // 返回当前时间毫秒 来发现访问变化
            
            //报个异常试试
            //int a=1/0;
            test();
            
            return System.currentTimeMillis();
        }
        @HystrixCommand(commandProperties = {
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "20000") }, threadPoolProperties = {
                        @HystrixProperty(name = "coreSize", value = "64") }, threadPoolKey = "test1")
        private void test(){
            //code
            
        }
    
    }

    因为使用了@HystrixCommand注解之后方法会放到隔离线程池中执行已经不再是tomcat或jetty等容器创建的web http线程池执行方法了,如果有两个可能会占用两个被隔离的线程执行方法。

     

  • 相关阅读:
    面向对象设计技巧[Object Oriented Design Tips] 2
    面向对象设计的技巧[Object Oriented Design Tips]1
    36家示范性软件学院验收的得分排名顺序
    解决windows系统乱码(其实是法语)
    [maven] maven/appfuse 常用命令介绍
    [plsql] win7/64位 PL/SQL登录时报 ora12154无法解析指定的连接标识
    [maven] pom.xml常用配置介绍
    web.xml中classpath:和classpath*:的区别
    [http] 深入理解HTTP消息头
    [Hibernate] Hibernate连接mysql示范
  • 原文地址:https://www.cnblogs.com/zhyg/p/9482252.html
Copyright © 2011-2022 走看看