Eureka combat support Remote Region [-3]

Pom dependent on public works

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>

 

1, eureka server project

1.1, eureka server project pom-dependent:

<! - plus the head of the public rely article -> 

<the Dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

 

1.2, the project started categories:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurkeaServerApplication {

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

 

1.3, where four eureka server configuration example, the path: eureka-server \ src \ main \ resources \, Zone 4 points, belonging to the region-east, region-west two regions.

Belonging to the region-east profile for: application-zone1.yml, application-zone2.yml

application-zone1.yml:

server:
  port: 8761
spring:
  application:
    name: eureka-server
eureka:
  server:
    waitTimeInMsWhenSyncEmpty: 0
    enableSelfPreservation: false
    remoteRegionUrlsWithName:
      region-west: http://localhost:8763/eureka/
  client:
    register-with-eureka: true
    fetch-registry: true
    region: region-east
    service-url:
      zone1: http://localhost:8761/eureka/
      zone2: http://localhost:8762/eureka/
    availability-zones:
      region-east: zone1,zone2
  instance:
    hostname: localhost
    metadataMap.zone: zone1

application-zone2.yml:

server:
  port: 8762
spring:
  application:
    name: eureka-server
eureka:
  server:
    waitTimeInMsWhenSyncEmpty: 0
    enableSelfPreservation: false
    remoteRegionUrlsWithName:
      region-west: http://localhost:8763/eureka/
  client:
    register-with-eureka: true
    fetch-registry: true
    region: region-east
    service-url:
      zone1: http://localhost:8761/eureka/
      zone2: http://localhost:8762/eureka/
    availability-zones:
      region-east: zone1,zone2
  instance:
    hostname: localhost
    metadataMap.zone: zone2

 

 

It belongs to region-west profile for: application-zone3-region-west.yml, application-zone4-region-west.yml

application-zone3-region-west.yml

server:
  port: 8763
spring:
  application:
    name: eureka-server
eureka:
  server:
    waitTimeInMsWhenSyncEmpty: 0
    enableSelfPreservation: false
    remoteRegionUrlsWithName:
      region-east: http://localhost:8761/eureka/
  client:
    register-with-eureka: true
    fetch-registry: true
    region: region-west
    service-url:
      zone3: http://localhost:8763/eureka/
      zone4: http://localhost:8764/eureka/
    availability-zones:
      region-west: zone3,zone4
  instance:
    hostname: localhost
    metadataMap.zone: zone3

application-zone4-region-west.yml

server:
  port: 8764
spring:
  application:
    name: eureka-server
eureka:
  server:
    waitTimeInMsWhenSyncEmpty: 0
    enableSelfPreservation: false
    remoteRegionUrlsWithName:
      region-east: http://localhost:8761/eureka/
  client:
    register-with-eureka: true
    fetch-registry: true
    region: region-west
    service-url:
      zone3: http://localhost:8763/eureka/
      zone4: http://localhost:8764/eureka/
    availability-zones:
      region-west: zone3,zone4
  instance:
    hostname: localhost
    metadataMap.zone: zone4

 

Due to the framework, remoteRegionAppWhitelist EurekaServerConfigBean default value is null, and getRemoteRegionAppWhitelist (String regionName) method has been called directly, if not addressed on the project, a direct null pointer exception.

// frame Source 
Package Penalty for
org.springframework.cloud.netflix.eureka.server; import ...... @ConfigurationProperties("eureka.server") public class EurekaServerConfigBean implements EurekaServerConfig { public static final String PREFIX = "eureka.server"; private static final int MINUTES = 60000; @Autowired( required = false ) PropertyResolver propertyResolver; private String aWSAccessId; //.....private String[] remoteRegionUrls; private Map<String, Set<String>> remoteRegionAppWhitelist;
  //......
}

so, add the following configuration in eureka server project:

import com.netflix.discovery.EurekaClientConfig;
import com.netflix.eureka.EurekaServerConfig;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration;
import org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * Configuration class
 */
@Configuration
@AutoConfigureBefore (EurekaServerAutoConfiguration. Class ) // current configuration subsequent load category EurekaServerAutoConfiguration loading operation after completion 
public  class RegionConfig {

    @Bean
    @ConditionalOnMissingBean
    public EurekaServerConfig eurekaServerConfig(EurekaClientConfig clientConfig) {
        EurekaServerConfigBean server = new EurekaServerConfigBean();
        if (clientConfig.shouldRegisterWithEureka()) {
            server.setRegistrySyncRetries(5);
        }
        server.setRemoteRegionAppWhitelist(new HashMap<>());
        return server;
    }
}

 

1.4, start an instance, execute the command:

mvn spring-boot:run -Dspring.profiles.active=zone1
mvn spring-boot:run -Dspring.profiles.active=zone2
mvn spring-boot:run -Dspring.profiles.active=zone3-region-west
mvn spring-boot:run -Dspring.profiles.active=zone4-region-west

 

2, Eureka Client Project

2.1, eureka client project pom-dependent:

<! - plus the head of the public rely article -> 

<the Dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

 

2.2, eureka client project started categories:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

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

 

2.3, eureka client project profile

Here client configuration example 4, Zone is divided into four, two areas belonging to region-east, region-west. 

Belonging to the region-east profile for: application-zone1.yml, application-zone2.yml

application-zone1.yml:

server:
  port: 8071
spring:
  application.name: demo-client
eureka:
  client:
    prefer-same-zone-eureka: true
    region: region-east
    service-url:
      zone1: http://localhost:8761/eureka/
      zone2: http://localhost:8762/eureka/
    availability-zones:
      region-east: zone1,zone2
  instance:
    metadataMap.zone: zone1

application-zone2.yml

server:
  port: 8072
spring:
  application.name: demo-client
eureka:
  client:
    prefer-same-zone-eureka: true
    region: region-east
    service-url:
      zone1: http://localhost:8761/eureka/
      zone2: http://localhost:8762/eureka/
    availability-zones:
      region-east: zone1,zone2
  instance:
    metadataMap.zone: zone2

 

Belonging to the region-west profile for: application-zone3.yml, application-zone4.yml

application-zone3.yml:

server:
  port: 8073
spring:
  application.name: demo-client
eureka:
  client:
    prefer-same-zone-eureka: true
    region: region-west
    service-url:
      zone3: http://localhost:8763/eureka/
      zone4: http://localhost:8764/eureka/
    availability-zones:
      region-west: zone3,zone4
  instance:
    metadataMap.zone: zone3

application-zone4.yml:

server:
  port: 8074
spring:
  application.name: demo-client
eureka:
  client:
    prefer-same-zone-eureka: true
    region: region-west
    service-url:
      zone3: http://localhost:8763/eureka/
      zone4: http://localhost:8764/eureka/
    availability-zones:
      region-west: zone3,zone4
  instance:
    metadataMap.zone: zone4

 

application.yml:

management:
  endpoints:
    web:
      exposure:
        include: '*'

 

2.4, start eureka client project, execute the command:

mvn spring-boot:run -Dspring.profiles.active=zone1
mvn spring-boot:run -Dspring.profiles.active=zone2
mvn spring-boot:run -Dspring.profiles.active=zone3
mvn spring-boot:run -Dspring.profiles.active=zone4

 

3, Zuul Gateway Project

3.1, zuul gateway project pom-dependent:

<! - plus the head of the public rely article -> 

<the Dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

 

3.2, zuul gateway project started categories:

package cn.springcloud.book;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public  class ZuulGatewayApplication {

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

 

3.3, zuul gateway project profile, as used herein, examples of two gateway fallback to the application example to demonstrate the function of the remote region, a profile belonging to region-east, belonging to a region-west.

application.yml:

spring:
  application:
    name: zuul-gateway
management:
  endpoints:
    web:
      exposure:
        include: '*'

application-zone1.yml:

server:
  port: 10001
eureka:
  instance:
    metadataMap.zone: zone1
  client:
    register-with-eureka: true
    fetch-registry: true
    region: region-east
    service-url:
      zone1: http://localhost:8761/eureka/
      zone2: http://localhost:8762/eureka/
    availability-zones:
      region-east: zone1,zone2

application-zone3-region-west.yml:

server:
  port: 10002
eureka:
  instance:
    metadataMap.zone: zone3
  client:
    register-with-eureka: true
    fetch-registry: true
    region: region-west
    service-url:
      zone3: http://localhost:8763/eureka/
      zone4: http://localhost:8764/eureka/
    availability-zones:
      region-west: zone3,zone4

 

3.4, start the gateway project, execute the command:

mvn spring-boot:run -Dspring.profiles.active=zone1
mvn spring-boot:run -Dspring.profiles.active=zone3-region-west

Access: localhost: 10001 / demo-client / actuator / env, was as follows:

 

 Access: localhost: 10002 / demo-client / actuator / env, was as follows:

 

 You can see ZoneAffinity characteristics ( on an article about speaking ), zone1 the gateway access is zone1 of demo-client, zone3 the gateway access is zone3 of demo-client.

Next, turn off zone1 instance eureka-client and continue accessing localhost: 10001 / demo-client / actuator / env, you can see after a few errors, automatic fallback to the zone4 instance remote-region to achieve a similar off-site live request automatically transfer efficiency.

 

Guess you like

Origin www.cnblogs.com/idoljames/p/11620971.html