标签: spring-cloud

Spring Boot + Cloud | Zuul代理| 集成测试

使用Spring Boot构建微服务时,很容易编写广泛且易读的集成测试并模拟远程服务请求MockRestServiceServer.

有没有办法使用类似的方法来执行额外的集成测试ZuulProxy?我想要实现的是能够模拟远程服务器,ZuulProxy这些服务器将转发并验证我ZuulFitler的所有操作符合预期.但是,ZuulProxy是否正在使用RestClientNetflix(它似乎被弃用?),它自然不会使用RestTemplate哪些可以重新配置MockRestServiceServer,我目前无法找到一种很好的方法来模拟远程服务对代理请求的响应.

我有一个微服务,负责处理API会话密钥创建,然后将采取类似于API网关的行为.使用Zuul Proxy对底层公开的服务进行转发,Zuul Filters将检测Session密钥是否有效.因此,集成测试将创建有效会话,然后转发到虚假端点,例如"集成/测试".

通过设置配置属性@WebIntegrationTest,可以指定"集成/测试"是新端点,我可以成功模拟通过RestTemplate但不是Zuul转发处理的所有服务.

实现模拟远期目标服务的最佳方法是什么?

java integration-testing spring-boot spring-cloud netflix-zuul

10
推荐指数
1
解决办法
2576
查看次数

在Zuul代理后面的Spring重定向url问题

在过去的两天里,我一直试图找到一个奇怪的重定向问题,但没有成功.

基于spring-cloud示例项目,我已经配置了Eureka,Zuul以及在Zuul后面运行的基本服务.

我有以下方法;

@RequestMapping(method = RequestMethod.POST, value = "/register")
public String registerDevice(Principal principal, String response) {
  // ...
  return "redirect:/account";
}
Run Code Online (Sandbox Code Playgroud)

表单设置为发布到代理URL,如下所示;

POST https://localhost:8443/service/register
Run Code Online (Sandbox Code Playgroud)

(Zuul在localhost上运行:8443).

本地服务的URL(非代理)将是; HTTP://本地主机:9001 /寄存器

POST调用正确代理到上述方法,但是发送到浏览器的重定向位置是服务的非代理URL; HTTP://本地主机:9001 /帐户

Zuul代理肯定会发送正确的x-forwarded-*头文件,所以我希望Spring中的视图解析器能够根据x-forwarded值构建正确的重定向.

为了证明标头被正确发送,我重新配置了如下方法;

@RequestMapping(method = RequestMethod.POST, value = "/register")
public void registerDevice(Principal, String response, HttpServletResponse response) {
  // ...
  String rUrl = ServletUriComponentsBuilder.fromCurrentContextPath().path("/account").build().toUriString();
  servletResponse.sendRedirect(rUrl);
}
Run Code Online (Sandbox Code Playgroud)

这正确地将浏览器重定向到代理位置; https://开头本地主机:8443 /服务/帐户

这是一个错误,还是预期的行为?我认为使用"redirect:"是为了纪念从代理传递的前向头.

spring-mvc spring-boot spring-cloud netflix-eureka netflix-zuul

10
推荐指数
1
解决办法
5622
查看次数

netflix.feign和openfeign之间的差异

介绍

我最近使用netflix假装和功能区非常有用.

一个例子是:

@FeignClient(name = "ldap-proxy")
public interface LdapProxyClient  { 
    @RequestMapping(path = "/ldap-proxy/v1/users/{userNameOrEMail}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    LdapUser search(@PathVariable("userNameOrEMail") String userNameOrEMail);
}
Run Code Online (Sandbox Code Playgroud)

但是,在某些时候,我认为不必手动编写所有这些定义(对于现有的Web服务),我应该看看是否存在工具.

我偶然发现了https://github.com/swagger-api/swagger-codegen一些客户端生成的例子,例如https://github.com/swagger-api/swagger-codegen/tree/master/samples/client/petstore/java/feign.

但是,一旦我仔细查看了进口,我注意到以下情况:

import feign.Feign;

另一方面,Netflix的开源解决方案有包名: org.springframework.cloud.netflix.feign.

另外,我注意到两者都使用功能区(如果可用),但Netflix的表示法更加清晰,后台发生了很多事情.例如,@FeignClient注释类javadoc声明:

  • 接口的注释声明应该*创建具有该接口的REST客户端(例如,用于自动装配到另一个组件中).如果功能区可用,它将*用于对后端请求进行负载平衡,并且可以使用@RibbonClient与feign客户端具有相同名称(即值)的a来配置负载均衡器.

但是在Feign.feign文档中(https://github.com/OpenFeign/feign),我看到:

RibbonClient覆盖了Feign客户端的URL解析,添加了Ribbon提供的智能路由和弹性功能.

集成要求您将功能区客户端名称作为URL的主机部分传递,例如myAppProd.

> MyService api =
> Feign.builder().client(RibbonClient.create()).target(MyService.class,
> "https://myAppProd");
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:

  1. 两者之间的历史/关系和差异是什么?
  2. 各自的优点和缺点是什么?

它们是完全不同的项目,没有关系,或者netflix只是分叉/利用OpenFeign并将其修改为在其集成云解决方案中?从本质上讲,netflix是否只是从开源项目中获取并集成了Discovery,Ribbon和feign等不同技术?

java spring-cloud netflix-feign spring-cloud-feign feign

10
推荐指数
2
解决办法
8758
查看次数

写入时保险库错误

我想测试Spring Cloud Vault配置.

我在本地安装了一个Vault服务器,当我尝试write一些键值时它失败并要求我使用vault kv put命令.

虽然此链接中的Spring Cloud Config示例显示了Vault写入命令的用法

这是我得到的错误

$ vault write secret/my-app foo=bar
Error writing data to secret/my-app: Error making API request.

URL: PUT http://127.0.0.1:8200/v1/secret/my-app
Code: 404. Errors:


WARNING! The following warnings were returned from Vault:

  * Invalid path for a versioned K/V secrets engine. See the API docs for the
  appropriate API endpoints to use. If using the Vault CLI, use 'vault kv put'
  for this operation.
Run Code Online (Sandbox Code Playgroud)

spring spring-cloud hashicorp-vault spring-cloud-vault-config

10
推荐指数
2
解决办法
4482
查看次数

为什么 bootstrap.properties 被 spring-cloud-starter-config 忽略?

我的目标是world-serviceconfig-service.

架构:

  1. config-service依赖spring-cloud-config-serverlocalhost:8888
  2. world-service依赖于spring-webspring-cloud-starter-config

我做了什么:

  1. 我已经设置了配置服务器并向配置服务器发送了一个 GET 请求,http://localhost:8888/hello-service/master配置服务器hello-service.propertiesconfig-repo 存储库中获取。(如果你需要它config-service的源代码,我会把它推送到这个仓库。)

我的预期结果:world-service使用端口 8081。

我的实际结果:world-service使用端口 8080。

bootstrap.properties

spring.application.name=world-service
spring.cloud.config.uri=http://localhost:8888
Run Code Online (Sandbox Code Playgroud)

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>world-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>world-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>2020.0.0-M5</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency> …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot spring-cloud spring-cloud-config

10
推荐指数
3
解决办法
5396
查看次数

Spring OAuth:具有授权服务器后端的资源服务器

我想开发两个独立的服务,一个用于业务,一个用于使用Spring OAuth 2进行用户身份验证

我们称之为商业服务和OAuth服务.

现在,如果请求未经过身份验证,我希望将Business-Service委托给OAuth-Service.客户端应用程序(Android应用程序)不应该事先知道OAuth-Service,它应该只由Business-Service委托给它,并且302 HTTP重定向用于非认证请求.确切地说,我希望我的API登录页面提供指向http://businessservice.com/login的链接,当我的客户端应用程序决定关注此链接时,它会被重定向到OAuth-Service.

如果我使用@ EnableOAuth2Resource注释Business-Service ,那么当我在没有访问令牌的情况下卷曲它时,它的所有资源都会受到保护,返回401.到现在为止还挺好.如果我提供这样的访问令牌:

curl -v http://localhost:8667/resource/ -H "Authorization: Bearer $TOKEN"
Run Code Online (Sandbox Code Playgroud)

我可以访问该资源.还好.

但是,如果我使用@ EnableOAuth2Sso注释业务服务以启用重定向到OAuth服务,则会失去使用访问令牌访问资源的功能(与上面相同的卷曲),它只返回302到登录页面http: //本地主机:8667 /登录

如果我同时使用两个注释,@ EnableOAuth2Resource似乎总是"赢",因为身份验证有效,但调用http:// localhost:8667/login返回404.

那么创建一个委托给auth服务器进行非认证调用的资源服务器的正确方法是什么?

spring-security oauth-2.0 spring-security-oauth2 spring-cloud

9
推荐指数
1
解决办法
5756
查看次数

如何覆盖Spring Cloud OAuth2客户端自动配置?

我们想要设置一个提供REST API的微服务,以便将其配置为OAuth2资源服务器.此服务还应充当具有客户端凭据授权的OAuth2客户端.这是配置:

spring.oauth2.client.id=clientCredentialsResource
spring.oauth2.client.accessTokenUri=http://localhost:9003/oauth/token
spring.oauth2.client.userAuthorizationUri=http://localhost:9003/oauth/authorize
spring.oauth2.client.grantType=client_credentials
spring.oauth2.client.clientId=<service-id>
spring.oauth2.client.clientSecret=<service-pw>
Run Code Online (Sandbox Code Playgroud)

资源服务器部分工作正常.对于客户端部分,我们要使用Feign,Ribbon和Eureka:

@FeignClient("user")
public interface UserClient
{
  @RequestMapping( method = RequestMethod.GET, value = "/user/{uid}")
  Map<String, String> getUser(@PathVariable("uid") String uid);
}
Run Code Online (Sandbox Code Playgroud)

基于问题的主旨https://github.com/spring-cloud/spring-cloud-security/issues/56我创建了一个假装请求拦截器,它在假装请求头中设置自动装配的OAuth2RestOperations模板中的访问令牌

@Autowired
private OAuth2RestOperations restTemplate; 

template.header(headerName, String.format("%s %s", tokenTypeName, restTemplate.getAccessToken().toString()));
Run Code Online (Sandbox Code Playgroud)

但这给了我调用用户服务的错误:

error="access_denied", error_description="Unable to obtain a new access token for resource 'clientCredentialsResource'. The provider manager is not configured to support it.
Run Code Online (Sandbox Code Playgroud)

正如我所看到的,OAuth2ClientAutoConfiguration始终为Web应用程序创建AuthorizationCodeResourceDetails实例,但不创建仅用于非Web应用程序的必需ClientCredentialsResourceDetails.最后,no access token privider负责资源详细信息并且调用失败

AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:146) 
Run Code Online (Sandbox Code Playgroud)

我试图覆盖自动配置但失败了.有人可以给我一个提示怎么做?

spring spring-security spring-boot spring-security-oauth2 spring-cloud

9
推荐指数
1
解决办法
8741
查看次数

AWS Instance Profile不适用于Spring Cloud AWS

我有一个小的Spring Boot应用程序,使用Spring Cloud AWS(1.0.0.RELEASE)来访问SQS队列.它部署在具有实例配置文件集的EC2实例上.似乎AWS的一面正在发挥作用,因为我可以访问两个相关的元数据链接:iam/info并且iam/security-credentials/role-name,它们确实包含正确的信息.只是为了确定,我已经使用了aws cmdline实用程序(aws sqs list-queues)并且确实有效,所以我猜设置正常.但是,当应用程序启动时,它会读取application.properties(包含行cloud.aws.credentials.instanceProfile=true),然后在警告后丢弃:com.amazonaws.util.EC2MetadataUtils: Unable to retrieve the requested metadata最后抛出以下异常:

Caused by: com.amazonaws.AmazonServiceException: The security token included in the request is invalid. (Service: AmazonSQS; Status Code: 403; Error Code: InvalidClientTokenId; Request ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
        at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1071)
        at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:719)
        at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:454)
        at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:294)
        at com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:2291)
        at com.amazonaws.services.sqs.AmazonSQSClient.getQueueUrl(AmazonSQSClient.java:516)
        at com.amazonaws.services.sqs.buffered.AmazonSQSBufferedAsyncClient.getQueueUrl(AmazonSQSBufferedAsyncClient.java:278)
        at org.springframework.cloud.aws.messaging.support.destination.DynamicQueueUrlDestinationResolver.resolveDestination(DynamicQueueUrlDestinationResolver.java:78)
        at org.springframework.cloud.aws.messaging.support.destination.DynamicQueueUrlDestinationResolver.resolveDestination(DynamicQueueUrlDestinationResolver.java:37)
        at org.springframework.messaging.core.CachingDestinationResolverProxy.resolveDestination(CachingDestinationResolverProxy.java:88)
        at org.springframework.cloud.aws.messaging.listener.AbstractMessageListenerContainer.start(AbstractMessageListenerContainer.java:295)
        at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer.start(SimpleMessageListenerContainer.java:38)
        at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:173)
        ... 17 common frames omitted
Run Code Online (Sandbox Code Playgroud)

...这意味着由于某种原因,Spring …

java amazon-web-services spring-boot spring-cloud

9
推荐指数
1
解决办法
4580
查看次数

为什么Eureka DS Replicas Links不起作用?

为什么DS副本不能在Spring gui中运行?当您点击链接时,他们会尝试访问http:// hostname:8761/eureka /.该链接不应指向http:// hostname:8761 /

Eureka GUI

spring-cloud

9
推荐指数
1
解决办法
4330
查看次数

如何从Spring Cloud Config Server读取多个配置文件

Spring云配置服务器支持使用名称读取属性文件${spring.application.name}.properties.但是我的应用程序中有2个属性文件.

a.properties
b.properties
Run Code Online (Sandbox Code Playgroud)

我可以让配置服务器读取这两个属性文件吗?

spring-cloud spring-cloud-config

9
推荐指数
3
解决办法
5669
查看次数