-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
221 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package feign; | ||
|
||
import com.alibaba.arthas.deps.org.slf4j.Logger; | ||
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory; | ||
import com.cxytiandi.foxmock.agent.factory.MockInfoFactory; | ||
import com.cxytiandi.foxmock.agent.model.MockInfo; | ||
import com.cxytiandi.foxmock.agent.storage.StorageHelper; | ||
import com.cxytiandi.foxmock.agent.transformer.MethodInvokeFilter; | ||
import com.cxytiandi.foxmock.agent.utils.JsonUtils; | ||
import com.cxytiandi.foxmock.agent.utils.ReflectionUtils; | ||
import java.lang.reflect.Type; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @作者 尹吉欢 | ||
* @个人微信 jihuan900 | ||
* @微信公众号 猿天地 | ||
* @GitHub https://github.com/yinjihuan | ||
* @作者介绍 http://cxytiandi.com/about | ||
* @时间 2022-05-24 23:07 | ||
*/ | ||
public class FeignInvokeFilter { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(FeignInvokeFilter.class); | ||
|
||
public static Object invoke(Object[] argsArray, Object methodHandler) throws Throwable { | ||
Object[] args = (Object[]) argsArray[0]; | ||
SynchronousMethodHandler handler = (SynchronousMethodHandler) methodHandler; | ||
Class<? extends SynchronousMethodHandler> handlerClass = handler.getClass(); | ||
|
||
MethodMetadata methodMetadata = (MethodMetadata) ReflectionUtils.getFieldValue("metadata", handlerClass, handler); | ||
Target<?> target = (Target<?>) ReflectionUtils.getFieldValue("target", handlerClass, handler);; | ||
|
||
String className = target.type().getName(); | ||
String ms = methodMetadata.configKey().split("#")[1]; | ||
String methodName = ms.substring(0, ms.indexOf("(")); | ||
|
||
String key = String.format("%s#%s", className, methodName); | ||
String data = StorageHelper.get(key); | ||
|
||
if (Objects.nonNull(data)) { | ||
MockInfo mockInfo = MockInfoFactory.create(data); | ||
boolean filter = MethodInvokeFilter.filter(args, mockInfo.getOgnlExpress()); | ||
if (filter) { | ||
LOG.info(String.format("mock methods %s, mock data is %s", key, data)); | ||
Type genericReturnType = methodMetadata.returnType(); | ||
Object value = JsonUtils.parseByType(mockInfo.getMockData(), genericReturnType); | ||
return value; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/feign/FeignApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.cxytiandi.foxmock.example.feign; | ||
|
||
import com.cxytiandi.foxmock.example.Result; | ||
import com.cxytiandi.foxmock.example.UserDetail; | ||
import com.cxytiandi.foxmock.example.UserInfo; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
/** | ||
* @作者 尹吉欢 | ||
* @个人微信 jihuan900 | ||
* @微信公众号 猿天地 | ||
* @GitHub https://github.com/yinjihuan | ||
* @作者介绍 http://cxytiandi.com/about | ||
* @时间 2022-05-24 22:59 | ||
*/ | ||
@FeignClient(name = "fox-mock-example", url = "http://localhost:8080") | ||
public interface FeignApi { | ||
|
||
@RequestMapping(value = "/getUserInfo", method = RequestMethod.GET) | ||
UserInfo getUserInfo(@RequestParam("id")Long id); | ||
|
||
@RequestMapping(value = "/getUserDetail", method = RequestMethod.GET) | ||
Result<UserDetail> getUserDetail(); | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/feign/FeignApiImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.cxytiandi.foxmock.example.feign; | ||
|
||
import com.cxytiandi.foxmock.example.Result; | ||
import com.cxytiandi.foxmock.example.UserDetail; | ||
import com.cxytiandi.foxmock.example.UserInfo; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
/** | ||
* @作者 尹吉欢 | ||
* @个人微信 jihuan900 | ||
* @微信公众号 猿天地 | ||
* @GitHub https://github.com/yinjihuan | ||
* @作者介绍 http://cxytiandi.com/about | ||
* @时间 2022-05-24 22:59 | ||
*/ | ||
@RestController | ||
public class FeignApiImpl { | ||
|
||
@RequestMapping(value = "/getUserInfo", method = RequestMethod.GET) | ||
public UserInfo getUserInfo(@RequestParam("id")Long id) { | ||
return new UserInfo(); | ||
} | ||
|
||
@RequestMapping(value = "/getUserDetail", method = RequestMethod.GET) | ||
public Result<UserDetail> getUserDetail() { | ||
return new Result<>(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
fox-mock-example/src/main/java/com/cxytiandi/foxmock/example/feign/FeignApiTestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.cxytiandi.foxmock.example.feign; | ||
|
||
import com.cxytiandi.foxmock.example.Result; | ||
import com.cxytiandi.foxmock.example.UserDetail; | ||
import com.cxytiandi.foxmock.example.UserInfo; | ||
import com.cxytiandi.foxmock.example.dubbo.DubboApi; | ||
import org.apache.dubbo.config.annotation.Reference; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
/** | ||
* @作者 尹吉欢 | ||
* @个人微信 jihuan900 | ||
* @微信公众号 猿天地 | ||
* @GitHub https://github.com/yinjihuan | ||
* @作者介绍 http://cxytiandi.com/about | ||
* @时间 2022-05-18 23:00 | ||
*/ | ||
@Service | ||
public class FeignApiTestService { | ||
|
||
@Autowired | ||
private FeignApi feignApi; | ||
|
||
public UserInfo getUserInfo(Long id) { | ||
return feignApi.getUserInfo(id); | ||
} | ||
|
||
public Result<UserDetail> getUserDetail() { | ||
return feignApi.getUserDetail(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters