标签: spring-cloud

缺少 spring-cloud-starter-ribbon 的 HTTP 客户端依赖项

我有一个简单的 Spring Boot 应用程序,它有一个简单的 REST 客户端,看起来像这样:

@Service
public class MyRestClient {

  private static final String url = "http://localhost:8080/";

  private RestTemplate restTemplate;

  @Autowired
  public MyRestClient(RestTemplate restTemplate) {
    this.restTemplate = restTemplate;
  }

  public String invoke() {
    return restTemplate.getForObject(url, String.class);
  }
}
Run Code Online (Sandbox Code Playgroud)

这与 Spring Boot 完美配合。

现在我正在尝试添加Spring Cloud到项目中以实现功能区客户端负载平衡。我按照这里的链接:

https://spring.io/guides/gs/client-side-load-balancing/

或者这里,这似乎是复制和粘贴,但具有更多更新的依赖项:

http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon

即使没有添加任何注释MyRestClient,我添加以下内容的那一刻:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我收到以下异常:

 Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.client.RestTemplate]: Factory method 'restTemplate' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClients
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
        at …
Run Code Online (Sandbox Code Playgroud)

rest spring spring-boot spring-cloud spring-cloud-netflix

5
推荐指数
1
解决办法
5214
查看次数

如何从 MessageCollector 接收带有对象负载而不是字符串的 GenericMessage

我尝试通过spring-cloud-stream使用测试消息发送和接收MessageCollector。

当我对流云使用本机 java 序列化时一切正常,但是当我将序列化更改为 json 时,MessageCollector 返回带有string有效负载而不是SomeObject有效负载的GenericMessage 。

配置是

  cloud.stream.default.contentType=application/json 
Run Code Online (Sandbox Code Playgroud)

测试用例:

    Message outMsg = new GenericMessage<SomeObject>(new SomeObject(1));
    someChannel.send(outMsg);
    GenericMessage<SomeObject> inMsg = (GenericMessage<SomeObject>) messageCollector.forChannel(someChannel).poll();

    Assert.assertTrue(inMsg.getPayload() instanceof SomeObject);
Run Code Online (Sandbox Code Playgroud)

因此,断言是错误的。inMsg包含字符串负载(字符串包含 SomeObject 的有效 json 表示)。

我的问题是:我如何GenericMessage从 SomeObject接收有效载荷MessageCollector?

生产环境工作正常,无需显式映射到SomeObject.

java spring spring-messaging spring-cloud spring-cloud-stream

5
推荐指数
1
解决办法
2385
查看次数

在 Spring Cloud Gateway 中集成多个服务响应

当我获得请求表单路径时,例如 /bar 是否可以在 Spring Cloud Gateway 中调用多个微服务并集成它们的结果(例如 JSON)并作为 /bar 的响应发送?我该怎么做?谢谢

spring spring-cloud

5
推荐指数
1
解决办法
951
查看次数

Spring Cloud Bus - 客户端未收到更改

我想我在使用 RabbitMQ 实现 Spring Cloud Bus 方面陷入了死胡同。

请记住,这是通过Spring Boot 2.0.0.M7 和 Spring Cloud Finchley.M5 完成的,根据它们的发行说明应该是兼容的。

我试图让它发挥作用:

  • 在配置服务器的 Git 存储库中编辑 .yml 文件
  • ConfigServer 应该通过 GitHub 的 webhook 接收通知(通过 ngrok 暴露本地主机地址)
  • 连接到配置服务器的客户端应该通过/actuator/bus-refreshPOST 调用接收刷新请求

但是,我可以在服务器的日志中看到这一点:

o.s.cloud.bus.event.RefreshListener      : Received remote refresh request. Keys refreshed []
Run Code Online (Sandbox Code Playgroud)

(空数组是罪魁祸首吗?在这种情况下,我真的不知道该怎么办:()

客户端没有任何内容(除非我手动向总线刷新端点发送 POST 请求)......在这种情况下,日志中的输出如下:

o.s.cloud.bus.event.RefreshListener      : Received remote refresh request. Keys refreshed [config.client.version, message]
Run Code Online (Sandbox Code Playgroud)

当我查看 RabbitMQ(作为 Docker 容器运行)中的图表时,我看到了三个连接的应用程序:客户端、Zuul(反向代理)和配置服务器。

你能帮我解决这个问题吗?非常感谢。以下是我对应用程序的配置。

配置服务器 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.paromsoft</groupId>
    <artifactId>config</artifactId>
    <version>0.0.1-SNAPSHOT</version> …
Run Code Online (Sandbox Code Playgroud)

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

5
推荐指数
0
解决办法
1717
查看次数

如何使用 Feign.Builder 实现侦探追踪?

我正在尝试将 Sleuth 集成到我们的系统中。如果我使用带注释的接口@FeignClient,则一切正常。这些接口会自动检测,并且 Sleuth 标头会通过 REST 调用传播。

但是,我们有一些现有代码直接使用 Feign.Builder 和 Feign 注释接口(只是没有用 注释@FeignClient)。这段代码添加了一些自定义的请求拦截器、编码器、代理等。

例如:

// Feign REST interface
public interface MyService {

    @RequestMapping(method = RequestMethod.GET, value = "/version")
    String getVersion();
}

// Creating the builder
Feign.Builder builder = Feign.builder();
builder.requestInterceptor(new MyCustomInterceptor());
// + adding proxy, encoder, decoder, etc

// Using the builder
MyService myService = builder.target(MyService.class, "http://localhost:8080/myservice");
myService.getVersion();
Run Code Online (Sandbox Code Playgroud)

我希望这个旧代码传播 Sleuth 标头。有什么简单的方法来连接它吗?

(我想一种选择是重新设计我们的 Feign 接口以使用 @FeignClient 并重新设计所有自定义拦截器、编码器等的应用方式,但最终这可能需要大量工作并有很大风险。)

我是否需要做一个特殊的请求拦截器来手动注入这些(例如从自动装配的 Tracer)?有没有一种干净的方法(或现有的类)来做到这一点?

spring-boot spring-cloud feign spring-cloud-netflix spring-cloud-sleuth

5
推荐指数
1
解决办法
5403
查看次数

Spring Sleuth 和 Zipkin:Could not find artifact io.zipkin.brave:brave-bom:pom:4.16.3-SNAPSHOT

我有一个 Spring Boot 2.0.0 REST 服务,我正在尝试启用 Sleuth 和 Zipkin 将跟踪发送到我的本地主机 Zipkin 服务器。

该应用程序运行良好spring-cloud-starter-sleuth,spring-cloud-sleuth-zipkin直到我将两个依赖项添加到我的 pom.xml 中。一旦我这样做了,我现在收到一个编译错误:

项目构建错误:不可解析的导入 POM:找不到工件 io.zipkin.brave:brave-bom:pom:4.16.3-SNAPSHOT

我通过删除我的 .m2 文件夹并更新(两次)来确保它不是损坏的 Maven 包问题。

为什么我会收到此错误,我该如何解决?

这是我的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.urig</groupId>
    <artifactId>inventory</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>inventory</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.M8</spring-cloud.version>
        <sleuth.version>2.0.0.M8</sleuth.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId> …
Run Code Online (Sandbox Code Playgroud)

java maven zipkin spring-cloud spring-cloud-sleuth

5
推荐指数
1
解决办法
2343
查看次数

使用 Spring Cloud Gateway 的简单网关 - ProxyExchange

我想使用 spring cloud 编写一个简单的网关,以便对第三方的请求似乎来自我的服务(就像我过去对 Zuul 所做的那样)。

github 页面上的示例以及官方文档中的示例似乎正是我想要的。即一个简单的控制器路由将所有请求代理到给定路由:

@GetMapping("/proxy/path/**")
public ResponseEntity<?> proxyPath(ProxyExchange<?> proxy) throws Exception {
  String path = proxy.path("/proxy/path/");
  return proxy.uri(home.toString() + "/foos/" + path).get();
}
Run Code Online (Sandbox Code Playgroud)

但我遇到的问题是,人工制品spring-cloud-starter-gateway不包括org.springframework.cloud.gateway.mvc.ProxyExchange.

为了获得ProxyExchange,我需要包含spring-cloud-gateway-mvc与spring-cloud-starter-gateway. 将这两个东西放在一起会导致以下错误:

***************************************************
Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway 
at this time. Please remove spring-boot-starter-web dependency.
***************************************************
Run Code Online (Sandbox Code Playgroud)

因此,我不得不使用以下两个依赖项来使基本网关正常工作:

  • spring-cloud-gateway
  • spring-cloud-gateway-mvc

哪个工作正常,直到我需要org.springframework.cloud.gateway.filter.*在项目中的其他地方使用这些包,这意味着我需要包括:

  • spring-cloud-gateway-core, 或者
  • spring-cloud-starter-gateway

这意味着我又回到了不兼容横幅。


无论如何,是否可以仅使用spring-cloud-starter-gateway依赖项来使用简单的 …

spring-cloud spring-cloud-gateway

5
推荐指数
0
解决办法
6266
查看次数

问题测试 spring cloud SQS 监听器

环境

  • Spring Boot:1.5.13.RELEASE
  • 云:Edgware.SR3
  • 云 AWS:1.2.2.RELEASE
  • 爪哇 8
  • OSX 10.13.4

问题

我正在尝试为 SQS 编写集成测试。

我有一个本地运行的localstack docker容器,其中运行 SQSTCP/4576

在我的测试代码中,我定义了一个端点设置为本地 4576 的 SQS 客户端,并且可以成功连接并创建队列、发送消息和删除队列。我还可以使用 SQS 客户端来接收消息并提取我发送的消息。

我的问题是,如果我删除手动接收消息的代码以允许另一个组件获取消息,似乎什么也没有发生。我有一个弹簧组件注释如下:

听众

@Component
public class MyListener {
@SqsListener(value = "my_queue", deletionPolicy = ON_SUCCESS)
    public void receive(final MyMsg msg) {
        System.out.println("GOT THE MESSAGE: "+ msg.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

测试

@RunWith(SpringRunner.class)
@SpringBootTest(properties = "spring.profiles.active=test")
public class MyTest {

    @Autowired
    private AmazonSQSAsync amazonSQS;

    @Autowired
    private SimpleMessageListenerContainer container;

    private String queueUrl;

    @Before
    public void setUp() {
        queueUrl = …
Run Code Online (Sandbox Code Playgroud)

spring-cloud spring-cloud-aws

5
推荐指数
1
解决办法
3797
查看次数

Spring云网关无法从尤里卡服务器解析服务ID

按照spring 指南构建 eureka 服务器、spring 云网关和示例休息服务。

但是,网关无法使用 eureka 服务器中的服务名称检索 url。网关和服务的注册似乎没问题。当提供实际端点而不是服务 id 时,它工作正常。我无法理解为什么网关没有从尤里卡解析服务 ID。我是否缺少任何配置?

错误信息:

2018-09-05 23:20:17.751  INFO 47037 --- [ctor-http-nio-2] c.netflix.loadbalancer.BaseLoadBalancer  : Client: localhost instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=localhost,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2018-09-05 23:20:17.756  INFO 47037 --- [ctor-http-nio-2] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2018-09-05 23:20:17.760  INFO 47037 --- [ctor-http-nio-2] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client localhost initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=localhost,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@107dc063
2018-09-05 23:20:17.822 ERROR 47037 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : …
Run Code Online (Sandbox Code Playgroud)

spring-boot spring-cloud netflix-eureka spring-cloud-gateway

5
推荐指数
1
解决办法
3454
查看次数

WireMock 使用错误的 __files 目录进行多目录映射配置

我正在使用 Spring Bootspring-cloud-contract-wiremock和com.github.tomakehurst.wiremock依赖项。我的线模定义存储在 json 文件中。像那样:

directoryA/mappings/detail-mapping-123.json:

{
  "request" : {
    "urlPath" : "/detail/123",
    "method" : "GET"
  },
  "response" : {
    "status" : 200,
    "bodyFileName" : "detail.json",
    "headers" : {
      "Content-Type" : "application/json;charset=UTF-8"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

directoryA/__files/detail.json:

{
  "id": "123",
  "name": "name-123"
}
Run Code Online (Sandbox Code Playgroud)

directoryB/mappings/search-mapping-123.json:

{
  "request" : {
    "urlPath" : "/service/usa/search",
    "queryParameters" : {
      "query": {
        "equalTo": "123"
      }
    },
    "method" : "GET"
  },
  "response" : {
    "status" : 200,
    "bodyFileName" : "search-123.json",
    "headers" …
Run Code Online (Sandbox Code Playgroud)

java junit wiremock spring-cloud spring-cloud-contract

5
推荐指数
1
解决办法
3426
查看次数