目录
1、Spring Cloud crm开发定制工程中配置断路器Hystrix
在Spring Cloud Feign工程中配置断路器的时候,pom.xml文件已经加入Hystrix相关的jar:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
Application.java:
@SpringBootApplication@EnableDiscoveryClient@EnableFeignClients@EnableHystrix@EnableHystrixDashboardpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
2、当启动的时候,报如下错误 NoClassDefFoundError:
Exception in thread "main" java.lang.NoClassDefFoundError: com/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at java.lang.Class.getDeclaredMethods(Class.java:1975) at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:613) at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:524) at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:510) at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:570) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:697) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:640) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:609) at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1484) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:425) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:395) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:515) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:508) at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1186) at org.springframework.boot.SpringApplication.getExitCodeFromMappedException(SpringApplication.java:818) at org.springframework.boot.SpringApplication.getExitCodeFromException(SpringApplication.java:804) at org.springframework.boot.SpringApplication.handleExitCode(SpringApplication.java:790) at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:744) at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
可以看出来是找不到HystrixCommandAspec.java这个类,于是在上找到这个源文件:
3、查看源码(切记:一定要学会查看源码)
源码如下:
/** * Copyright 2012 Netflix, Inc. * <p/> * 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 * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * 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.aop.aspectj;import com.google.common.base.Throwables;import com.google.common.collect.ImmutableMap;import com.netflix.hystrix.HystrixExecutable;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import com.netflix.hystrix.contrib.javanica.collapser.CommandCollapser;import com.netflix.hystrix.contrib.javanica.command.CommandExecutor;import com.netflix.hystrix.contrib.javanica.command.ExecutionType;import com.netflix.hystrix.contrib.javanica.command.GenericHystrixCommandFactory;import com.netflix.hystrix.contrib.javanica.command.MetaHolder;import com.netflix.hystrix.exception.HystrixBadRequestException;import org.apache.commons.lang3.Validate;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import java.lang.reflect.Method;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.util.List;import java.util.Map;import java.util.concurrent.Future;import static com.netflix.hystrix.contrib.javanica.utils.AopUtils.getDeclaredMethod;import static com.netflix.hystrix.contrib.javanica.utils.AopUtils.getMethodFromTarget;import static com.netflix.hystrix.contrib.javanica.utils.EnvUtils.isCompileWeaving;import static com.netflix.hystrix.contrib.javanica.utils.ajc.AjcUtils.getAjcMethodAroundAdvice;/** * AspectJ aspect to process methods which annotated with {@link HystrixCommand} annotation. */@Aspectpublic class HystrixCommandAspect { private static final Map<HystrixPointcutType, MetaHolderFactory> META_HOLDER_FACTORY_MAP; static { META_HOLDER_FACTORY_MAP = ImmutableMap.<HystrixPointcutType, MetaHolderFactory>builder() .put(HystrixPointcutType.COMMAND, new CommandMetaHolderFactory()) .put(HystrixPointcutType.COLLAPSER, new CollapserMetaHolderFactory()) .build(); } @Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)") public void hystrixCommandAnnotationPointcut() { } @Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser)") public void hystrixCollapserAnnotationPointcut() { } @Around("hystrixCommandAnnotationPointcut() || hystrixCollapserAnnotationPointcut()") public Object methodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinPoint) throws Throwable { Method method = getMethodFromTarget(joinPoint); Validate.notNull(method, "failed to get method from joinPoint: %s", joinPoint); if (method.isAnnotationPresent(HystrixCommand.class) && method.isAnnotationPresent(HystrixCollapser.class)) { throw new IllegalStateException("method cannot be annotated with HystrixCommand and HystrixCollapser " + "annotations at the same time"); } MetaHolderFactory metaHolderFactory = META_HOLDER_FACTORY_MAP.get(HystrixPointcutType.of(method)); MetaHolder metaHolder = metaHolderFactory.create(joinPoint); HystrixExecutable executable; ExecutionType executionType = metaHolder.isCollapser() ? metaHolder.getCollapserExecutionType() : metaHolder.getExecutionType(); if (metaHolder.isCollapser()) { executable = new CommandCollapser(metaHolder); } else { executable = GenericHystrixCommandFactory.getInstance().create(metaHolder, null); } Object result; try { result = CommandExecutor.execute(executable, executionType); } catch (HystrixBadRequestException e) { throw e.getCause(); } return result; } /** * A factory to create MetaHolder depending on {@link HystrixPointcutType}. */ private static abstract class MetaHolderFactory { public MetaHolder create(final ProceedingJoinPoint joinPoint) { Method method = getMethodFromTarget(joinPoint); Object obj = joinPoint.getTarget(); Object[] args = joinPoint.getArgs(); Object proxy = joinPoint.getThis(); return create(proxy, method, obj, args, joinPoint); } public abstract MetaHolder create(Object proxy, Method method, Object obj, Object[] args, final ProceedingJoinPoint joinPoint); MetaHolder.Builder metaHolderBuilder(Object proxy, Method method, Object obj, Object[] args, final ProceedingJoinPoint joinPoint) { MetaHolder.Builder builder = MetaHolder.builder() .args(args).method(method).obj(obj).proxyObj(proxy) .defaultGroupKey(obj.getClass().getSimpleName()) .joinPoint(joinPoint); if (isCompileWeaving()) { builder.ajcMethod(getAjcMethodFromTarget(joinPoint)); } return builder; } } private static class CollapserMetaHolderFactory extends MetaHolderFactory { @Override public MetaHolder create(Object proxy, Method collapserMethod, Object obj, Object[] args, final ProceedingJoinPoint joinPoint) { HystrixCollapser hystrixCollapser = collapserMethod.getAnnotation(HystrixCollapser.class); if (collapserMethod.getParameterTypes().length > 1 || collapserMethod.getParameterTypes().length == 0) { throw new IllegalStateException("Collapser method must have one argument: " + collapserMethod); } Method batchCommandMethod = getDeclaredMethod(obj.getClass(), hystrixCollapser.batchMethod(), List.class); if (batchCommandMethod == null || !batchCommandMethod.getReturnType().equals(List.class)) { throw new IllegalStateException("required batch method for collapser is absent: " + "(java.util.List) " + obj.getClass().getCanonicalName() + "." + hystrixCollapser.batchMethod() + "(java.util.List)"); } if (!collapserMethod.getParameterTypes()[0] .equals(getGenericParameter(batchCommandMethod.getGenericParameterTypes()[0]))) { throw new IllegalStateException("required batch method for collapser is absent, wrong generic type: expected" + obj.getClass().getCanonicalName() + "." + hystrixCollapser.batchMethod() + "(java.util.List<" + collapserMethod.getParameterTypes()[0] + ">), but it's " + getGenericParameter(batchCommandMethod.getGenericParameterTypes()[0])); } Class<?> collapserMethodReturnType; if (Future.class.isAssignableFrom(collapserMethod.getReturnType())) { collapserMethodReturnType = getGenericParameter(collapserMethod.getGenericReturnType()); } else { collapserMethodReturnType = collapserMethod.getReturnType(); } if (!collapserMethodReturnType .equals(getGenericParameter(batchCommandMethod.getGenericReturnType()))) { throw new IllegalStateException("Return type of batch method must be java.util.List parametrized with corresponding type: expected " + "(java.util.List<" + collapserMethodReturnType + ">)" + obj.getClass().getCanonicalName() + "." + hystrixCollapser.batchMethod() + "(java.util.List<" + collapserMethod.getParameterTypes()[0] + ">), but it's " + getGenericParameter(batchCommandMethod.getGenericReturnType())); } HystrixCommand hystrixCommand = batchCommandMethod.getAnnotation(HystrixCommand.class); if (hystrixCommand == null) { throw new IllegalStateException("batch method must be annotated with HystrixCommand annotation"); } // method of batch hystrix command must be passed to metaholder because basically collapser doesn't have any actions // that should be invoked upon intercepted method, its required only for underlying batch command MetaHolder.Builder builder = MetaHolder.builder() .args(args).method(batchCommandMethod).obj(obj).proxyObj(proxy) .defaultGroupKey(obj.getClass().getSimpleName()) .joinPoint(joinPoint); if (isCompileWeaving()) { builder.ajcMethod(getAjcMethodAroundAdvice(obj.getClass(), batchCommandMethod.getName(), List.class)); } builder.hystrixCollapser(hystrixCollapser); builder.defaultCollapserKey(collapserMethod.getName()); builder.collapserExecutionType(ExecutionType.getExecutionType(collapserMethod.getReturnType())); builder.defaultCommandKey(batchCommandMethod.getName()); builder.hystrixCommand(hystrixCommand); builder.executionType(ExecutionType.getExecutionType(batchCommandMethod.getReturnType())); return builder.build(); } } private static class CommandMetaHolderFactory extends MetaHolderFactory { @Override public MetaHolder create(Object proxy, Method method, Object obj, Object[] args, final ProceedingJoinPoint joinPoint) { HystrixCommand hystrixCommand = method.getAnnotation(HystrixCommand.class); MetaHolder.Builder builder = metaHolderBuilder(proxy, method, obj, args, joinPoint); builder.defaultCommandKey(method.getName()); builder.hystrixCommand(hystrixCommand); builder.executionType(ExecutionType.getExecutionType(method.getReturnType())); return builder.build(); } } private static enum HystrixPointcutType { COMMAND, COLLAPSER; static HystrixPointcutType of(Method method) { return method.isAnnotationPresent(HystrixCommand.class) ? COMMAND : COLLAPSER; } } private static Method getAjcMethodFromTarget(JoinPoint joinPoint) { return getAjcMethodAroundAdvice(joinPoint.getTarget().getClass(), (MethodSignature) joinPoint.getSignature()); } private static Class<?> getGenericParameter(Type type) { Type tType = ((ParameterizedType) type).getActualTypeArguments()[0]; String className = tType.toString().split(" ")[1]; try { return Class.forName(className); } catch (ClassNotFoundException e) { throw Throwables.propagate(e); } }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
https://github.com/dmgcodevil/Hystrix/tree/958ec5d7b4bb967be077a4c2bbcdc71e7a7f5248/hystrix-contrib/hystrix-javanica
4、通过查看源码找到解决方案
上面这个图片看到:
在pom.文件中加入:
<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-javanica --> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> <version>1.5.12</version> </dependency>
- 1
- 2
- 3
- 4
- 5
- 6
问题解决OK!
更多系列文章推荐: