首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用eureka的spring故障转移并动态部署多个eureka实例

使用eureka的spring故障转移并动态部署多个eureka实例
EN

Stack Overflow用户
提问于 2016-07-06 02:28:56
回答 1查看 1.9K关注 0票数 2

最近,我一直在从事一个API网关项目。但是,对于服务注册和发现,我遇到了一些问题,涉及Zuul故障转移和多个Eureka实例的动态部署。以下是我的项目。

eureka服务器项目

EurekaBootstrap.java

代码语言:javascript
复制
package com.plateno.cloud.netflix.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaBootstrap {
public static void main(String[] args) {
    SpringApplication.run(EurekaBootstrap.class, args);
 }
}

config:application.yml

代码语言:javascript
复制
#spring.application.name=plateno-cloud-netflix-eureka
#server.port=9000
#eureka.instance.hostname=localhost
#eureka.client.registerWithEureka=false
#eureka.client.fetchRegistry=false
#eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
#spring.profiles.active: dev

server:
    port: 9000
spring.application.name: localhost
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/
  instance:
    hostname: localhost
eureka.client.register-with-eureka: false
eureka.client.fetch-registry: false
---
spring:
    profiles: peer1
server:
    port: 9001
peer2:
    port: 9002    
spring.application.name: peer1
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer2:${peer2.port}/eureka/  
  instance:
    hostname: peer1
eureka.client.register-with-eureka: true
eureka.client.fetch-registry: true
---
spring:
    profiles: peer2
server:
    port: 9002
spring.application.name: peer2
peer1:
    port: 9001 
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:${peer1.port}/eureka/
  instance:
    hostname: peer2
eureka.client.register-with-eureka: true
eureka.client.fetch-registry: true
---
spring:
  profiles:
    active: peer1

I将peer1和peer2配置为主机中的本地主机。

zuul与丝带项目

ZuulBootstrap.java

代码语言:javascript
复制
package com.plateno.cloud.netflix.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapper;
import org.springframework.context.annotation.Bean;

import com.netflix.zuul.exception.ZuulException;

@SpringBootApplication
@EnableZuulProxy
public class ZuulBootstrap {
    public static void main(String[] args) {
        SpringApplication.run(ZuulBootstrap.class, args);
    }

    @Bean
    public SimpleFilter simpleFilter() {
        return new SimpleFilter();
    }

    @Bean
    public PatternServiceRouteMapper serviceRouteMapper() {
        return new PatternServiceRouteMapper("(?<name>^.+)", "${name}") {
            @Override
            public String apply(final String serviceId) {
                String route = super.apply(serviceId);
                return route;
            }
        };
    }

    @RefreshScope
    @ConfigurationProperties("zuul")
    public ZuulProperties zuulProperties() {
        ZuulProperties zuulProperties = new ZuulProperties();
        return zuulProperties;
    }
}

SimpleFilter.java PreFilter: PreFilter

代码语言:javascript
复制
package com.plateno.cloud.netflix.zuul;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

public class SimpleFilter extends ZuulFilter {

    private static Logger log = LoggerFactory.getLogger(SimpleFilter.class);

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
        return null;
    }
}

zuul config:application.yml

代码语言:javascript
复制
#default_charset=utf-8
#spring.application.name=plateno-cloud-netflix-zuul
#server.port=9030
#ribbon.eureka.enabled=true
#eureka.client.serviceUrl.defaultZone=http\://localhost\:9001/eureka/

---
default_charset: utf-8
spring:
  application:
    name: plateno-cloud-netflix-zuul
server:
  port: 9030
ribbon: 
  eureka:
    enabled: true

eureka:
  client:
    service-url:
      defaultZone: http://localhost:9001/eureka/,http://localhost:9002/eureka/

目标服务项目只是带有yml config的公共项目

我没有解析这个项目的源代码,但是解析了配置配置文件

代码语言:javascript
复制
#default_charset=utf-8
#spring.application.name=plateno-app0
#server.port=9010
#eureka.client.serviceUrl.defaultZone=http\://localhost\:9000/eureka/
server:
    port: 8080
default_charset: utf-8
spring:
  application:
    name: plateno-app0

eureka.client.serviceUrl.defaultZone: http://peer1:9001/eureka/,http://peer2:9002/eureka/

端的源码,附件是源代码

我通过更改active启动了两个eureka实例,然后启动了zuul服务器。最后,我通过更改服务器端口启动了2个目标服务。我使用JMeter运行了一些测试。我在并发测试中停止了一个目标服务,然后一些关于套接字关闭的请求失败了。

所以问题是为什么zuul和丝带不能处理故障转移,或者我应该如何配置才能达到这种效果?

第二个问题是,每次我部署另一个eureka服务器时,我都需要编辑eureka服务器配置、zuul配置和目标服务配置。因此,所有这些项目都需要重新启动。我不能接受这一点,那么是否有一种方法可以在不编辑其他项目的配置并重新启动的情况下动态地更改eureka服务器的数量?

最后,我希望关键团队能够更新spring的文档,以提供更详细的信息。

EN

回答 1

Stack Overflow用户

发布于 2021-05-31 19:33:10

您必须为所有客户端应用程序提供所有实例(逗号分隔列表),如下所示- bootstrap.properties- eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka

因为如果只让eureka服务器彼此了解,那么它们就可以复制所有的实例,这是一个完美的部分。现在假设在客户端应用程序中,如果您只传递一个实例假设-- eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka,并且假设这个应用程序将重新启动,并且此时您的eureka实例已经关闭,那么在这种情况下,它会检查另一个eureka服务器实例,但是您如何在客户端应用程序中只提供了一个实例。

因此,您必须在客户端应用程序中提供所有eureka服务器实例。谢谢

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38215539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档