Spring Cloud--Eureka 注册中心搭建(集群)

Eureka 注册中心搭建(集群)
  • 导入依赖
    导入依赖时注意下springcloud 和 springboot 的版本对应关系 可以在官网找到相关信息
    在这里插入图片描述
    假设选springcloud 的Hoxton SR3版本,
    在这里插入图片描述 springboot 可使用2.2.5.RELEASE版本
    在这里插入图片描述
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <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>
            <version>2.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <!-- springcloud 和springboot版本 需对应-->
                <version>Hoxton.SR3</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>

  • 配置文件
server:
  port: 8000
spring:
  application:
    name: spring-cloud-eureka
eureka:
  client:
    register-with-eureka: false # the default is true
    fetch-registry: false # This client gets the registration information on the eureka server registry, the default is true
    #注册中心路径,如果有多个eureka server,在这里需要配置其他eureka server的地址,
    #用","进行区分,如"http://address:8888/eureka,http://address:8887/eureka"
    service-url:
      defaultZone: http://localhost:8001/eureka/,http://localhost:8002/eureka/
  instance:
    # ip-address: 127.0.0.1
    hostname: port1
----

server:
  port: 8001
spring:
  application:
    name: spring-cloud-eureka
eureka:
  client:
    register-with-eureka: false # the default is true
    fetch-registry: false # This client gets the registration information on the eureka server registry, the default is true
    #注册中心路径,如果有多个eureka server,在这里需要配置其他eureka server的地址,
    #用","进行区分,如"http://address:8888/eureka,http://address:8887/eureka"
    service-url:
      defaultZone: http://localhost:8000/eureka/,http://localhost:8002/eureka/
  instance:
    # ip-address: 127.0.0.1
    hostname: port2

------
server:
  port: 8002
spring:
  application:
    name: spring-cloud-eureka
eureka:
  client:
    register-with-eureka: false # the default is true
    fetch-registry: false # This client gets the registration information on the eureka server registry, the default is true
    #注册中心路径,如果有多个eureka server,在这里需要配置其他eureka server的地址,
    #用","进行区分,如"http://address:8888/eureka,http://address:8887/eureka"
    service-url:
      defaultZone: http://localhost:8000/eureka/,http://localhost:8001/eureka/
  instance:
    # ip-address: 127.0.0.1
    hostname: port3

需要在hosts文件配置 hostname和ip 的对应关系(实际请求会根据hosts文件中的配置进行域名解析,可以了解下hosts文件–>Linux 下hosts文件详解

  • 启动类
    加上@EnableEurekaServer
    在这里插入图片描述
  • 启动
    用 IDEA maven 打个包
    在这里插入图片描述
    windows 环境启动
D:\IdeaProject\springcloud-learn\springcloud-eureka\spring-cloud-eureka-cluster\target>java -jar spring-cloud-eureka-cluster-1.0-SNAPSHOT.jar --spring.profiles.active=po
rt1
// 启动三个terminal 
java -jar spring-cloud-eureka-cluster-1.0-SNAPSHOT.jar --spring.profiles.active=po
rt1
java -jar spring-cloud-eureka-cluster-1.0-SNAPSHOT.jar --spring.profiles.active=po
rt2
java -jar spring-cloud-eureka-cluster-1.0-SNAPSHOT.jar --spring.profiles.active=po
rt3

springcloud 官方文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值