【问题标题】:Spring cloud: Ribbon and HTTPS WITHOUT EurekaSpring Cloud:没有 Eureka 的 Ribbon 和 HTTPS
【发布时间】:2015-12-12 17:51:52
【问题描述】:

我对 spring-cloud 比较陌生,所以我可能还没有找到所有文档 (Spring-Cloud documentation)。我找到了this stackoverflow entry,但不幸的是这对我没有帮助(或者我不明白答案)。

我使用的是 Spring-Boot 1.3-SNAPSHOT,这里包含 Spring-Cloud 1.0.3。

我想使用 Feign 和 Ribbon 来使用 REST Web 服务,但第一步不使用 Eureka、Hystrix 和 Zuul。

为此,我注释了 Client-Service 方法

@FeignClient("modelService")
public interface ProductModelService {...}

并放置类似

的配置
modelService.ribbon.listOfServers: localhost:8444

进入 application.properties 以在没有 Eureka 的情况下使用 Ribbon。

这适用于 HTTP,但我无法使用 HTTPS - 我无法找到使用 HTTPS 的正确配置功能区。

有类似的注释

@FeignClient("https://modelService")
public interface ProductModelService {...}

没有帮助。

有类似的配置

modelService.ribbon.isSecure=true

没有帮助。

如何配置 Ribbon 以使用 HTTPS 安全的 Rest Webservice?

【问题讨论】:

  • 可以试试最近发布的 Spring Cloud Brixton.M1 吗?
  • 目前我没有足够的时间,但也许下周我可以测试这个 - 目前我删除了 Feign Rest 客户端并用传统的 Spring RestTemplate 替换它。
  • 我刚刚设法让这个用例工作(ribbon & feign 但不是 Eureka 等),尽管与上述没有显着差异。如果您仍在尝试使其正常工作,我很乐意为您提供帮助。

标签: java spring spring-cloud netflix-feign


【解决方案1】:

最近我遇到了同样的问题,我用下面的配置解决了。

我在没有https前缀的客户端类上保留注释,例如:

@FeignClient("modelService")
public interface ProductModelService {...}

这个配置对我来说最终没用:

modelService.ribbon.isSecure=true

对我有帮助的主要因素是明确指出的 HTTPs 443 端口,如下所示:

modelService.ribbon.listOfServers: https://somedomain.com:443

【讨论】:

    【解决方案2】:

    我一直这样做的方式是使用自定义属性来确定在功能区客户端上是使用 http 还是 https。我没有使用 feign,所以我的示例使用 RibbonClient 代替,但是相同的方法应该适用于任何一个(?)。

    例子:

    package some.name.whatever;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.netflix.ribbon.RibbonClient;
    import org.springframework.web.client.RestTemplate;
    
    @RibbonClient(name = "random-service")
    public class MyClient {
    
        @Autowired
        @Qualifier("loadBalancedTemplate")
        private RestTemplate rest;
    
        @Value("${modelService.https}")
        private boolean https;
    
        private String getServiceUrl() {
    
            return (https ? "https" : "http") + "://random-service";
        }
    
        public Integer getRandomNumber() {
    
            String fullUrl = ;
    
            return rest.getForEntity(getServiceUrl()+"/randomnumber", Integer.class).getBody();
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-02
      • 2016-03-13
      • 2016-02-23
      • 2015-02-12
      • 2019-09-18
      • 2022-10-19
      • 2020-10-15
      • 1970-01-01
      • 2019-01-16
      相关资源
      最近更新 更多