Eureka高可用的服务注册中心

我们知道,如果要注册服务,需要向Eureka Server进行注册,但是当成千上万个服务进行注册的时候,开销是比较大的,Eureka Server的负载太高,所以我们需要一个高可用的服务注册中心。原则就是运行多个Eureka Server实例,其实就是用冗余的注册中心来保证可靠性,如果有一个注册中心宕机,因为还有好几个,所以服务不会终止。

下面来看代码:

文件目录

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>NewEurekaServer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application-peer1.yml

server:
  port: 8761
spring:
  profiles: peer1
  application:
    name: eureka-server
eureka:
  instance:
    hostname: peer1
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://peer2:8769/eureka/

application-peer2.yml

server:
  port: 8769
spring:
  profiles: peer2
  application:
    name: eureka-server
eureka:
  instance:
    hostname: peer2
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://peer1:8761/eureka/

NewEurekaServerApplication

package com.example.demo;

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

@SpringBootApplication
@EnableEurekaServer
public class NewEurekaServerApplication {

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

}

这样运行是会报错的,我们需要加一个application.yml

spring:
  profiles:
    active: peer1

然后

去掉勾选,这样就可以运行多个实例了,第一次运行的时候applicaiton.yml里的active是peer1,第二次是peer2,这样在运行服务,就可以了。

例如,我运行了一个服务service-hi,他的appliction.yml是

eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: service-hi

这个时候,访问http://localhost:8761/

再访问http://localhost:8769/   你发现已经自动注册了那个服务

 

 目的达到了。这样即便是一个server挂了,还有其他的,依然能保证运行。

猜你喜欢

转载自blog.csdn.net/qq_41076797/article/details/89056927
今日推荐