标签: spring-cloud

如何在Spring云中创建自定义zuul过滤器

我想为我正在编写的spring cloud微服务编写一些自己的自定义zuul过滤器.一旦我编写了过滤器,我该如何集成它,以便底层的netflix zuul框架可以利用它.

spring-cloud netflix-zuul

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

使用Feign客户端RequestInterceptor转发请求标头或安全上下文

我想转发带有假装客户端RequestInterceptor的请求标头,但是,在内RequestInterceptor.apply, RequestContextHolder.getRequestAttributes()是null,所以SecurityContextHolder.getContext().getAuthentication()(我最终也可以得到我的标头的值).

这曾经在升级到Spring-Cloud Brixton之前工作,其中hystrix命令现在必须可以在单独的线程中运行,因为更改为以下参数可以解决问题:

hystrix.command.default.execution.isolation.strategy: SEMAPHORE
Run Code Online (Sandbox Code Playgroud)

现在,如果没有必要,我不太热衷于更改这种默认值,是否有另一种推荐的转发标头现在的方式?

谢谢

spring-boot spring-cloud netflix-feign

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

Spring cloud SQS - 轮询间隔

使用Spring云监听AWS SQS队列,如下所示:

@SqsListener(value = "${queue.name}", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
public void queueListener(String message, @Headers Map<String, Object> sqsHeaders) {
    // code
}
Run Code Online (Sandbox Code Playgroud)

Spring配置:

<aws-messaging:annotation-driven-queue-listener
    max-number-of-messages="10" wait-time-out="20" visibility-timeout="3600"
    amazon-sqs="awsSqsClient" />
Run Code Online (Sandbox Code Playgroud)

AwsSqsClient:

@Bean
public com.amazonaws.services.sqs.AmazonSQSAsyncClient awsSqsClient() {
    ExecutorService executorService = Executors.newFixedThreadPool(10);
    return new AmazonSQSAsyncClient(new DefaultAWSCredentialsProviderChain(), executorService);
}
Run Code Online (Sandbox Code Playgroud)

这很好用.

在SQS客户端中配置了10个线程来处理这些消息,如上所示.这也工作正常,在任何时间点处理最多10条消息.

问题是,我无法弄清楚控制轮询间隔的方法.默认情况下,一旦所有线程都空闲,弹出轮询.

即考虑以下示例

  1. 大约有3条消息被发送到Queue
  2. Spring轮询队列并获得3条消息
  3. 处理每条消息的3条消息大约需要20分钟

与此同时,大约有25条消息被送到队列.在完成之前传递的所有3条消息之前,Spring不会轮询队列.从上面的例子来看,只有20分钟之后春季民意调查,尽管有7个线程仍然免费!

知道我们如何控制这种民意调查吗?即如果有任何线程空闲,则应该开始轮询,并且不应该等到所有线程都空闲

message-queue amazon-sqs amazon-web-services aws-sdk spring-cloud

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

Spring Cloud Config Server

我正在使用Spring Cloud Config Server并能够检测来自git存储库的更改并将其传递给Config客户端.

有两种方法,我已经实现了它:

  1. 在git存储库中进行更改(提交和推送)后,我发出curl请求curl -X POST http:// server:port/bus/refresh,它运行正常.为此,我使用RabbitMQ作为Spring Cloud Bus.

参考:http: //tech.asimio.net/2017/02/02/Refreshable-Configuration-using-Spring-Cloud-Config-Server-Spring-Cloud-Bus-RabbitMQ-and-Git.html

  1. 在git存储库中进行更改(提交和推送)后,我发出curl请求curl -X POST http:// server:port/refresh(在url中没有/ bus),它工作正常.我不是在这里使用Spring Cloud Bus.

参考:https://spring.io/guides/gs/centralized-configuration/

所以两者都运行良好,那么使用Spring Cloud Bus是否有任何优势,或者在生产环境中,没有Spring Cloud Bus会有任何问题吗?因为在生产中将RabbitMQ集群(HA)设置为Spring Cloud Bus需要额外的努力.

谢谢,大卫

spring-boot spring-cloud spring-cloud-netflix spring-cloud-config

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

SpringBoot日志记录中的ClassCastException

我的应用程序启动时遇到问题。在ClassCastException上配置日志级别时,应用程序失败java.util.HashMap cannot be cast to org.springframework.boot.logging.LogLevel。

我尝试logging.level.root=INFO在我的设置,application.properties并发生相同的错误。不知道还有什么尝试。

我刚刚将其升级到SpringBoot 2.x,特别是使用了此依赖项BOM:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springframework.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
Run Code Online (Sandbox Code Playgroud)

请找到依赖关系树的相关部分:

[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.0.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.12.1:compile
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.12.1:compile
[INFO] |  |  |  \- …
Run Code Online (Sandbox Code Playgroud)

java spring classcastexception spring-boot spring-cloud

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

微服务:Spring 云应用中的 Zuul &amp; consul

我正在尝试使用 Zuul 和 Consul 创建一个 Spring 云微服务应用程序。

我的项目中有 2 个组件:

  1. 使用 Zuul 的 api-gateway 微服务

  2. Hello world 微服务(一个简单的 hello world Rest Webservice)

这是 api-gateway 的代码:

 @SpringBootApplication
 @EnableZuulProxy
 @EnableDiscoveryClient
 public class ZuulApplication {

 public static void main(String[] args) {
    SpringApplication.run(ZuulApplication.class, args);
  }

 }
Run Code Online (Sandbox Code Playgroud)

pom.xml

<parent>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-parent</artifactId>
    <version>Brixton.M3</version>
</parent>

<properties>
    <java.version>1.8</java.version>
    <spring.cloud.consul.version>1.0.0.M4</spring.cloud.consul.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-commons</artifactId>
    </dependency>

    <dependency>
        <!-- Setup Spring Boot -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <!-- Setup Spring MVC & REST, use Embedded Tomcat -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId> …
Run Code Online (Sandbox Code Playgroud)

spring-boot consul spring-cloud netflix-zuul

6
推荐指数
1
解决办法
6048
查看次数

Spring Cloud Config Eureka-first方法无效

我正在开发Spring Cloud Eureka微服务应用程序.我希望我的服务通过Eureka优先方法连接到配置服务.微服务被打包为docker容器并通过docker-compose进行部署.该申请由以下人员组成:

  1. myapp-service-registry:使用Spring Cloud Eureka实现的服务注册服务
  2. myapp-config-service:Spring Cloud配置服务服务器
  3. myapp-service-test:一个示例微服务,它应该尝试通过Eureka优先方法连接到配置服务来获取配置数据.

与配置服务的连接失败,如下所述.首先是一些配置数据:

这里myapp-service-registry的application.yml:

server:
  port: ${PORT:8761}

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
  server:
    waitTimeInMsWhenSyncEmpty: 0
Run Code Online (Sandbox Code Playgroud)

这是myapp-config-service's application.yml:

server:
  port: ${MYAPP_CONFIG_SERVICE_PORT:8888}

spring: 
  cloud:
    config:
      server:
        git:
          uri: ${MYAPP_CONFIG_SERVICE_GIT_URI}
  config:
    name: myapp-config-service

# eureka service registry client

eureka: 
    client:
        serviceUrl:
            defaultZone: http://${SERVICE_REGISTRY_HOST}:${SERVICE_REGISTRY_PORT}/eureka/
    instance:
        preferIpAddress: true
Run Code Online (Sandbox Code Playgroud)

配置服务器和客户端初始化为https://github.com/spring-cloud-samples/tests中的样本configserver-eureka和eureka-first样本:

myapp-config-service的bootstrap.yml是:

spring:
    application:
        name: myapp-config-service
    cloud:
        config:
            discovery:
                enabled: …
Run Code Online (Sandbox Code Playgroud)

spring spring-boot microservices spring-cloud netflix-eureka

6
推荐指数
1
解决办法
1万
查看次数

为可能的空数组定义合同?

我正在尝试使用 Spring-Cloud-Contract 定义 CDC 合同,如下所示:

org.springframework.cloud.contract.spec.Contract.make {
    request {
        method 'GET'
        url $(client(~/\/categories\?publication=[a-zA-Z-_]+?/), server('/categories?publication=DMO'))
    }
    response {
        status 200
        headers {
            header('Content-Type', 'application/json;charset=UTF-8')
        }
        body """\
            [{
                "code": "${value(client('DagKrant'), server(~/[a-zA-Z0-9_-]*/))}",
                "name": "${value(client('De Morgen Krant'), server(~/[a-zA-Z0-9_\- ]*/))}",
                "sections" : []
            },
            {
                "code": "${value(client('WeekendKrant'), server(~/[a-zA-Z0-9_-]*/))}",
                "name": "${value(client('De Morgen Weekend'), server(~/[a-zA-Z0-9_\- ]*/))}",
                "sections" : [
                    {
                    "id" : "${value(client('a984e824'), server(~/[0-9a-f]{8}/))}",
                    "name" : "${value(client('Binnenland'), server(~/[a-zA-Z0-9_\- ]*/))}"
                    }
                ]
            }]
        """
    }
}
Run Code Online (Sandbox Code Playgroud)

在生成的测试中,这会导致以下断言:

DocumentContext parsedJson = JsonPath.parse(response.getBody().asString());
assertThatJson(parsedJson).array().contains("code").matches("[a-zA-Z0-9_-]*");
assertThatJson(parsedJson).array().array("sections").contains("id").matches("([0-9a-f]{8})?");
assertThatJson(parsedJson).array().array("sections").contains("name").matches("[a-zA-Z0-9_\\- ]*");
assertThatJson(parsedJson).array().contains("name").matches("[a-zA-Z0-9_\\- ]*"); …
Run Code Online (Sandbox Code Playgroud)

spring-cloud spring-cloud-contract

6
推荐指数
1
解决办法
4613
查看次数

“spring.cloud.consul.host”配置值可以有多个领事代理吗?

我对这个配置有点困惑。我的 Spring Boot 应用程序@EnableDiscoveryClient已spring.cloud.consul.host设置为localhost. 我正在运行我的引导应用程序的主机上运行 Consul 代理,但我有几个问题(似乎无法在文档中找到我的答案)。

这个配置可以接受多个值吗?

  • 如果是这样,我更愿意将这些值设置为 Consul 服务器地址列表(但是,运行 Consul 代理有什么意义呢,所以这似乎不切实际,这意味着我在这里不明白)
  • 如果没有,我们是否希望在运行引导应用程序的每个节点上运行 Consul Agent @EnableDiscoveryClient?(这也感觉不对;一方面,即使一个代理应该能够告诉集群的所有信息,这似乎也是一个单点故障;如果我无法联系到这个代理怎么办?)
  • 这种配置的最佳实践是什么?

consul spring-cloud spring-cloud-consul

6
推荐指数
1
解决办法
2503
查看次数

从 Spring Boot 连接到 AWS SQS 时出错

我正在尝试使用 spring cloud AWS 将 AWS SQS 集成到我的 springboot 应用程序中,但不断收到此错误(在下面发布),有人可以帮忙吗?

这是我的文件。

org.springframework.beans.factory.BeanCreationException:创建名为“simpleMessageListenerContainer”的 bean 在类路径资源 [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class] 中定义时出错:调用 init 方法失败;嵌套异常是 java.lang.NoSuchMethodError: com.amazonaws.http.ExecutionContext.setCredentials(Lcom/amazonaws/auth/AWSCredentials;)V

@Configuration
public class AWSConfig {
    @Value("${amazon.dynamodb.endpoint}")
    private String amazonDynamoDBEndpoint;

    @Value("${amazon.aws.accesskey}")
    private String amazonAWSAccessKey;

    @Value("${amazon.aws.secretkey}")
    private String amazonAWSSecretKey;

    @Value("${amazon.sqs.endpoint}")
    private String amazonSqsEndpoint;
    @Bean
    @Primary
    public AmazonSQSAsyncClient amazonSQSAsyncClient() {

        AmazonSQSAsyncClient amazonSQSAsyncClient = new AmazonSQSAsyncClient(amazonAWSCredentials());
        if (!StringUtils.isEmpty(amazonSqsEndpoint)) {
            amazonSQSAsyncClient.setEndpoint(amazonSqsEndpoint);
        }

        return amazonSQSAsyncClient;

    }

    @Bean
    public AWSCredentials amazonAWSCredentials() {
        return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以与 dynamodb 一起工作,但无法连接到 SQS。我已经在 application.properties 文件中提供了正确的访问密钥、秘密访问密钥和端点。

@Component
    @EnableSqs
    public …
Run Code Online (Sandbox Code Playgroud)

spring amazon-sqs amazon-web-services spring-boot spring-cloud

6
推荐指数
1
解决办法
9023
查看次数