【springcloud】hystrixCommand 注解

/**
 * Copyright 2012 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.netflix.hystrix.contrib.javanica.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/**
 * This annotation used to specify some methods which should be processes as hystrix commands.
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface HystrixCommand {

    /**
     * The command group key is used for grouping together commands such as for reporting,
     * alerting, dashboards or team/library ownership.
     * <p/>
     * default => the runtime class name of annotated method
     *
     * @return group key
     */
    String groupKey() default "";

    /**
     * Hystrix command key.
     * <p/>
     * default => the name of annotated method. for example:
     * <code>
     *     ...
     *     @HystrixCommand
     *     public User getUserById(...)
     *     ...
     *     the command name will be: 'getUserById'
     * </code>
     *
     * @return command key
     */
    String commandKey() default "";

    /**
     * The thread-pool key is used to represent a
     * HystrixThreadPool for monitoring, metrics publishing, caching and other such uses.
     *
     * @return thread pool key
     */
    String threadPoolKey() default "";

    /**
     * Specifies a method to process fallback logic.
     * A fallback method should be defined in the same class where is HystrixCommand.
     * Also a fallback method should have same signature to a method which was invoked as hystrix command.
     * for example:
     * <code>
     *      @HystrixCommand(fallbackMethod = "getByIdFallback")
     *      public String getById(String id) {...}
     *
     *      private String getByIdFallback(String id) {...}
     * </code>
     * Also a fallback method can be annotated with {@link HystrixCommand}
     * <p/>
     * default => see {@link com.netflix.hystrix.contrib.javanica.command.GenericCommand#getFallback()}
     *
     * @return method name
     */
    String fallbackMethod() default "";

    /**
     * Specifies command properties.
     *
     * @return command properties
     */
    HystrixProperty[] commandProperties() default {};

    /**
     * Specifies thread pool properties.
     *
     * @return thread pool properties
     */
    HystrixProperty[] threadPoolProperties() default {};

    /**
     * Defines exceptions which should be ignored.
     * Optionally these can be wrapped in HystrixRuntimeException if raiseHystrixExceptions contains RUNTIME_EXCEPTION.
     *
     * @return exceptions to ignore
     */
    Class<? extends Throwable>[] ignoreExceptions() default {};

    /**
     * Specifies the mode that should be used to execute hystrix observable command.
     * For more information see {@link ObservableExecutionMode}.
     *
     * @return observable execution mode
     */
    ObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER;

    /**
     * When includes RUNTIME_EXCEPTION, any exceptions that are not ignored are wrapped in HystrixRuntimeException.
     *
     * @return exceptions to wrap
     */
    HystrixException[] raiseHystrixExceptions() default {};

    /**
     * Specifies default fallback method for the command. If both {@link #fallbackMethod} and {@link #defaultFallback}
     * methods are specified then specific one is used.
     * note: default fallback method cannot have parameters, return type should be compatible with command return type.
     *
     * @return the name of default fallback method
     */
    String defaultFallback() default "";
}

 

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 30000
//上面的default是对没有在HystrixCommand中指定commandKey的所有方法有效,如下:
	@HystrixCommand
	@PostMapping("/v1/queryRmNoPfCUDirectoryByCondition") 
	public ResponseResult<List<RmNoPfCUDirectoryEntity>> queryRmNoPfCUDirectoryByCondition(@RequestBody RmNoPfCUDirectoryEntity rmNoPfCUDirectoryEntity) {

	}


//如果想对其他方法设置不同的超时时间,可以通过指定commandKey来区分,如下用test区分:

hystrix.command.test.execution.isolation.thread.timeoutInMilliseconds: 5000

	@HystrixCommand(commandKey = "test")
	@PostMapping("/v1/queryRmNoPfCUDirectoryByCondition") 
	public ResponseResult<List<RmNoPfCUDirectoryEntity>> queryRmNoPfCUDirectoryByCondition(@RequestBody RmNoPfCUDirectoryEntity rmNoPfCUDirectoryEntity) {

	}

 

其他参考:Spring cloud Hystrix的配置属性优先级和详解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Spring Cloud Hystrix的代码示例: 1. 添加Hystrix依赖 在aven项目中,需要在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.2.6.RELEASE</version> </dependency> ``` 2. 创建服务接口 创建服务接口,并在接口中定义需要调用的方法。 例如: ``` public interface OrderService { String getOrder(String orderId); } ``` 3. 创建服务实现类 创建服务实现类,并在实现类中实现服务接口中的方法。 例如: ``` @Service public class OrderServiceImpl implements OrderService { @Autowired private RemoteService remoteService; @Override @HystrixCommand(fallbackMethod = "fallbackGetOrder") public String getOrder(String orderId) { return remoteService.getOrder(orderId); } private String fallbackGetOrder(String orderId) { return "Failed to get order"; } } ``` 4. 配置Hystrix 可以使用@HystrixCommand注解配置Hystrix的参数,例如: ``` @HystrixCommand( fallbackMethod = "fallbackGetOrder", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000") } ) public String getOrder(String orderId) { return remoteService.getOrder(orderId); } ``` 5. 测试Hystrix 可以通过以下方式测试Hystrix: ``` @Autowired private OrderService orderService; @RequestMapping("/order/{orderId}") public String getOrder(@PathVariable String orderId) { return orderService.getOrder(orderId); } ``` 以上是Spring Cloud Hystrix的简单使用示例,更多内容可以参考官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值