本文介绍了Eureka功能区LoadBalancer缓存更新延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个基于微服务的应用程序,其中聚合层/API网关对微服务进行调用. Eureka用于服务发现,Ribbon用于提供负载平衡RestTemplate.

I am setting up a micro service based application, where Aggregation layer / API gateway makes calls to micro services. Eureka is used for service discovery and Ribbon for providing a load balancing RestTemplate.

邮递员调用聚合->聚合使用Eureka/Ribbon/RestTemplate调用微服务.

Postman calls Aggregation--> Aggregation calls Micro service using Eureka/Ribbon/RestTemplate.

我在计算机上的4个不同端口上运行着4种微服务类型的实例.重复击中相同的REST终结点Postman使请求以循环方式正确地实现负载均衡.

I have 4 instances of one micro services type running on my machine on 4 different ports. Hitting the same REST endpoint repeatedly Postman causes the requests to get load balanced properly in a round robin fashion.

当我停止其中一个微服务实例时,该服务已从Eureka注销,但是LoadBalancer仍然向死服​​务发送请求,并且呼叫失败.

When I stop one of the micro service instances the service is deregistered from Eureka, but the LoadBalancer still sends requests to the dead service and the call fails.

下面是我的代码:

汇总:

 @Configuration
    @ComponentScan(basePackages = {"com.mycompany.aggregator"})
    @EnableAutoConfiguration
    @EnableEurekaClient
    public class AggregatorApplication {

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

**Configuration:**
@Configuration
public class AggregatorConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

@Configuration
@RibbonClient(name="microservice", configuration = FooConfig.class)
public class TestConfig {
}

//FooConfig从组件扫描中排除

//FooConfig is excluded from component-scan

@Configuration
public class FooConfig {

    @Bean
    public IPing ribbonPing(IClientConfig config) {
        return new NIWSDiscoveryPing();
    }

    @Bean
    public IRule ribbonRule(IClientConfig config) {
        return new AvailabilityFilteringRule();
    }
}

restTemplate调用:

restTemplate call:

ResponseEntity<Object> responseEntity = restTemplate.getForEntity(myUrl, Object.class);

属性:

spring.application.name=aggregator
server.contextPath=/ott
server.port = 8090
my.url=http://microservice
eureka.instance.leaseRenewalIntervalInSeconds=1
eureka.instance.leaseExpirationDurationInSeconds=2

微服务代码:

@SpringBootApplication
@EnableEurekaClient
public class MicroServiceApplication

属性:

spring.application.name=microservice
server.contextPath=/ott
server.port = 9000
eureka.instance.leaseRenewalIntervalInSeconds=1
eureka.instance.leaseExpirationDurationInSeconds=2

Eureka服务器:

@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {

属性:

server.port=8761

eureka.server.enableSelfPreservation=false
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false

logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

推荐答案

导致此问题的原因是Eureka intellisense给了关闭实例更多的时间.智能感知以这种方式适用于在同一盒子上运行的多个具有相同名称的微服务.

The issue causing this is that Eureka intellisense gives the shutdown instance more time to come up. The intellisense works this way for multiple microservices with the same name running on the same box.

将服务部署在其他设备上时,不会出现此问题.

When the services are deployed on different boxes then this issue doesn't come up.

这篇关于Eureka功能区LoadBalancer缓存更新延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 11:31