【SpringCloud】05 SpringCloud Eureka集群创建代码演示

一、引入依赖

1、Eureka 6001

  1. 依赖导入
<?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">
    <parent>
        <artifactId>microservice-cloud-01</artifactId>
        <groupId>com.haoxiansheng.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservice-cloud-05-eureka-6001</artifactId>


    <dependencies>
        <!-- 导入Eureka-server 服务端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>
  1. 配置文件 application.yml
server:
  port: 6001 # 服务端口

eureka:
  instance:
    hostname: eureka6001.com # eureka服务端的实例名
  client:
    registerWithEureka: false # 服务注册,false表示不将自已注册到Eureka服务中
    fetchRegisrty: false # 服务发现,false表示自己不从Eureka服务中获取注册信息
    serviceUrl: # Eureka客户端与Eureka服务端的交互地址,集群版配置对方的地址,单机版配置自
                #己(如果不配置则默认本机8761端口)
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      defaultZone: http://127.0.0.1:6002/eureka/
  server:
    enable-self-preservation: false # 禁用自我保护机制
  1. 开启
package com.haoxiansheng.springcloud;

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

@EnableEurekaServer  //标识一个Eureka Server服务注册中心
@SpringBootApplication
public class EurekaServer_6001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_6001.class, args);
    }
}

2、 Eureka 6002

  1. 依赖
<?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">
    <parent>
        <artifactId>microservice-cloud-01</artifactId>
        <groupId>com.haoxiansheng.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservice-cloud-05-eureka-6002</artifactId>

    <dependencies>
        <!-- netflix-eureka-server 服务端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>
  1. 配置文件 application.yml
server:
  port: 6002 # 服务端口

eureka:
  instance:
    hostname: eureka6002.com # eureka服务端的实例名
  client:
    registerWithEureka: false # 服务注册,false表示不将自已注册到Eureka服务中
    fetchRegisrty: false # 服务发现,false表示自己不从Eureka服务中获取注册信息
    serviceUrl: # Eureka客户端与Eureka服务端的交互地址,集群版配置对方的地址,单机版配置自
                #己(如果不配置则默认本机8761端口)
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      defaultZone:  http://127.0.0.1:6001/eureka/
  server:
    enable-self-preservation: false # 禁用自我保护机制
  1. 启动
package com.haoxiansheng.springcloud;

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值