SpringCloud05:Eureka集群环境配置

springcloud 第1课 :Rest学习环境搭建—服务提供者
springcloud 第2课 :Rest学习环境搭建—服务消费者
springcloud 第3课 :Eureka是什么
springcloud 第4课 :Eureka服务注册、信息配置、自我保护机制

1、配置3个eureka注册中心

新建maven子模块

springcloud-eureka-7002

springcloud-eureka-7003

将springcloud-eureka-7001的 yaml配置文件主启动类pom依赖 都复制,生成3个eureka注册中心

以springcloud-eureka-7002 的复制为例

1.1、pom依赖

<dependencies>
   <!--eureka-->
   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka-server</artifactId>
      <version>1.4.7.RELEASE</version>
   </dependency>
   <!--热部署-->
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
   </dependency>
</dependencies>

1.2、主启动类

package com.lian.springcloud;

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

@EnableEurekaServer  //eureka服务端
@SpringBootApplication
public class EurekaServer_7002 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7002.class,args);
    }
}

1.3、application.yaml配置文件

server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com  #eureka服务端的实例名称,默认是localhost
  client:
    register-with-eureka: false  #表示是否向eureka注册中心注册自己,服务端不需要注册自己
    fetch-registry: false #如果为false,表示自己为注册中心,等待别人来连接,是否从eureka server获取注册信息,由于单节点,不需要同步其他节点数据,用false
    service-url:   #设置服务注册中心的URL,用于client和server端交流
      #单机:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      #集群(关联):
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/

2、配置域名映射,使3个eureka发生配合关系

配置映射内容如下,访问这3个网址,都会访问到 127.0.0.1,这样即使其中一台eureka注册中心挂掉,生产者提供的服务依然可以正常在其他eureka注册中心正常运行
在这里插入图片描述
添加配置内容如下
在这里插入图片描述

3、修改提供者8001的yaml配置

server:
  port: 8001

#mybatis的配置
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml  #mybatis核心配置文件
  mapper-locations: classpath:mybatis/mapper/*.xml
  type-aliases-package: com.lian.springcloud.pojo

#spring的配置
spring:
  application:
    name: springcloud-provider-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8
    username: root
    password: root

#================================= 修改eureka的defaultZone ========================================

#eureka的配置,服务注册到eureka7001、eureka7002、eureka7003
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept8001   #修改eureka页面的status描述
    fetch-registry: false

#info配置
info:
  app.name: lian-springcloud
  company.name: msb

4、测试,集群如果其中断掉对于其他端口也不影响

http://localhost:7001/ 测试
在这里插入图片描述
http://localhost:7003/ 测试
在这里插入图片描述
gif演示
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值