Eureka集群搭建

Eureka集群搭建

普通操作

我们再新建两个module microservice-eureka-server-2002 microservice-eureka-server-2003
pom文件


    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--  修改后立即生效,热部署  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

2002 2003的主启动类EurekaServerApplication_2002,EurekaServerApplication_2003复制修改下;

package com.liubiao.microserviceeurekaserver2003;

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

@EnableEurekaServer
@SpringBootApplication
public class MicroserviceEurekaServer2003Application {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceEurekaServer2003Application.class, args);
    }

}

前面单机的时候 eureka注册中心实例名称 是localhost,现在是集群,不能三个实例都是localhost,这里复杂的办法是搞三个虚拟机,麻烦,这里有简单办法,直接配置本机hosts,来实现本机域名映射;
找到 C:\Windows\System32\drivers\etc 打开hosts,加配置

127.0.0.1  eureka2001.liubiao.com
127.0.0.1  eureka2002.liubiao.com
127.0.0.1  eureka2003.liubiao.com

在这里插入图片描述
修改三个项目的application.yml文件,主要是修改 hostname和defaultZone,
2001修改:

server:
  port: 2001
  context-path: /

eureka:
  instance:
    hostname: eureka2001.liubiao.com
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka2002.liubiao.com:2002/eureka/,http://eureka2003.liubiao.com:2003/eureka/ # 集群

2002修改:

server:
  port: 2002
  context-path: /

eureka:
  instance:
    hostname: eureka2002.liubiao.com
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka2001.liubiao.com:2001/eureka/,http://eureka2003.liubiao.com:2003/eureka/ # \u96C6\u7FA4

2003修改:

server:
  port: 2003
  context-path: /

eureka:
  instance:
    hostname: eureka2003.liubiao.com
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka2001.liubiao.com:2001/eureka/,http://eureka2002.liubiao.com:2002/eureka/ # \u96C6\u7FA4

修改服务提供者项目的application.yml,主要修改eureka.client.service-url.defaultZone

server:
  port: 1001
  context-path: /
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/liubiao?useUnicode=true&characterEncoding=utf8
    username: root
    password: 157359lb
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1001
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:2001/eureka
info:
  groupId: com.liubiao.springcloud
  artifactId: microservice-student-provider-1001
  version: 1.0-SNAPSHOT
  userName: http://liubiao.com
  phone: 123456

最后我们测试下:
启动三个注册中心,以及服务提供者项目;

然后浏览器地址栏输入:
http://eureka2001.liubiao.com:2001/
http://eureka2002.liubiao.com:2002/
http://eureka2003.liubiao.com:2003/
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

骚操作

上面eureka服务搭建,除了yml文件不一样,其他文件都一样,那么我们有什么办法能够将多个eureka服务集合到一个工程中去呢?
创建一个microservice-eureka-server(三合一)子工程
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.liubiao</groupId>
        <artifactId>springcloug</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-eureka-server</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--  修改后立即生效,热部署  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Yml文件

---
server:
  port: 2001
  context-path: /
eureka:
  instance:
    hostname: eureka2001.liubiao.com
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka2002.liubiao.com:2002/eureka/,http://eureka2003.liubiao.com:2003/eureka/
spring:
  profiles: eureka2001
---
server:
  port: 2002
  context-path: /
eureka:
  instance:
    hostname: eureka2002.liubiao.com
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka2001.liubiao.com:2001/eureka/,http://eureka2003.liubiao.com:2003/eureka/
spring:
  profiles: eureka2002
---
server:
  port: 2003
  context-path: /
eureka:
  instance:
    hostname: eureka2003.liubiao.com
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka2001.liubiao.com:2001/eureka/,http://eureka2002.liubiao.com:2002/eureka/
spring:
  profiles: eureka2003

启动类MicroserviceEurekaServerApplication.java

package com.liubiao.microserviceeurekaserver;

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

@EnableEurekaServer
@SpringBootApplication
public class MicroserviceEurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceEurekaServerApplication.class, args);
    }

}

在这里插入图片描述
最后效果跟上面是一样的

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值