Eureka集群(2)

Eureka搭建集群

集群:我是这样理解的,同一个业务,部署在多个服务器上(不同的服务器运行同样的代码,干同一件事)

当注册中心扛不住高并发的时候,这时候 要用集群来扛;

今天介绍搭建的两种方式:
第一种方式
我们在上篇博客的基础上再构建两个项目,而这两个项目的配置又不能都是本地,开3个虚拟机又太麻烦,所以我们直接配置本机hosts来实现本机的域名映射
进入你的系统文件:C:\Windows\System32\drivers\etc,找到hosts文件,然后加上你的域名映射,后面在yml文件中配置

127.0.0.1 eureka2001.xy.com
127.0.0.1 eureka2002.xy.com
127.0.0.1 eureka2003.xy.com
在这里插入图片描述
在这里插入图片描述
然后开始创建两个模块 microservice-eureka-server-2002 和 microservice-eureka-server-2003
它们的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.xy</groupId>
		<artifactId>springcloud01</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>
	<groupId>com.xy</groupId>
	<artifactId>microservice-eureka-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>microservice-eureka-server</name>
	<description>Demo project for Spring Boot</description>

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

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

然后分别修改它们的yml配置文件,2001的工程就需要配置其他两个模块的域名,其他的两个模块一样
2001模块yml

server:
  port: 2001
  context-path: /

eureka:
  instance:
    # 单机 hostname: localhost #eureka注册中心实例名称
    hostname: eureka2001.xy.com # 集群
  client:
    register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
    fetch-registry: false     #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
    service-url:
      defaultZone: http://eureka2002.xy.com:2002/eureka/,http://eureka2003.xy.com:2003/eureka/ # 集群
      #单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka注册中心交互的地址,查询服务和注册服务用到


2002模块yml

server:
  port: 2002
  context-path: /

eureka:
  instance:
    hostname: eureka2002.xy.com
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://eureka2001.xy.com:2001/eureka/,http://eureka2003.xy.com:2003/eureka/

2003模块yml

server:
  port: 2003
  context-path: /

eureka:
  instance:
    hostname: eureka2003.xy.com
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://eureka2001.xy.com:2001/eureka/,http://eureka2002.xy.com:2002/eureka/

然后还要修改服务提供者yml文件,我们这里也就是我们的1001模块,整个配置文件

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/db_0524?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123
  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

在每个启动类加上注解@EnableEurekaServer

然后在地址栏输入你在本地还有配置文件配置的域名就能访问了
127.0.0.1 eureka2001.xy.com
127.0.0.1 eureka2002.xy.com
127.0.0.1 eureka2003.xy.com

在这里插入图片描述

第二种方式
我们发现第一种的3个模块除了yml文件不同,其他的都是一样,所以我们可以把它整合到一个模块中去,全部集成到application.yml中就可以,然后我们在IDEA的启动配置中分别读取这个配置文件不同的端口
新建一个模块 microservice-eureka-server
application.yml

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

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.xy</groupId>
		<artifactId>springcloud01</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>
	<groupId>com.xy</groupId>
	<artifactId>microservice-eureka-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>microservice-eureka-server</name>
	<description>Demo project for Spring Boot</description>

	<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>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

一样我们把那一个模块复制3边,读取不同的配置即可
在这里插入图片描述
然后在你的那一个模块的启动类加上@EnableEurekaServer
我们一样能访问我们的集群了
在这里插入图片描述
开发环境,我们经常会遇到这个红色的警告;
当我们长时间为访问服务以及变更服务实例名称的时候,就会出现这个红色警告;

默认情况,如果服务注册中心再一段时间内没有接收到某个微服务实例的心跳,服务注册中心会注销该实例(默认90秒)。

由于正式环境,经常会有网络故障,网络延迟问题发生,服务和注册中心无法正常通信,此时服务是正常的,不应该注销该服务,Eureka这时候,就通过“自我保护模式”来解决问题,当短时间和服务失去通信时,保留服务信息,当恢复网络和通信时候,退出“自我保护模式”;
通过“自我保护模式”,使Eureka集群更加的健壮和稳定;

end…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不愿秃头的阳某

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值