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