使用 Spring 云 Netflix OSS 的负载平衡微服务

Load Balancing micro services using Spring cloud Netflix OSS

我有一个使用 Spring Boot 和 Netflix OSS 构建的微服务。我用过 Central 配置服务器、Eureka 和 Zuul。由于可扩展性,服务的多个实例在不同端口上 运行。所有实例都在 Eureka 中注册,但请求只会发送到最后注册的服务器。

如何负载均衡服务。我应该在 Zuul 中使用功能区来负载平衡吗?请告诉我如何在多个实例上实现同一服务 运行 的负载平衡。

如果代码更改需要一个代码片段,那么请 post 也提供一个代码片段。

应用程序配置

spring.application.name=book-service
server.port=0

eureka.client.region = default
eureka.client.registryFetchIntervalSeconds = 5
eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:10082/eureka/

#eureka.instance.metadataMap.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}
eureka.instance.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}
eureka.instance.leaseRenewalIntervalInSeconds=5
eureka.instance.leaseExpirationDurationInSeconds=5

尤里卡配置

spring.application.name=discovery
server.port=10082

eureka.instance.hostname=localhost

eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:10082/eureka/
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

spring.session.store-type=hash-map

ZUUL 配置

spring.application.name=gateway
server.port=10080

eureka.client.region = default
eureka.client.registryFetchIntervalSeconds = 5

management.security.sessions=always

zuul.routes.book-service.path=/book-service/**
zuul.routes.book-service.sensitive-headers=Set-Cookie,Authorization
hystrix.command.book-service.execution.isolation.thread.timeoutInMilliseconds=600000

#zuul.routes.rating-service.path=/rating-service/**
#zuul.routes.rating-service.sensitive-headers=Set-Cookie,Authorization
#hystrix.command.rating-service.execution.isolation.thread.timeoutInMilliseconds=600000

zuul.routes.discovery.path=/discovery/**
zuul.routes.discovery.sensitive-headers=Set-Cookie,Authorization
zuul.routes.discovery.url=http://localhost:8082
hystrix.command.discovery.execution.isolation.thread.timeoutInMilliseconds=600000

logging.level.org.springframework.web.=debug
logging.level.org.springframework.security=debug
logging.level.org.springframework.cloud.netflix.zuul=debug

spring.session.store-type=hash-map

使用 Eureka 正确注册应用程序即可。

以下条目将唯一地向 Eureka 注册服务。

#eureka.instance.metadataMap.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}
eureka.instance.instanceId=${spring.application.name}:${spring.application.instance_id:${random.value}}