标签: spring-cloud

spring-cloud with ribbon/eureka/hystrix使用restTemplate无法设置连接/读取超时

我使用spring-cloud构建了一个spring boot应用程序,并希望在我的客户端应用程序(也是一个微服务)中使用RestTemplate,这样我就可以继续使用mockMvc进行集成测试.我正在使用我正在调用的服务中的客户端微服务和eureka客户端的默认ribbon/eureka/hystrix客户端设置.这是有效的(一旦我发现serviceIds是标识restTemplate中的服务端点的东西).我的问题是,我似乎无法更改restTemplate读取和连接超时,似乎默认为300毫秒.

电话详情:

`@Configuration
 @EnableAutoConfiguration
 @ComponentScan
 @EnableConfigurationProperties
 @EnableHystrix
 @EnableEurekaClient
public class Application { ... public static void main(String[] args) {} ... }

@Component
class EricComponentToDoHystrix {   // apparently this has to be a component for hystrix to work btw
    @Autowired
    RestTemplate restTemplate;
    ..
    @HystrixCommand(fallbackMethod="defaultRestTemplateCall")
    public void doRestTemplateCall() 
        ResponseEntity<String> result = restTemplate.getForEntity("http://someservice/doSomething", String.class);  // actually make a call
    ..
    }
}`
Run Code Online (Sandbox Code Playgroud)

使用包含以下内容的application.properties:

spring:
  cloud:
    client:
      serviceIds:
        - someservice

someservice:
  ribbon:
    #listOfServers: localhost:7080
    #NIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList

    # the eureka vipAddress of the target …
Run Code Online (Sandbox Code Playgroud)

spring-cloud

7
推荐指数
1
解决办法
6389
查看次数

Sonar抱怨Spring Boot配置

我有这个类来启动spring-cloud配置服务器.这是一个spring-boot应用程序.

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {

    public static void main( String[] args ) {

        SpringApplication.run( ConfigServerApplication.class, args );

    }

}
Run Code Online (Sandbox Code Playgroud)

应用程序运行正常,我所有的单元测试都很好.但是,在我们的竹子管道中,它将初始化一个声纳过程来分析代码.我们不断收到这些小警告,表明以下内容:

Utility classes should not have a public constructor
Run Code Online (Sandbox Code Playgroud)

我知道这是一个小问题,但我的任务是从代码中删除这些.

理想情况下,您可以将类标记为final并提供私有构造函数,或者所有搜索都提供解决方案.但是,Spring Configuration类不能成为final,也不能有私有构造函数.

任何想法如何解决这个问题?

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

7
推荐指数
1
解决办法
5485
查看次数

尤里卡的自我保护模式永远不会恢复

我目前正面临一个问题,即Eureka从未清除因虚拟机意外停机而变得陈旧的服务实例.可以理解的是,Eureka的自我保护模式已经启动,因为在服务续订/心跳请求中存在大幅下降(低于阈值).然而,15个多小时后,死亡的实例仍在Eureka注册.这是一个主要问题,因为服务请求继续被定向到死实例,仅返回错误.

我的希望是门槛不断调整,经过一段时间后,尤里卡的门槛将处于新的标准水平,自我保护模式将被重置.我们在镜像设置中使用Eureka,我们的配置不是很复杂.

我们的设置:

Eureka通过spring-boot-starter-parent 1.2.5.RELEASE

eureka:
  dashboard:
    path: services
    enabled: false
  instance:
    hostname: localhost
    leaseRenewalIntervalInSeconds: 3
    metadataMap:
      managementPath: /admin
      instanceId: discoveryPrimary
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
    waitTimeInMsWhenSyncEmpty: 0
Run Code Online (Sandbox Code Playgroud)

是否可以调整Eureka配置以重置自我保存模式(停止清除实例的位置)并允许它在服务已停止5分钟以上时清除服务注册表?

spring-cloud netflix-eureka

7
推荐指数
1
解决办法
3712
查看次数

SpringBoot @SqsListener - 不工作 - 使用Exception - TaskRejectedException

我有一个AWS SQS,其中已有5000条消息在队列中(示例消息看起来像'Hello @ 1')我创建了一个SpringBoot应用程序,并在其中一个组件类中创建了一个从SQS读取消息的方法.

package com.example.aws.sqs.service;

import org.springframework.cloud.aws.messaging.listener.SqsMessageDeletionPolicy;
import org.springframework.cloud.aws.messaging.listener.annotation.SqsListener;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class MessageReceiverService {   

@SqsListener(value = { "${cloud.aws.sqs.url}" }, deletionPolicy = SqsMessageDeletionPolicy.ALWAYS)
public void readMessage(String message){
    log.info("Reading Message... {}", message);
}
Run Code Online (Sandbox Code Playgroud)

}

我的主要SpringBoot类

@SpringBootApplication 
public class AwsSqsApplicationConsumer {
public static void main(String[] args) {
    SpringApplication.run(AwsSqsApplicationConsumer.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)

我运行应用程序时遇到的异常:

s.c.a.m.l.SimpleMessageListenerContainer : An Exception occurred while polling queue '<my sqs name>'. The failing operation will be retried in 10000 milliseconds
org.springframework.core.task.TaskRejectedException: Executor [java.util.concurrent.ThreadPoolExecutor@7c1594a5[Running, pool size …
Run Code Online (Sandbox Code Playgroud)

java amazon-sqs spring-boot spring-cloud

7
推荐指数
3
解决办法
4177
查看次数

如何在 opentracing 中更改 trace-id 标头?

有没有办法配置 opentracing-spring-jaeger-cloud-starter 来处理除 Uber-Trace-Id 之外的任何其他标头?我的 kubernetes 集群中有 Traefik 作为入口。Traefik 可以配置为更改 traceContextHeaderName。默认值为“uber-trace-id”。当我将其更改为某种自定义时,服务之间没有连接(我的意思是跨度连接)。我相信 opentracing 仅适用于 Uber-Trace-Id。有没有办法配置它?

我在 minikube 中使用 Traefik 作为入口进行测试。然后所有请求都转到 spring-cloud-gateway 并传播到服务。

感谢帮助!

java spring-cloud traefik opentracing jaeger

7
推荐指数
1
解决办法
3598
查看次数

防止 spring-cloud-aws-messaging 尝试停止队列

我spring-cloud-aws-messaging在 Spring Boot 项目中使用。
我在 AWS 中手动创建了 SQS 队列。

它的使用方式如下:

@SqsListener("${sqs.name.incoming}")
public void listen(String message) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

它工作正常。但是当我在 IDE 中停止我的应用程序时,或者当 Spring Boot 测试完成时,它会尝试停止队列。它无法阻止它并最终超时。它抛出这个异常:

2019-10-29 15:40:07.949  WARN 10378 --- [       Thread-2] s.c.a.m.l.SimpleMessageListenerContainer : An exception occurred while stopping queue 'my-awesome-queue-name'

java.util.concurrent.TimeoutException: null
    at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:204) ~[na:na]
    at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer.waitForRunningQueuesToStop(SimpleMessageListenerContainer.java:161) ~[spring-cloud-aws-messaging-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer.doStop(SimpleMessageListenerContainer.java:140) ~[spring-cloud-aws-messaging-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.cloud.aws.messaging.listener.AbstractMessageListenerContainer.stop(AbstractMessageListenerContainer.java:351) ~[spring-cloud-aws-messaging-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer.stop(SimpleMessageListenerContainer.java:45) ~[spring-cloud-aws-messaging-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.cloud.aws.messaging.listener.AbstractMessageListenerContainer.stop(AbstractMessageListenerContainer.java:239) ~[spring-cloud-aws-messaging-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer.stop(SimpleMessageListenerContainer.java:45) ~[spring-cloud-aws-messaging-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.doStop(DefaultLifecycleProcessor.java:238) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.access$300(DefaultLifecycleProcessor.java:53) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.stop(DefaultLifecycleProcessor.java:377) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.stopBeans(DefaultLifecycleProcessor.java:210) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.onClose(DefaultLifecycleProcessor.java:128) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE] …
Run Code Online (Sandbox Code Playgroud)

java spring-boot spring-messaging spring-cloud spring-cloud-aws

7
推荐指数
1
解决办法
3612
查看次数

如何在微服务架构中实现基于角色的安全

我有一个带有 4 个微服务、eureka 服务器和一个集中式 API 网关的 spring-boot 应用程序。

所有外部流量都通过我的 API 网关到达我的微服务。

我的 API 网关 (Zuul) 正在验证和验证 JWT 令牌。

JWT 令牌由我的微服务之一在用户登录后生成(用户微服务),该令牌包含用户 ID 及其角色/权限。

现在,我想对网关以外的微服务中存在的方法实施基于角色的安全性。

我尝试使用,@PreAuthorize但它在网关之外不起作用(显然,为了使其工作,我必须SecurityContextHolder在我的微服务中设置一个 Spring Security 身份验证对象并用权限填充它)。

那么是否有任何解决方案可以实现这种安全性?

在微服务架构中设置安全性的最佳设计是什么?

API 网关级别的身份验证和微服务级别的授权?

在 API 网关级别验证 JWT 后,我是否需要在微服务中使用 spring security 或仅传递角色(将它们附加到请求中),例如创建我自己的注释并使用 Spring AOP 处理授权?

spring-security spring-boot microservices spring-cloud netflix-zuul

7
推荐指数
3
解决办法
3395
查看次数

Spring Cloud Gateway Cors 问题

我在 Spring Cloud Gateway 应用程序中设置 CORS 配置时遇到问题。我已经设置了以下配置以允许所有内容:

spring:
  cloud:
    gateway:
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOrigins: "*"
            allowedMethods: "*"
            allowedHeaders: "authorization"
Run Code Online (Sandbox Code Playgroud)

然而,当我调用网关的端点时,我得到:

OPTIONS http://localhost:8080/api/spot/spots 403 (Forbidden)
localhost/:1 Access to XMLHttpRequest at 'http://localhost:8080/api/spot/spots' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Run Code Online (Sandbox Code Playgroud)

当我通过 UI 调用绕过网关的服务之一时,请求运行良好。但是,我没有看到任何选项请求。

一旦我将 UI 指向网关主机,就会发生错误。

同样在网络选项卡中,请求显示为“失败”,没有其他信息。任何应用程序日志中也没有任何内容。这些是响应头:

HTTP/1.1 200 OK
transfer-encoding: chunked
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Access-Control-Allow-Origin: *
Vary: Origin
Vary: …
Run Code Online (Sandbox Code Playgroud)

spring-security spring-cloud spring-cloud-gateway

7
推荐指数
1
解决办法
5788
查看次数

当我尝试使用 AWS IAM 角色连接 HashiCorp Vault 时,如何修复“Vault location [kv/my-client-service] 无法解析:未找到”?

我已经使用 HashiCorp Vault 六个月了,我的所有秘密都来自配置服务。我正在使用spring.cloud.config.token连接我所有的客户端服务,但是当保险库令牌每 30 天左右过期时,问题就出现了。对于较低的环境,令牌到期是可以接受的,因为我们可以一次又一次地重新部署,但生产,我们不能重新部署。因此,决定使用 AWS IAM 角色,可以连接到保管库并且不会有任何过期。

我已关注此官方 链接,但在启动应用程序时遇到以下问题。

在此处输入图片说明

我已经用谷歌搜索了它,但没有得到一个有效的解决方案。

我在我的客户端服务 (my-client-service) 的 bootstrap.yml 文件中使用以下代码

引导程序.yml

spring:
  application:
    name: my-client-service
  cloud:
    config:
      enabled: true
      uri:  'https://localhost:8080' 
    vault:
      enabled: true
      uri: 'https://localhost:8090'
      port: 443
      scheme: https
      namespace: 'vault-namespace/aus'
      authentication: AWS_IAM
      fail-fast: true
      aws-iam:
        role: aus-vault-role
        aws-path: aws
      generic:
        enabled: true
        backend: kv
        profile-separator: '/'
        default-context: my-client-service
        application-name: my-client-service
      config:
        order: -1000
Run Code Online (Sandbox Code Playgroud)

Vault 身份验证 ARN 到 AWS

vault write auth/aws/config/sts/<account_number> sts_role=arn:aws:iam::<account_number>:role/role_name
Run Code Online (Sandbox Code Playgroud)

将 ARN 与保险柜策略相关联

我为同一个账户创建了一个 IAM 角色,该角色映射到一个 …

amazon-web-services amazon-iam spring-cloud hashicorp-vault spring-vault

7
推荐指数
1
解决办法
898
查看次数

升级到 Spring Boot 2.4.0 打破传统应用

我正在将一组遗留应用程序升级到 Spring Boot 2.4.0,在 OpenJDK 8 上运行,部署到 Pivotal Cloud Foundry。

在我开始升级之前,该应用程序及其所有单元测试在 Spring 2.3.4-RELEASE 下完美运行。

有一些工作需要立即完成:Spring Boot 2.4.0 引入了 JUnit 5.x,所以我必须修复所有 JUnit 4.x 测试以使用新的注释和类。

修复所有测试后,我尝试在 IntelliJ 2020.2 中运行。由于相同的原因,所有测试都失败了:java.lang.IllegalStateException: Failed to load ApplicationContext. 堆栈跟踪的根源给出了这个原因:

Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
Run Code Online (Sandbox Code Playgroud)

为了 Kubernetes,他们从 Spring Boot 中删除了一个基本类。我不确定我明白为什么。

我做了谷歌搜索,找到了这个解释和修复。

我添加spring.config.use-legacy-processing到我的application.yml和一个测试application.properties文件中:

spring:
  config:
    use-legacy-processing: true
Run Code Online (Sandbox Code Playgroud)

仍然没有快乐 - 所有的测试都失败了。

我对我看到的其他链接感到困惑。这是一个问题spring-cloud-dependencies吗?我读到Hoxton版本可能有问题。我需要将它添加到我的应用程序中吗?在升级之前它从不需要这个依赖:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR9</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

问题比测试更大。如果我忽略它们并尝试运行该应用程序,我仍然会失败:

ERROR [main]: Application run failed …
Run Code Online (Sandbox Code Playgroud)

intellij-idea spring-boot spring-cloud junit5

7
推荐指数
1
解决办法
5538
查看次数