SpringCloud-Eureka-集群式搭建

一、介绍

(1)Eureka集群式搭建优点:避免单点故障。
(2)Eureka服务端如果是非集群式搭建的话,就不需要将其注册至服务注册中心中,如果是集群式搭建的话,各个服务端实例需要分别注册到其他服务端实例中。

二、服务注册中心7001模块搭建

(1)修改host文件
SpringCloud-Eureka-集群式搭建_第1张图片

(2)编写yml文件

server:
  port: 7001

eureka:
  instance:
    hostname: eureka1.com
  client:
#    服务端设置为false,表明该实例作为服务注册中心
    register-with-eureka: false
#    服务端设置为false,表明该实例作为服务注册中心
    fetch-registry: false
    service-url:
      defaultZone: http://eureka2.com:7002/eureka/

三、服务注册中心7002模块搭建

(1)SpringCloud-Eureka-集群式搭建_第2张图片
(2)编写pom文件


<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">
    <parent>
        <artifactId>demo20220821artifactId>
        <groupId>com.wsh.springcloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud-eureka-server7002artifactId>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
        
        <dependency>
            <groupId>com.wsh.springcloudgroupId>
            <artifactId>cloud-api-commonartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
        dependency>
    dependencies>
project>

(3)编写yml文件

server:
  port: 7002

eureka:
  instance:
    hostname: eureka2.com
  client:
#    服务端设置为false,表明该实例作为服务注册中心
    register-with-eureka: false
#    服务端设置为false,表明该实例作为服务注册中心
    fetch-registry: false
    service-url:
      defaultZone: http://eureka1.com:7001/eureka/

(4)编写启动类
SpringCloud-Eureka-集群式搭建_第3张图片

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

(5)分别访问http://eureka1.com:7001和http://eureka2.com:7002
SpringCloud-Eureka-集群式搭建_第4张图片
SpringCloud-Eureka-集群式搭建_第5张图片

四、将支付、订单模块注册入集群式的Eureka服务注册中心

(1)订单模块配置pom
SpringCloud-Eureka-集群式搭建_第6张图片
(2)订单模块编写yml

server:
  port: 80

spring:
  application:
    name: cloud-order-service

eureka:
  client:
    #    客户端设置为true
    register-with-eureka: true
    #    客户端设置为true
    fetch-registry: true
    service-url:
      #      defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka1.com:7001/eureka, http://eureka2.com:7002/eureka

(3)订单模块编写启动类

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

(4)分别访问http://eureka1.com:7001和http://eureka2.com:7002
SpringCloud-Eureka-集群式搭建_第7张图片
SpringCloud-Eureka-集群式搭建_第8张图片

你可能感兴趣的:(Eureka,SpringCloud,eureka,spring,cloud)