Spring Cloud Zuul,Spectator和Atlas config

时间:2016-03-07 15:50:54

标签: spring-cloud netflix-zuul

我在本地计算机上玩这些奇妙的Spring Cloud内容。我有一个基于Zuul的API网关发现并调用Spring Data Rest服务。效果很好。

现在我想介绍一下Spectator和Atlas。我似乎无法正确配置此配置。如果我在Zuul服务上启用Atlas,则Atlas客户端的RestTemplate会失败,因为Ribbon会尝试将其包装起来,并且Ribbon不知道" atlas"。如果我为Atlas添加Ribbon Client配置,那么我的API网关调用会中断。有没有办法从Ribbon包装中排除atlas路线?我错过了什么?见下文:

我已经定义了一个主机名" atlas"指向localhost。 Atlas服务器正在运行。

Zuul / Api网关依赖项:

spring-boot-starter-parent: 1.3.2.RELEASE
spring-boot-starter-actuator
spring-cloud-starter-config
spring-cloud-starter-eureka
spring-cloud-starter-hystrix
spring-cloud-starter-zuul
spring-cloud-starter-stream-rabbit
spring-boot-starter-hateoas
spring-boot-starter-test
spring-cloud-starter-spectator
spring-cloud-starter-atlas

Zuul / API网关应用属性 -

hystrix.command.default.execution.isolation.strategy: SEMAPHORE
netflix.atlas.uri=http://atlas:7101/api/v1/publish
server.port: ${PORT:8080}

API网关启动属性:

spring.application.name=reservationClient
spring.cloud.config.uri=http://localhost:8888

Zuul / Eureka / Ribbon / Hystrix客户端都按预期工作,但Atlas客户端失败。

@SpringBootApplication
@EnableZuulProxy
@EnableHystrix
@EnableEurekaClient
@EnableAtlas
@EnableScheduling
public class APIGateway {

    @Autowired
    Registry registry;

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

Atlas客户端由Ribbon包装,Ribbon不知道Atlas

2016-03-07 08:57:50.312 ERROR 184 --- [trace=,span=] [ask-scheduler-2] o.s.integration.handler.LoggingHandler   : java.lang.IllegalStateException: No instances available for atlas
    at org.springframework.cloud.netflix.ribbon.RibbonClientHttpRequestFactory.createRequest(RibbonClientHttpRequestFactory.java:58)
    at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:77)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:592)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475)
    at org.springframework.cloud.netflix.metrics.atlas.AtlasMetricObserver.sendMetricsBatch(AtlasMetricObserver.java:148)
    at org.springframework.cloud.netflix.metrics.atlas.AtlasMetricObserver.update(AtlasMetricObserver.java:126)
    at org.springframework.cloud.netflix.metrics.atlas.AtlasExporter.export(AtlasExporter.java:35)
    at org.springframework.boot.actuate.metrics.export.MetricExporters$ExportRunner.run(MetricExporters.java:112)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

好的,让我们为Atlas定义一个Ribbon客户端。

@SpringBootApplication
@EnableZuulProxy
@EnableHystrix
@EnableEurekaClient
@EnableAtlas
@EnableScheduling
public class APIGateway {

    @Autowired
    Registry registry;

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

@Component
public class AtlasClientConfiguration {
    @Bean
    public StaticServerList ribbonServerList() {
        return new StaticServerList<Server>(new Server("atlas", 7101));
    }
}

现在Atlas客户很高兴。我看到了Atlas服务器的POST 但是现在我通过Zuul向我的Spring Data服务提出的GET请求,现在已经失败了:

&#34;不允许使用HTTP方法,支持的方法:POST&#34;

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

我偶然发现了解决方案。

AtlasClientConfiguration不是Component,它只是一个提供StaticServerList的Bean。删除了@Component。

我需要明确命名我的RibbonClient配置。即 @RibbonClient(name =“atlas”,configuration = AtlasClientConfiguration.class)

@SpringBootApplication
@EnableZuulProxy
@EnableHystrix
@EnableEurekaClient
@RibbonClient(name = "atlas", configuration = AtlasClientConfiguration.class)
@EnableAtlas
@EnableScheduling
public class APIGateway {

    @Autowired
    Registry registry;

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


public class AtlasClientConfiguration {

    @Bean
    public StaticServerList<Server> ribbonServerList() {
        return new StaticServerList<Server>(new Server("atlas", 7101));
    }
}