简单配置Eureka和Eureka伪集群

不要着急,最好的总会在最不经意的时候出现。——泰戈尔

创建一个springboot父项目

该父项目啥都不用写,但是在创建的时候,选择的依赖有:
在这里插入图片描述

创建一个eureka注册中心

在创建的项目中,新增一个maven模块来配置eureka注册中心
pom文件


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

application.yml

server:
  port: 20000
spring:
  application:
    # 配置为服务的名称,以后的监控中心可以看到
    name: EurekaService
eureka:
  client:
    service-url:
      defaultZone: http://localhoat:20000/eureka
    #当前配置的是监控中心,没有必要抓取服务
    fetch-registry: false
    # 是否自我注册
    register-with-eureka: false


然后自己手写个启动类,注意添加@EnableEurekaServer注解:

package com.yuxi;

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

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

建立集群

在创建的父项目下,将刚刚新建的eureka复制两份,修改一下application.yml文件中的端口号,注意在 所有的eureka配置文件中的defaultZone:中添加所有的eureka地址

server:
  port: 20001
spring:
  application:
    # 配置为服务的名称,以后的监控中心可以看到
    name: EurekaService
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:20000/eureka,http://eureka2:20001/eureka,http://eureka3:20002/eureka
    #当前配置的是监控中心,没有必要抓取服务
    fetch-registry: false
    # 是否自我注册
    register-with-eureka: false


将计算机中C:\Windows\System32\drivers\etc下的hosts文件打开,添加如下配置(模拟集群时才会这么配置,用来区分)

127.0.0.1 eureka1
127.0.0.1 eureka2
127.0.0.1 eureka3

最后添加一个服务模块就好,新建一个maven模块,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">
    <parent>
        <artifactId>springcloud_boot</artifactId>
        <groupId>com.yuxi</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud_student</artifactId>

    <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-client</artifactId>
        </dependency>
    </dependencies>

    <!--打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.xml

server:
  port: 8080
spring:
  application:
    # 不可以用下划线
    name: micro-Student
eureka:
  client:
    service-url:
      defaultZone: http://localhost:20000/eureka

然后自己写个启动类和其他功能方法就好,然后启动三个eureka和一个服务模块,出现下面的三个eureka名称和服务模块信息就好(我的做出改动,服务模块所以没有)
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值