小编Joh*_*han的帖子

为特定项目和集群运行单个 kubectl 命令?

背景

我们使用 Jenkins 将新版本的 Kubernetes (k8s) 复制控制器部署到我们的测试或生产集群。测试和生产(k8s)集群位于不同的(谷歌云平台)项目下。我们在 Jenkins 上为 gcloud SDK 配置了两个配置文件,一个用于测试 (test-profile),一个用于生产 (prod-profile)。我们在 Jenkins 中定义了一个托管脚本,用于为我们的复制控制器执行滚动更新。问题是我无法找到一种方法来控制我想要将kubectl rolling-update命令定位到哪个项目(您可以指定哪个集群,但不能指定哪个项目)。所以现在我们对测试服务器进行滚动更新的脚本看起来像这样:

gcloud config configurations activate test-profile && kubectl rolling-update ...
Run Code Online (Sandbox Code Playgroud)

虽然这可行,但如果两个作业在不同环境下同时运行,可能会非常危险。假设作业 1 的目标是测试环境,作业 2 的目标是生产环境。如果在作业 1 执行其任务之前,作业 2 将活动配置文件切换为“prod-profile”rolling-update命令之前将活动配置文件切换到“prod-profile”,则作业 1 将定位到错误的项目,并且在最坏的情况下更新错误的复制控制器(如果集群具有相同的名称)。

问题

有没有办法指定 kubectl 命令所针对的项目(例如在滚动更新期间)可以安全地同时运行?

google-cloud-platform kubernetes google-kubernetes-engine

4
推荐指数
1
解决办法
4250
查看次数

使用 RabbitMQ 通过 Spring Cloud Stream 为消费者提供多个绑定路由密钥

我想在 Spring Cloud Stream 中配置一个输入通道,以将其绑定到具有多个路由键的同一交换(目的地)。我已经设法使用单个路由键来实现此功能,如下所示:

spring:
  cloud:
    stream:
      rabbit:
        bindings:
          input1:
            consumer:
              bindingRoutingKey: key1.#
      bindings:
        input1:
          binder: rabbit
          group: group1
          destination: dest-group1
Run Code Online (Sandbox Code Playgroud)

但我似乎无法让它适用于多个键。我试过这个:

spring:
  cloud:
    stream:
      rabbit:
        bindings:
          input1:
            consumer:
              bindingRoutingKey: key1.#,key2.#
      bindings:
        input1:
          binder: rabbit
          group: group1
          destination: dest-group1
Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用。

我使用的是 Spring Boot 2.0.1,Spring 云依赖项是从以下位置导入的:

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

有谁知道如何实现这一目标?

spring rabbitmq spring-boot spring-cloud-stream

4
推荐指数
1
解决办法
2183
查看次数

使用cron在Spring中安排周期性反应任务?

通常我会做这样的事情来安排一个作业在 Spring 中定期执行,并在给定的时区使用 cron:

@Scheduled(cron = "0 0 10 * * *", zone = "Europe/Stockholm")
public void scheduleStuff() {
    // Do stuff
}
Run Code Online (Sandbox Code Playgroud)

这将阻止线程调用,scheduleStuff直到作业完成。然而,在这种情况下,“东西”我想要做的是所有使用弹簧无阻塞的构建块实现项目反应器(即MonoFlux等)。

例如,假设我想定期触发此功能:

Flux<Void> stuff() {
    return ..
}
Run Code Online (Sandbox Code Playgroud)

我当然可以简单地调用stuff().subscribe()(甚至stuff().block()),但这会阻塞线程。有没有更好的方法来实现与@Scheduled(cron = "0 0 10 * * *", zone = "Europe/Stockholm")非阻塞代码相同的东西?

我正在使用 Spring Boot 2.1。

cron spring spring-boot project-reactor spring-webflux

4
推荐指数
1
解决办法
4322
查看次数

使用 Spring Data MongoDB 在事务中调用两个不同 ReactiveMongoRepository 中的方法?

在 Spring Data MongoDB 中使用反应式编程模型时,可以执行如下事务:

Mono<DeleteResult> result = template.inTransaction()                                      
    .execute(action -> action.remove(query(where("id").is("step-1")), Step.class)); 
Run Code Online (Sandbox Code Playgroud)

但是 Spring Data MongoDB 也支持“reactive repositories”,例如:

public interface PersonRepository extends ReactiveMongoRepository<Person, String>

  Flux<Person> findByLocationNear(Point location, Distance distance);
}
Run Code Online (Sandbox Code Playgroud)

public interface CarRepository extends ReactiveMongoRepository<Car, String>

  Flux<Car> findByYear(int year);
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,鉴于你有ReactiveMongoRepository's,你能以某种方式利用 MongoDB 事务,例如在同一个事务中插入 aPerson和吗Car(在这种情况下使用PersonRepositoryCarRepository)?如果是这样,你如何做到这一点?

java spring mongodb spring-boot spring-data-mongodb-reactive

4
推荐指数
1
解决办法
2427
查看次数

在页面加载时加载 div,然后每隔 n 秒使用 htmx 加载?

使用HTMX,我知道我可以每 2 秒轮询一次“/news”,如下所示:

<div hx-get="/news" hx-trigger="every 2s">div>
Run Code Online (Sandbox Code Playgroud)

但页面加载后第一次触发需要2s时间。我想做的是在页面加载后立即触发对“/news”的调用,然后每 2 秒轮询一次。

我尝试过诸如以下的事情:

<div hx-get="/news" hx-trigger="on load or every 2s">div>
Run Code Online (Sandbox Code Playgroud)

<div hx-get="/news" hx-trigger="on htmx:afterOnLoad or every 2s">div>
Run Code Online (Sandbox Code Playgroud)

但这些都不起作用。我怎样才能用 HTMX 做到这一点?

(我知道这对于 HTMX 通常是一种不好的做法,因为您可以在响应中包含来自服务器的数据,但出于好奇我想知道:))

htmx

4
推荐指数
1
解决办法
2287
查看次数

以编程方式启动和停止 Spring Integration 中的某些 IntegrationFlows?

我在 spring 集成中创建了一些 IntegrationFlow,如下所示:

IntegrationFlows.from(..).id("test").autoStartup(false). ..
Run Code Online (Sandbox Code Playgroud)

如何在 Spring Integration 中查找此集成流程并启动它?我正在寻找类似于org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry可用于启动和停止RabbitListener实例的东西。

java spring spring-integration

3
推荐指数
1
解决办法
1132
查看次数

Spring Reactive Webclient 的请求级背压?

这类似于如何做 akka-http 请求级背压?但对于 Spring 回声系统。

我正在考虑如何在以反应方式使用 Spring WebClient 时为 HTTP 客户端实现背压。对我来说,这听起来像是让 WebClient 意识到 HTTP 语义并对例如状态“429 - 请求过多”施加反压。我没有找到任何关于此的文档,这让我有点怀疑这是否是要走的路。

问题:

  1. 基于 HTTP 响应标头的背压是否有意义(例如,基于 429 或 503 状态代码和Retry-After标头)?或者是否有更好的方法通过 HTTP为非流式(请求-响应)用例进行背压?
  2. 是否在 Webclient 或其他一些与 Spring 反应式回声系统配合良好的库中实现了这样的功能?
  3. 如果当前不存在这样的情况并且考虑到它是有道理的,那么简单地使用Retry-After标头中设置的超时重试是否有意义?

spring spring-boot project-reactor spring-webflux

3
推荐指数
1
解决办法
1348
查看次数

如何在 Spring Webflux 中获取当前控制器方法的 URL?

这个问题类似于在 Spring MVC 中获取当前 URL 的最佳方法什么?但是对于 Webflux(它也可能与如何在 spring-webflux 中获取当前请求的上下文有关,但这个问题更具体,所以我希望有解决方案)。

我想要做的是在 Webflux 控制器中获取当前 URL。例如:

@GetMapping("/greeting")
public Mono<Greeting> greeting() {
   String linkToCurrentResource = .. // How do I get this?
   Greeting greeting = new Greeting("hello",  linkToCurrentResource);
   return Mono.just(greeting);
}
Run Code Online (Sandbox Code Playgroud)

如果在负载均衡器后面运行,最好linkToCurrentResource应该了解X-Forwarded-???标头。所以在这种情况下,我想linkToCurrentResource成为类似https://external-uri.com/api/greeting. 如果重要的话,我正在使用 Netty 服务器实现。

java spring-boot spring-webflux

3
推荐指数
1
解决办法
4447
查看次数

使用 Spring Data MongoDB 中的 MongoTemplate 进行查找查询时仅投影某些字段?

我想知道如果可能的话,如何使用 Spring Data MongoDB 执行查找查询mongoTemplate但不返回整个文档?使用 MongoDB 的“本机”Java 同步驱动程序,我可以这样做:

Document document = myCollection.find(eq("something", 12)).sort(descending("field")).limit(1).projection(include("field")).first();
Run Code Online (Sandbox Code Playgroud)

这样document只包含“字段”而没有其他内容。

我怎样才能做同样的事情,mongoTemplate因为我似乎找不到与projection使用时类似的东西mongoTemplate.findOne(..)。您是否需要使用聚合管道来执行此操作mongoTemplate

我正在使用 Spring Data MongoDB 版本 3.0.1.RELEASE (spring boot 2.3.3)。

spring mongodb spring-data-mongodb mongotemplate

3
推荐指数
1
解决办法
3302
查看次数

使用 Hyperscript 清除提交时的所有输入字段

我正在使用 htmx 和 hyperscript,我希望在提交时清除下面表单中的所有输入字段:

<form hx-post="/example" hx-target="#table tbody" hx-swap="beforeend"
      _="<what should I write here??>">
        <label class="control-label" for="firstNameInput">First Name</label>
        <input id="firstNameInput" name="firstName" class="form-control" type="text" required placeholder="John"/>
        
        <label class="control-label" for="lastNameInput">Last Name</label>
        <input id="lastNameInput" name="lastName" class="form-control" type="text" required placeholder="Doe"/>
        <button class="btn btn-primary">Add User</button>
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)

我尝试<what should I write here??>用例如on submit put '' into <input/>on submit put '' into <input[value]/>许多其他组合替换,但我无法完成这项工作。

问:提交表单后如何清除所有输入字段?

hyperscript htmx

3
推荐指数
1
解决办法
2269
查看次数