我有两个微服务,例如 A 和 B。微服务 B 的其余端点必须只能从微服务 A 访问。如何限制微服务之间的访问?如果可能,最佳实践是什么?
我正在使用 spring 云安全(oauth2,jwt)。
spring-security microservices spring-cloud-feign spring-cloud-security
使用spring cloud feign调用我的服务,当服务返回401异常时,respose.body()为null。
当我抛出异常时,throw new BadRequestException(400\xef\xbc\x8c \xe2\x80\x9cthis http code is 400\xe2\x80\x9d);我可以得到错误消息,this http code is 400通过response.body()。\n但是当我抛出异常时throw new BadRequestException(401\xef\xbc\x8c \xe2\x80\x9cthis http code is 401\xe2\x80\x9d);,response.body()为空。\n这个响应是feign.Response。
为什么当 http 代码为 401 时我无法收到此错误消息?
\n\n希望您的帮助!非常感谢!
\n我有一个 Spring 启动应用程序,它使用 Feign 通过 Eureka 调用外部 Web 服务。我希望能够使用 Feign 接口的模拟实现来运行应用程序,这样我就可以在本地运行应用程序,而不必运行 Eureka 或外部 Web 服务。我曾想过定义一个允许我这样做的运行配置,但我正在努力让它工作。问题是,无论我尝试什么,Spring 的“魔法”都会为 Feign 接口定义一个 bean。
Feign 接口
@FeignClient(name = "http://foo-service")
public interface FooResource {
@RequestMapping(value = "/doSomething", method = GET)
String getResponse();
}
Run Code Online (Sandbox Code Playgroud)
服务
public class MyService {
private FooResource fooResource;
...
public void getFoo() {
String response = this.fooResource.getResponse();
...
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试添加一个配置类,如果 Spring 配置文件是“本地”,则该配置类有条件地注册了一个 bean,但是当我使用该 Spring 配置文件运行应用程序时,它从未被调用过:
@Configuration
public class AppConfig {
@Bean
@ConditionalOnProperty(prefix = "spring.profile", name = "active", havingValue="local")
public FooResource fooResource() { …Run Code Online (Sandbox Code Playgroud) spring-boot netflix-feign spring-cloud-feign spring-cloud-netflix
在 Feign 上配置代理路由规划器
\n\n我需要知道如何使用 Spring Boot REST 客户端在代理服务器后面发出请求。我可以使用 apache commons 对于 REST 请求进行此配置,如下所示:
\n\n执行POST的方法:
\n\nMap <String, Object> map = new TreeMap <String, Object> ();\n\nDynamicProxyRoutePlanner routePlanner = getProxy (param);\n\nCloseableHttpClient closeableHttpClient = HttpClients.custom (). SetRoutePlanner (routePlanner) .build ();\nRun Code Online (Sandbox Code Playgroud)\n\n获取代理方法:
\n\nprivate DynamicProxyRoutePlanner getProxy (Map <Integer, Object> param) {\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0HttpHost proxy = new HttpHost ((String) param.get (PROXY), (Integer) param.get (PROXY_PORT));\n\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0DynamicProxyRoutePlanner routePlanner = new DynamicProxyRoutePlanner (proxy);\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0return routePlanner;\n}\nRun Code Online (Sandbox Code Playgroud)\n\nDynamicProxyRoutePlanner 类:
\n\npublic class DynamicProxyRoutePlanner implements HttpRoutePlanner {\n\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0private DefaultProxyRoutePlanner defaultProxyRoutePlanner = null;\n\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0public …Run Code Online (Sandbox Code Playgroud) 我不知道我做错了什么,但每次 feign 客户端都会将声明为 get 的方法转换为 post 类型。
@FeignClient(name = "my-service", url = "http://localhost:8114", path = "service")
public interface MyServiceClient {
@RequestMapping(method = GET, value = "/clients")
Client getClients(@QueryMap MyPojo pojo);
}
@Getter
@Setter
public class MyPojo {
@NotNull
private String someValue;
@NotNull
private SomeEnum someEnum;
}
Run Code Online (Sandbox Code Playgroud)
此设置应解决此请求:
GET http://localhost:8114/service/clients?someValue=foo&someEnum=bar
但每次我得到这个结果:
{
"timestamp": 1542378765498,
"status": 405,
"error": "Method Not Allowed",
"exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
"message": "Request method 'POST' not supported",
"path": "/service/clients"
}
Run Code Online (Sandbox Code Playgroud)
然而,当我这样做时,效果很好:
@RequestMapping(method = GET, value = "/clients?someValue=foo&someEnum=bar")
Client getClients();
Run Code Online (Sandbox Code Playgroud)
我正在开发 …
我计划使用 spring webflux 将我的微服务从阻塞实现重构为反应式 API。我有几个疑问:
1)是否选择基于注解的控制器或功能路由器?2) 是否支持反应式假装客户端?
请帮忙。
spring microservices spring-cloud spring-cloud-feign spring-webflux
有什么区别:
当我尝试使用 openfeign 遵循教程时发现了这个问题@FeignClient(name=...)。不过我用的是feign,不是openfeign,这个注解也不一样。
这不是同一件事吗?
我一直在尝试为我的其余 api 公开一个 Feign Client。它采用 Pageable 作为输入并定义了 PageDefaults。
控制器:
@GetMapping(value = "data", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Get Data", nickname = "getData")
public Page<Data> getData(@PageableDefault(size = 10, page = 0) Pageable page,
@RequestParam(value = "search", required = false) String search) {
return service.getData(search, page);
}
Run Code Online (Sandbox Code Playgroud)
这是我的假客户:
@RequestMapping(method = RequestMethod.GET, value = "data")
public Page<Data> getData(@RequestParam(name = "pageable", required = false) Pageable page,
@RequestParam(name = "search", defaultValue = "null", required = false) String search);
Run Code Online (Sandbox Code Playgroud)
现在的问题是,无论我发送给 Feign Client 的页面大小和页码如何,它总是应用 PageDefaults (0,10)。 …
我正在尝试实现一个涉及 FeignClient 调用的单元测试,该调用应返回 404 Not Found。
由于 Feign 抛出了 404 异常,那么实现这个测试用例的正确方法是什么?
我正在尝试这样的事情......
when(mockedApiClient.userDataDelete(anyString()))
.thenThrow( ... );
Run Code Online (Sandbox Code Playgroud)
我应该扔什么?
我已经编写了一个FeignClient,我想使用单元测试来测试它是否有效。(就我而言,集成测试不是当前开发阶段的正确方法)。
在我的测试中,FeignClient 未初始化 ( null):NullPointerException运行测试时我收到一条消息。
如何成功自动装配 a FeignClient?
假冒客户:
package com.myapp.clients;
import com.myapp.model.StatusResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name="myClient", url="${feign.clients.my.url}")
public interface myClient {
@RequestMapping(method= RequestMethod.GET, value="/v1/users/{userId}")
StatusResponse getStatus(
@RequestHeader(value = "Auth", required = true) String authorizationHeader,
@RequestHeader(value = "my_tid", required = true) String tid,
@PathVariable("userId") String userId);
}
Run Code Online (Sandbox Code Playgroud)
测试:
package com.myapp.clients;
import com.intuit.secfraudshared.step.broker.model.StatusResponse;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class MyClientTest {
@Autowired
MyClient myClient; …Run Code Online (Sandbox Code Playgroud) feign ×4
spring ×4
java ×3
openfeign ×2
spring-cloud ×2
configure ×1
http-proxy ×1
rest ×1
spring-boot ×1
spring-data ×1