eureka server 单节点与多节点部署演示

邮差的信 提交于 2021-02-09 20:01:17

环境搭建

目录结构(ad-eureka为子模块)

  --ad-spring-cloud

   --ad-eureka

     --pom.xml

   --pom.xml

主pom.xml

  

<?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.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <modules>
        <module>ad-eureka</module>
    </modules>

    <groupId>com.me</groupId>
    <artifactId>ad-spring-cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>ad-spring-cloud</name>
    <description> project for Ad Spring-Cloud</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>
View Code

 

ad-eureka下的pom.xml

        

<?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>com.me</groupId>
        <artifactId>ad-spring-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>ad-eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ad-eureka</name>
    <description>project for ad-eureka</description>

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

</project>
View Code

 

 

单节点配置

ad-eureka的yml配置

      

# 单节点eureka配置
spring:
  application:
    name: ad-eureka-server

server:
  port: 8000

eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: false 
    register-with-eureka: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka

 单节点效果

 

 

多节点配置

  说明:由于我们在一台机器一个工程上模拟多个节点,我们需要给springboot应用配置profiles属性用来标识,并且打包成jar

  包之后,我们启动指定profiles的应用

修改hosts文件(单机单应用上模拟多节点所需要)

    

    127.0.0.1 server1
    127.0.0.1 server2
    127.0.0.1 server3

ad-eureka的yml配置

     

---
spring:
  application:
    name: ad-eureka
  profiles: server1
server:
  port: 8000
eureka:
  instance:
    hostname: server1
    prefer-ip-address: false
  client:
    service-url:
    #将该eureka注册到server2和server3
      defaultZone: http://server2:8001/eureka,http://server3:8002/eureka

---
spring:
  application:
    name: ad-eureka
  profiles: server2
server:
  port: 8001
eureka:
  instance:
    hostname: server2
    prefer-ip-address: false
  client:
    service-url:
    #将该eureka注册到server1和server3
      defaultZone: http://server1:8000/eureka,http://server3:8002/eureka

---
spring:
  application:
    name: ad-eureka
  profiles: server3
server:
  port: 8002
eureka:
  instance:
    hostname: server3
    prefer-ip-address: false
  client:
    service-url:
    #将该eureka注册到server1和server2
      defaultZone: http://server1:8000/eureka,http://server2:8001/eureka

 打包并启动

  E:\IdeaProjects\ad-spring-cloud> mvn clean package -D maven.skip.test=true -U

  分别启动指定的profiles的应用

  E:\IdeaProjects\ad-spring-cloud\ad-eureka\target> java -jar .\ad-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=server1

  E:\IdeaProjects\ad-spring-cloud\ad-eureka\target> java -jar .\ad-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=server2

  E:\IdeaProjects\ad-spring-cloud\ad-eureka\target> java -jar .\ad-eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=server3

 

 多节点效果

 

写在最后

  至此,在本机上演示完成了单节点和多节点的eureka的部署,当然在实际应用中,多节点的部署是部署到多个服务器上的,这样其实更为简单,我们就省去了修改hosts文件的步骤,只需要将每个eureka应用工程的defaultZone改为其余节点的ip地址加端口号即可。

  

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!