SpringCloud——eureka集群
目的:
第一种普通方式
第二种方式骚操作
Eureka自我保护机制
Eureka集群搭建
说的通俗易懂一点就是,同一个项目部署在多个服务器上。
当注册中心扛不住高并发的时候,这时候 要用集群来扛;
今天介绍搭建的两种方式:
第一种方式普通方式:
在上篇博客的基础上再构建两个项目,而这两个项目的配置又不能都是本地,开3个虚拟机又太麻烦,所以我们直接配置本机hosts来实现本机的域名映射
进入你的系统文件:C:\Windows\System32\drivers\etc 找到hosts文件,然后加上你的域名映射,后面在yml文件中配置
127.0.0.1 eureka2001.ht.com
127.0.0.1 eureka2002.ht.com
127.0.0.1 eureka2003.ht.com
创建三个POM依赖相同的Springboot项目
microservice-eureka-server-2001
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.ht</groupId>
<artifactId>htSpringCloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-eureka-server-2003</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文件
microservice-eureka-server-2001
server:
port: 2001
context-path: / eureka:
instance:
# 单机 hostname: localhost #eureka注册中心实例名称
hostname: eureka2001.ht.com # 集群
client:
register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
fetch-registry: false #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
service-url:
defaultZone: http://eureka2002.ht.com:2002/eureka/,http://eureka2003.ht.com:2003/eureka/ # 集群
microservice-eureka-server-2002
server:
port: 2002
context-path: /
eureka:
instance:
# 单机 hostname: localhost #eureka注册中心实例名称
hostname: eureka2002.ht.com # 集群
client:
register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
fetch-registry: false #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
service-url:
defaultZone: http://eureka2001.ht.com:2001/eureka/,http://eureka2003.ht.com:2003/eureka/ # 集群
#单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka注册中心交互的地址,查询服务和注册服务用到
microservice-eureka-server-2003
server:
port: 2003
context-path: /
eureka:
instance:
# 单机 hostname: localhost #eureka注册中心实例名称
hostname: eureka2003.ht.com # 集群
client:
register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
fetch-registry: false #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
service-url:
defaultZone: http://eureka2001.ht.com:2001/eureka/,http://eureka2002.ht.com:2002/eureka/ # 集群
#单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka注册中心交互的地址,查询服务和注册服务用到
给服务提供者修改的application.yml重新配置eureka
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/test?useUnicode=true&characterEncoding=utf8
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
eureka:
instance:
#eureka客户端主机实例名称
hostname: localhost
#客户端服务名
appname: microservice-student
#客户端实例名称
instance-id: microservice-student:1001
#显示IP
prefer-ip-address: true
client:
service-url:
defaultZone: http://eureka2001.ht.com:2001/eureka/,http://eureka2002.ht.com:2002/eureka/,http://eureka2003.ht.com:2003/eureka/
info:
groupId: com.ht.htSpringCloud
artifactId: microservice-student-provider-1001
version: 1.0-SNAPSHOT
userName: http://ht.com
phone: 123456
记得在每个启动类加上注解@EnableEurekaServer
运行你自己所配置的文件路径
127.0.0.1 eureka2001.ht.com
127.0.0.1 eureka2002.ht.com
127.0.0.1 eureka2003.ht.com
第二种方式骚操作
上面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.ht</groupId>
<artifactId>htSpringCloud</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.ht.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka2002.ht.com:2002/eureka/,http://eureka2003.ht.com:2003/eureka/
spring:
profiles: eureka2001
---
server:
port: 2002
context-path: /
eureka:
instance:
hostname: eureka2002.ht.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka2001.ht.com:2001/eureka/,http://eureka2003.ht.com:2003/eureka/
spring:
profiles: eureka2002
---
server:
port: 2003
context-path: /
eureka:
instance:
hostname: eureka2003.ht.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka2001.ht.com:2001/eureka/,http://eureka2002.ht.com:2002/eureka/
spring:
profiles: eureka2003
记得在启动类上添加@EnableEurekaServer
package com.ht.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);
} }
效果:
Eureka自我保护机制
如果在服务注册中心注册过的服务提供者突然失去了联系的话,服务注册中心不会立即注销掉该服务提供者的信息,而是会等个90秒再注销。
这样就使得Eureka集群更加的健壮和稳定
谢谢观看!
SpringCloud——eureka集群的更多相关文章
- SpringCloud之Eureka集群
前面我们介绍了SpringCloud注册中心Eureka,但是存在一个单点故障的问题,一个注册中心远远不能满足实际的生产环境,现在我们介绍一下如何搭建一个Eureka集群. 一:集群环境搭建 我们先建 ...
- springcloud费话之Eureka集群
目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud ...
- 三(2)、springcloud之Eureka集群配置
1)原理说明** 服务启动后向Eureka注册,Eureka Server会将注册信息向其他Eureka Server进行同步,当服务消费者要调用服务提供者,则向服务注册中心获取服务提供者地址,然后会 ...
- SpringCloud学习之—Eureka集群搭建
Eureka集群的搭建 上次说过了在SpringCloud应用中使用Eureka注册中心,用来对服务提供者进行服务注册与发现,但同时,它也是一个"微服务",单个应用使用空间有限,因 ...
- F版本SpringCloud 5—Eureka集群和自我保护机制
源码地址:https://gitee.com/bingqilinpeishenme/Java-Tutorials 前言 上篇文章中,通过代码搭建了Eureka注册中心和客户端,是Eureka的简单应用 ...
- SpringCloud学习之搭建eureka集群,手把手教学,新手教程
一.为什么需要集群 上一篇文章讲解了如何搭建单个节点的eureka,这篇讲解如何搭建eureka集群,这里的集群还是本地不同的端口执行三个eureka,因为条件不要允许,没有三台电脑,所以大家将就一下 ...
- 基于dns搭建eureka集群
eureka集群方案: 1.通常我们部署的eureka节点多于两个,根据实际需求,只需要将相邻节点进行相互注册(eureka节点形成环状),就达到了高可用性集群,任何一个eureka节点挂掉不会受到影 ...
- SpringCloud实现集群和负载均衡
Spring cloud是一个基于Spring Boot实现的服务治理工具包,在微服务架构中用于管理和协调服务的. 组成部分 spingcloud的五大神兽 服务发现——Netflix Eureka ...
- eureka集群高可用配置
譬如eureka.client.register-with-eureka和fetch-registry是否要配置,配不配区别在哪里:eureka的客户端添加service-url时,是不是需要把所有的 ...
随机推荐
- sublime text 3插件改造之AutoFileName去掉.vue文件中img标签后面的width和height,完全去掉!!
在.vue文件中img标签使用autofilename提示引入文件时,会在文件后面插入宽度高度,如下图: 文件后面会自动插入height和width,其实这两玩意儿在大多数时候并没卵用,然后就开始了百 ...
- GoCN每日新闻(2019-10-02)
GoCN每日新闻(2019-10-02) GoCN每日新闻(2019-10-02) 1. Golang中基于Gin和Casbin的web使用方式 https://dev.to/maxwellhertz ...
- Spark-Streaming DirectKafka count 案例
Spark-Streaming DirectKafka count 统计跟直接 kafka 统计类似,只不过这里使用的是 Direct 的方式,Direct方式使用的 kafka 低级API,不同的地 ...
- eclipse为项目设置jdk
1)在项目上右键选中properties,会进入如下界面 (2)然后点击Add Library,进入设置Library的界面 (3)选中JRE System Library进入下一界面就可以设置jdk ...
- C++2.0新特性(六)——<Smart Pointer(智能指针)之shared_ptr>
Smart Pointer(智能指针)指的是一类指针,并不是单一某一个指针,它能知道自己被引用的个数以至于在最后一个引用消失时销毁它指向的对象,本文主要介绍C++2.0提供的新东西 一.Smart P ...
- Freemarker的简单demo
第一步.导入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId>freemark ...
- node省市区三级数据性能测评
闲来无事,测试下node和egg 首先是数据库,大概长这样 然后是代码 'use strict'; const Controller = require('egg').Controller; clas ...
- 3ds Max学习日记(十一)——如何给模型上贴图
参考链接:https://jingyan.baidu.com/article/e4511cf38a810b2b845eaf1f.html 之前一直都不知道怎么在3dsMax里给模型上材质和贴图,被 ...
- bookdown - 撰写和发表自己的网络书籍/文档
BOOKDOWN官网 - bookdown上有不少人发布了自己的电子书 bookdown-demo (一个demo,github源码)- Rstudio出品,Yihui Xie开发,著名单细胞课程在用 ...
- Attention U-Net: Learning Where to Look for the Pancreas
Attention U-Net: Learning Where to Look for the Pancreas 2019-09-10 09:50:43 Paper: https://arxiv.or ...