博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
eureka集群
阅读量:3961 次
发布时间:2019-05-24

本文共 7965 字,大约阅读时间需要 26 分钟。

Eureka集群搭建

高可用集群配置

当注册中心扛不住高并发的时候,这时候 要用集群来扛;
Eureka集群搭建有两种操作
1.普通操作
2.骚操作
普通操作
我们再新建两个module
microservice-eureka-server-2002
在这里插入图片描述
pom.xml

4.0.0
com.zxp
microservice
1.0-SNAPSHOT
microservice-eureka-server-2002
1.8
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework
springloaded
org.springframework.boot
spring-boot-devtools
org.springframework.boot
spring-boot-maven-plugin

2002启动类

package com.zxp.microserviceeurekaserver2002;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class MicroserviceEurekaServer2002Application {    public static void main(String[] args) {        SpringApplication.run(MicroserviceEurekaServer2002Application.class, args);    }}

microservice-eureka-server-2003

在这里插入图片描述
pom.xml

4.0.0
com.zxp
microservice
1.0-SNAPSHOT
microservice-eureka-server-2003
1.8
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework
springloaded
org.springframework.boot
spring-boot-devtools
org.springframework.boot
spring-boot-maven-plugin

2003启动类

package com.zxp.microserviceeurekaserver2003;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class MicroserviceEurekaServer2003Application {    public static void main(String[] args) {        SpringApplication.run(MicroserviceEurekaServer2003Application.class, args);    }}

修改三个项目的application.yml文件,主要是修改 hostname和defaultZone

2001

server:  port: 2001  context-path: /eureka:  instance:    # 单机 hostname: localhost #eureka注册中心实例名称  集群必须在电脑不同的情况下,由于我们只有一台电脑,所以我们可以模拟不同域名    hostname: eureka2001.zxp.com # 集群  client:    register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。    service-url:      defaultZone: http://eureka2002.zxp.com:2002/eureka/,http://eureka2003.zxp.com:2003/eureka/ # 集群      #单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka注册中心交互的地址,查询服务和注册服务用到

2002

server:  port: 2002  context-path: /eureka:  instance:    # 单机 hostname: localhost #eureka注册中心实例名称    hostname: eureka2002.zxp.com # 集群  client:    register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。    service-url:      defaultZone: http://eureka2001.zxp.com:2001/eureka/,http://eureka2003.zxp.com:2003/eureka/ # 集群      #单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka注册中心交互的地址,查询服务和注册服务用到

2003

server:  port: 2003  context-path: /eureka:  instance:    # 单机 hostname: localhost #eureka注册中心实例名称    hostname: eureka2003.zxp.com # 集群  client:    register-with-eureka: false     #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。    service-url:      defaultZone: http://eureka2001.zxp.com:2001/eureka/,http://eureka2002.zxp.com:2002/eureka/ # 集群      #单机defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka注册中心交互的地址,查询服务和注册服务用到

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

找到C:\Windows\System32\drivers\etc 打开hosts,加配置
在这里插入图片描述

127.0.0.1  eureka2001.zxp.com127.0.0.1  eureka2002.zxp.com127.0.0.1  eureka2003.zxp.com

在这里插入图片描述

最后我们测试下
启动三个注册中心,以及服务提供者项目;
在这里插入图片描述
然后浏览器地址栏输入:
http://eureka2001.zxp.com:2001/或者http://localhost:2001/
在这里插入图片描述
http://eureka2002.zxp.com:2002/或者http://localhost:2002/
在这里插入图片描述
http://eureka2003.zxp.com:2003/或者http://localhost:2003/
在这里插入图片描述
骚操作
上面eureka服务搭建,除了yml文件不一样,其他文件都一样,那么我们有什么办法能够将多个eureka服务集合到一个工程中去呢?
创建一个microservice-eureka-server(三合一)子工程
Pom依赖

4.0.0
com.zxp
microservice
1.0-SNAPSHOT
microservice-eureka-server
1.8
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework
springloaded
org.springframework.boot
spring-boot-devtools
org.springframework.boot
spring-boot-maven-plugin

Yml文件

由三个—将差异性配置隔开

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

启动类MicroserviceEurekaServerApplication.java

package com.zxp.microserviceeurekaserver;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class MicroserviceEurekaServerApplication {    public static void main(String[] args) {        SpringApplication.run(MicroserviceEurekaServerApplication.class, args);    }}

跑起来的效果跟普通操作的效果是一致的;

在这里插入图片描述
Eureka自我保护机制
在这里插入图片描述

开发环境,我们经常会遇到这个红色的警告;

当我们长时间为访问服务以及变更服务实例名称的时候,就会出现这个红色警告;

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

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

通过“自我保护模式”,使Eureka集群更加的健壮和稳定。

转载地址:http://gurzi.baihongyu.com/

你可能感兴趣的文章
Android_Note(一)——主题界面设计
查看>>
Android_Note(二)——主界面功能
查看>>
Android开发之——子线程中使用Toast或者更新UI
查看>>
Android开发之——消息模式Toast.makeText的常见用法
查看>>
Android开发之——assets目录下资源使用总结
查看>>
Android开发之——FilenameFilter文件过滤器
查看>>
Ruby on Rails(ROR) 实例开发之一 scaffold创建框架
查看>>
转: 我的android studio学习日记
查看>>
Android_Note(三)——添加、更新、保存记事本功能
查看>>
Ruby 的 Test::Unit
查看>>
创建数据方法
查看>>
JUnit单元测试的几个规律总结
查看>>
QTP之网页链接
查看>>
Fiddler实用教程
查看>>
从入门到深入Fiddler (一)
查看>>
从入门到深入Fiddler (二)
查看>>
Win7+php7+apache2.4
查看>>
QTP基本使用——associate actions
查看>>
charAt()方法和charCodeAt()方法—— 从字符串中选取一个字符.
查看>>
(1)Pascal 程序结构和基本语句
查看>>