Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add spring cloud feign plugin #249

Merged
merged 2 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions chaosblade-exec-plugin/chaosblade-exec-plugin-feign/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>chaosblade-exec-plugin</artifactId>
<groupId>com.alibaba.chaosblade</groupId>
<version>1.6.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>chaosblade-exec-plugin-feign</artifactId>
<name>chaosblade-exec-plugin-feign</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.alibaba.chaosblade.exec.plugin.feign;

/**
* @author guoyu486@gmail.com
*/
public interface FeignConstant {

String TARGET_NAME = "feign";

String ENHANCER_CLASS = "feign.SynchronousMethodHandler";

String METHOD = "executeAndDecode";

String SERVICE_NAME = "service-name";

String TEMPLATE_URL = "url";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.chaosblade.exec.plugin.feign;

import com.alibaba.chaosblade.exec.common.aop.Enhancer;
import com.alibaba.chaosblade.exec.common.aop.Plugin;
import com.alibaba.chaosblade.exec.common.aop.PointCut;
import com.alibaba.chaosblade.exec.common.model.ModelSpec;
import com.alibaba.chaosblade.exec.plugin.feign.model.FeignModelSpec;

/**
* @author guoyu486@gmail.com
*/
public class FeignPlugin implements Plugin, FeignConstant {

@Override
public ModelSpec getModelSpec() {
return new FeignModelSpec();
}

@Override
public String getName() {
return "feign";
}

@Override
public PointCut getPointCut() {
return new FeignProducerPointCut();
}

@Override
public Enhancer getEnhancer() {
return new FeignProducerEnhancer();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.alibaba.chaosblade.exec.plugin.feign;

import java.lang.reflect.Method;

import com.alibaba.chaosblade.exec.common.aop.BeforeEnhancer;
import com.alibaba.chaosblade.exec.common.aop.EnhancerModel;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.util.ReflectUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author guoyu486@gmail.com
*/
public class FeignProducerEnhancer extends BeforeEnhancer implements FeignConstant {

private static final Logger LOGGER = LoggerFactory.getLogger(FeignProducerEnhancer.class);

@Override
public EnhancerModel doBeforeAdvice(ClassLoader classLoader, String className, Object object, Method method,
Object[] methodArguments) throws Exception {

if (methodArguments == null || object == null) {
LOGGER.warn("The necessary parameter is null.");
return null;
}
String serviceName = getServiceName(object);
String template = getTemplate(methodArguments);
if (LOGGER.isDebugEnabled()) {
LOGGER.info("feign : serviceName:{},template:{}", serviceName, template);
}
MatcherModel matcherModel = new MatcherModel();
matcherModel.add(SERVICE_NAME, serviceName);
matcherModel.add(TEMPLATE_URL, template);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("feign template url: {}", template);
}
return new EnhancerModel(classLoader, matcherModel);
}

private String getServiceName(Object curObject) throws Exception {
// test in open-feign 3.1.1,2.1.1.RELEASE
Object feignTargetObject = ReflectUtil.getFieldValue(curObject, "target", false);
return ReflectUtil.invokeMethod(feignTargetObject, "name");
}

private String getTemplate(Object[] methodArguments) throws Exception {
Object requestTemplateObj = methodArguments[0];
Object uriTemplateObj = ReflectUtil.getFieldValue(requestTemplateObj, "uriTemplate", false);
return ReflectUtil.getSuperclassFieldValue(uriTemplateObj, "template", false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.alibaba.chaosblade.exec.plugin.feign;

import com.alibaba.chaosblade.exec.common.aop.PointCut;
import com.alibaba.chaosblade.exec.common.aop.matcher.MethodInfo;
import com.alibaba.chaosblade.exec.common.aop.matcher.clazz.ClassMatcher;
import com.alibaba.chaosblade.exec.common.aop.matcher.clazz.NameClassMatcher;
import com.alibaba.chaosblade.exec.common.aop.matcher.method.MethodMatcher;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author guoyu486@gmail.com
*/
public class FeignProducerPointCut implements PointCut, FeignConstant {

private static final Logger LOGGER = LoggerFactory.getLogger(FeignProducerPointCut.class);

@Override
public ClassMatcher getClassMatcher() {
return new NameClassMatcher(ENHANCER_CLASS);
}

@Override
public MethodMatcher getMethodMatcher() {
return new MethodMatcher() {
@Override
public boolean isMatched(String methodName, MethodInfo methodInfo) {
return methodName.equals(METHOD);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.alibaba.chaosblade.exec.plugin.feign.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.alibaba.chaosblade.exec.common.aop.PredicateResult;
import com.alibaba.chaosblade.exec.common.model.FrameworkModelSpec;
import com.alibaba.chaosblade.exec.common.model.Model;
import com.alibaba.chaosblade.exec.common.model.action.ActionSpec;
import com.alibaba.chaosblade.exec.common.model.action.delay.DelayActionSpec;
import com.alibaba.chaosblade.exec.common.model.action.exception.ThrowCustomExceptionActionSpec;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherModel;
import com.alibaba.chaosblade.exec.common.model.matcher.MatcherSpec;
import com.alibaba.chaosblade.exec.plugin.feign.FeignConstant;

/**
* @author guoyu486@gmail.com
*/
public class FeignModelSpec extends FrameworkModelSpec implements FeignConstant {

public FeignModelSpec() {
addActionExample();
}

private void addActionExample() {
List<ActionSpec> actions = getActions();
for (ActionSpec action : actions) {
if (action instanceof DelayActionSpec) {
action.setLongDesc("feign delay experiment");
action.setExample("# Delay when service call api time\n" +
"blade create feign delay --time 3000 --service-name test-service --url /example/feign/api ");
} else if (action instanceof ThrowCustomExceptionActionSpec) {
action.setLongDesc("Feign throws custom exception experiment");
action.setExample("# Throw exception when service call api \n" +
"blade create feign throwCustomException --exception java.lang.RuntimeException --exception-message "
+ "mock-beans-exception --service-name test-service --url /example/feign/api");
}
}
}


@Override
protected List<MatcherSpec> createNewMatcherSpecs() {
ArrayList<MatcherSpec> arrayList = new ArrayList<MatcherSpec>();
arrayList.add(new ServiceNameMatcherSpec());
arrayList.add(new UrlMatcherSpec());
return arrayList;
}

@Override
public String getTarget() {
return TARGET_NAME;
}

@Override
public String getShortDesc() {
return "feign experiment";
}

@Override
public String getLongDesc() {
return "feign experiment for testing service api delay and exception.";
}

@Override
protected PredicateResult preMatcherPredicate(Model model) {
if (model == null) {
return PredicateResult.fail("matcher not found for feign");
}
MatcherModel matcher = model.getMatcher();
Set<String> keySet = matcher.getMatchers().keySet();
if (keySet.contains(SERVICE_NAME) || keySet.contains(TEMPLATE_URL)) {
return PredicateResult.success();
} else {
return PredicateResult.fail("feign chaos require param [serviceName,url] ");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.alibaba.chaosblade.exec.plugin.feign.model;

import com.alibaba.chaosblade.exec.common.model.matcher.BasePredicateMatcherSpec;
import com.alibaba.chaosblade.exec.plugin.feign.FeignConstant;

/**
* @author guoyu486@gmail.com
*/
public class ServiceNameMatcherSpec extends BasePredicateMatcherSpec implements FeignConstant {
@Override
public String getName() {
return SERVICE_NAME;
}

@Override
public String getDesc() {
return "the feign api service name";
}

@Override
public boolean noArgs() {
return false;
}

@Override
public boolean required() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.alibaba.chaosblade.exec.plugin.feign.model;

import com.alibaba.chaosblade.exec.common.model.matcher.BasePredicateMatcherSpec;
import com.alibaba.chaosblade.exec.plugin.feign.FeignConstant;

/**
* @author guoyu486@gmail.com
*/
public class UrlMatcherSpec extends BasePredicateMatcherSpec implements FeignConstant {
@Override
public String getName() {
return TEMPLATE_URL;
}

@Override
public String getDesc() {
return "the feign api url";
}

@Override
public boolean noArgs() {
return false;
}

@Override
public boolean required() {
return true;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 1999-2019 Alibaba Group Holding Ltd.
#
# 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.
#

com.alibaba.chaosblade.exec.plugin.feign.FeignPlugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.alibaba.chaosblade.exec.plugin.feign;

import junit.framework.TestCase;

/**
* @author hengyu
* @version 1.0.0
* @className FeignProducerEnhancerTest
* @createTime 2022/4/21 14:46:00
* @description
*/
public class FeignProducerEnhancerTest extends TestCase {

}
1 change: 1 addition & 0 deletions chaosblade-exec-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<module>chaosblade-exec-plugin-kafka</module>
<module>chaosblade-exec-plugin-gateway</module>
<module>chaosblade-exec-plugin-log</module>
<module>chaosblade-exec-plugin-feign</module>
</modules>

<dependencies>
Expand Down