Eureka包含两个组件:Eureka Server和Eureka Client

Eureka Server:提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册

Eureka Client:应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90秒)

一、构建Eureka Server

mhb-cloud-eureka【端口:8761】

mhb-cloud-eureka2【端口:8762】

mhb-cloud-eureka3【端口:8763】

1:pom文件(加入Eureka-server依赖)

<!--eureka服务环境支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency> <!--eureka服务安全控制-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

2:application.yml文件

集群中三台的配置文件,以下三处不同:

#服务名称不同
spring:
application:
name: mhb-cloud-eureka #每一个微服务必须有这个应用名称 #服务端口不同
server:
port: 8761 #服务端口 #服务主机不同
eureka:
instance:
hostname: eureka1.com #eureka服务的主机 hosts文件映射了
spring:
application:
name: mhb-cloud-eureka #每一个微服务必须有这个应用名称
security: #eureka验证信息
user:
name: admin
password: 123456 server:
port: 8761 #服务端口 eureka:
instance:
hostname: eureka1.com #eureka服务的主机 hosts文件映射了
server:
enable-self-preservation: false #关闭自我保护模式 生产环境不建议关闭
eviction-interval-timer-in-ms: 2000 #间隔2秒剔除
client:
## 是否注册自身到eureka服务器
register-with-eureka: false
## 因为自己是注册中心,不需要去检索服务信息
fetch-registry: false
service-url: #单机版不需要配置此属性
#集群配置
defaultZone: http://admin:123456@eureka2.com:8762/eureka/,http://admin:123456@eureka3.com:8763/eureka/

3:在启动类上开启Eureka注解

@EnableEurekaServer

package com.applesnt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer/*eureka注解*/
public class MhbCloudEurekaApplication { public static void main(String[] args) {
SpringApplication.run(MhbCloudEurekaApplication.class, args);
}
}

4:开启Eureka安全认证

com\applesnt\config\CsrfSecurityConfig.java

@@EnableWebSecurity

@Configuration

package com.applesnt.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @EnableWebSecurity
@Configuration
public class CsrfSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();//关闭csrf 否则注册中心能启动,但是服务无法注册进来
super.configure(http); // 这一句必须要加上的,否则直接关闭密码验证服务了
}
}

5:访问Eureka(Eureka集群配置完成)

http://eureka1:8761/ http://eureka2:8762/ http://eureka3:8761/

二、构建Eureka Client

mhb-cloud-producer 【提供者 端口:9904】

mhb-cloud-consumer【消费者 端口:8802】

1:pom文件

mhb-cloud-producer mhb-cloud-consumer:加入Eureka Client依赖

<!--eureka客户端环境支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2:application.yml文件

mhb-cloud-producer:

应用名称:mhb-cloud-producer

端口:9904

Eureka注册方式:集群

debug: false #关闭debug模式

spring:
application:
name: mhb-cloud-producer #应用的名称 server:
port: 9904 #应用的端口号 eureka:
instance:
appname: producer #eureka application的名称
prefer-ip-address: true #开启ip显示eureka的主机服务
#eureka仪表盘的Instances格式
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
client:
service-url:
defaultZone: http://admin:123456@eureka1.com:8762/eureka/,http://admin:123456@eureka2.com:8762/eureka/,http://admin:123456@eureka3.com:8763/eureka/
#从eureka服务器注册表中获取注册表信息的时间间隔,默认30s
registry-fetch-interval-seconds: 30
#客户端发送变化同步到eureka服务器的时间间隔 默认30s
instance-info-replication-interval-seconds: 30
#询问eureka服务url信息的变化的间隔时间 默认300s
eureka-service-url-poll-interval-seconds: 300
#最初同步到eureka服务器的时间 默认40s
initial-instance-info-replication-interval-seconds: 40
#注册表是否压缩
g-zip-content: true
#eureka等待超时时间 默认是5s
eureka-server-connect-timeout-seconds: 5
#eureka等待读取时间 默认是8s
eureka-server-read-timeout-seconds: 8
#eureka客户端允许的所有eureka服务器连接的总数 默认200
eureka-server-total-connections: 200

mhb-cloud-consumer:

应用名称:mhb-cloud-consumer

端口:8802

Eureka注册方式:集群

debug: false

spring:
application:
name: mhb-cloud-consumer #每一个微服务必须有这个应用名称 server:
port: 8802 #端口 eureka:
instance:
appname: consumer #eureka application的名称
prefer-ip-address: true #开启ip显示eureka的主机服务
#eureka仪表盘的Instances格式
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
client:
service-url:
defaultZone: http://admin:123456@eureka1.com:8762/eureka/,http://admin:123456@eureka2.com:8762/eureka/,http://admin:123456@eureka3.com:8763/eureka/
#从eureka服务器注册表中获取注册表信息的时间间隔,默认30s
registry-fetch-interval-seconds: 30
#客户端发送变化同步到eureka服务器的时间间隔 默认30s
instance-info-replication-interval-seconds: 30
#询问eureka服务url信息的变化的间隔时间 默认300s
eureka-service-url-poll-interval-seconds: 300
#最初同步到eureka服务器的时间 默认40s
initial-instance-info-replication-interval-seconds: 40
#注册表是否压缩
g-zip-content: true
#eureka等待超时时间 默认是5s
eureka-server-connect-timeout-seconds: 5
#eureka等待读取时间 默认是8s
eureka-server-read-timeout-seconds: 8
#eureka客户端允许的所有eureka服务器连接的总数 默认200
eureka-server-total-connections: 200

3:在启动类开启eureka客户端注解

mhb-cloud-producer mhb-cloud-consumer:

@@EnableEurekaClient

package com.applesnt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient/*eureka客户端注解*/
public class MhbCloudProducerApplication { public static void main(String[] args) {
SpringApplication.run(MhbCloudProducerApplication.class, args);
}
}

4:服务注册

分别启动 mhb-cloud-producer 和 mhb-cloud-consumer两个Eureka客户端项目

访问Eureka服务:http://eureka1:8761/

三、构建微服务通讯

1:在服务端创建测试controller【mhb-cloud-producer】

com\applesnt\controller\ProducerController.java

访问路径:http://localhost:9904/producer/get/123

/*类访问路径*/
@RequestMapping("/producer") /*返回传递过来的id
* 请求路径:http://localhost:9904/producer/get/123
* */
@GetMapping("/get/{id}")
public String getId(@PathVariable("id") String id){
System.out.println("-----"+id);
return "我是提供者 端口是9904 传递的参数= "+id;
}

2:在消费端创建测试controller【mhb-cloud-consumer】

1:使用RestTemplate进行测试通讯,在启动类上获取RestTemplate对象:

@Bean
@LoadBalanced/*微服务通讯时需要负载均衡 相同的spring.applincatin.name*/
public RestTemplate balanceRestTemplate(){
return new RestTemplate();
}

2:创建消费端远程调用controller

com\applesnt\MhbCloudConsumerApplication.java

访问路径:http://localhost:8802/consumer/get/123

package com.applesnt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; /**
* @description: 消费者控制层
**/
@RestController
@RequestMapping("/consumer")
public class ConsumerController { /*注入RestTemplate对象*/
@Autowired
@Qualifier("balanceRestTemplate")
private RestTemplate balanceRestTemplate; @GetMapping("/get/{id}")
public String getId(@PathVariable String id){ //eureka方式 需要在启动类的RestTemplate加上@LoadBalanced负载均衡的注解
String returnId = balanceRestTemplate.getForObject("http://mhb-cloud-producer/producer/get/"+id,String.class);
System.out.println("returnId = "+returnId);
return returnId;
}
}

3:远程调用测试

消费者访问:http://localhost:8802/consumer/get/123

返回提供者的信息

四、Eureka服务端事件监听

EurekaInstanceCanceledEvent:服务下线事件

EurekaInstanceRegisteredEvent:服务注册事件

EurekaInstanceRenewedEvent:服务续约事件

EurekaRegistryAvailableEvent:eureka注册中心启动事件

EurekaServerStartedEvent: eureka server完全启动后事件

package com.applesnt.event;

import com.netflix.appinfo.InstanceInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.eureka.server.event.*;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component; /**
* @description: eureka事件监听
**/
@Component
@Slf4j
public class EurekaEventLinstener { @EventListener/*实例注册事件*/
public void listen(EurekaInstanceRegisteredEvent event){
InstanceInfo instanceInfo = event.getInstanceInfo();
String name = instanceInfo.getAppName();
String id = instanceInfo.getInstanceId();
log.info(">>>>>>>"+id+":已经注册到eureka服务上 name="+name); } @EventListener/*实例续约事件*/
public void listen(EurekaInstanceRenewedEvent event){
InstanceInfo instanceInfo = event.getInstanceInfo();
String name = instanceInfo.getAppName();
String id = instanceInfo.getInstanceId();
log.info(">>>>>>>"+id+":续约时间触发 name="+name);
} @EventListener/*eureka服务端注册并且是可用状态时事件*/
public void listen(EurekaRegistryAvailableEvent event){
log.info(">>>>>>>注册中心启动触发");
} @EventListener/*eureka服务端完全启动成功后事件*/
public void listen(EurekaServerStartedEvent event){
log.info(">>>>>>>EurekaServer启动触发");
} @EventListener/*实例下线事件*/
public void listen(EurekaInstanceCanceledEvent event){
String name = event.getAppName();
String id = event.getServerId();
log.info(">>>>>>>"+id+":下线成功 name="+name);
}
}

五、eureka自我保护

Eureka Server 在运行期间会去统计心跳失败比例在 15 分钟之内是否低于 85%,如果低于 85%,Eureka Server 会将这些实例保护起来,让这些实例不会过期!

解释:https://www.cnblogs.com/xishuai/p/spring-cloud-eureka-safe.html

关闭保护模式:生产环境不推荐关闭

eureka:
instance:
hostname: eureka1.com #eureka服务的主机 hosts文件映射了
server:
enable-self-preservation: false #关闭自我保护模式 生产环境不建议关闭

SpringCloud(二)笔记之Eureka的更多相关文章

  1. SpringCloud学习笔记(1)——Eureka

    Spring Cloud Spring Cloud为开发者快速构建通用的分布式系统(例如:配置管理.服务发现.断路器.智能路由.微代理.控制总线.一次性的Token.全局锁.领导者选举.分布式会话.集 ...

  2. SpringCloud学习笔记(6)——Eureka高可用

    参考Spring Cloud官方文档第12章12.3.12.5.12.6小节 12.3 High Availability, Zones and Regions 默认情况下,每一个Eureka服务器同 ...

  3. 浅谈SpringCloud (二) Eureka服务发现组件

    上面学习到了如何由一个程序访问另一个程序,那么如果使用SpringCloud来进行访问,该如何访问呐? 可以借助Eureka服务发现组件进行访问. 可以借助官方文档:https://spring.io ...

  4. Alibaba内部SpringCloud参考笔记,在GitHub一天就标星81.6k?

    前言 阿里巴巴,作为国内互联网公司的Top,算是业界的标杆,有阿里背景的程序员,也更具有权威性.作为程序员,都清楚阿里对于员工要求有多高,技术人员掌握的技术水平更是望尘莫及.所以,大厂程序员的很多经验 ...

  5. SpringCloud服务如何在Eureka安全优雅的下线

    如果直接KILL SpringCloud的服务,因为Eureka采用心跳的机制来上下线服务,会导致服务消费者调用此已经kill的服务提供者然后出错,处理这种情况有2中方案. 如需平滑的发布服务请参考: ...

  6. springcloud微服务实战:Eureka+Zuul+Feign/Ribbon+Hystrix Turbine+SpringConfig+sleuth+zipkin

    相信现在已经有很多小伙伴已经或者准备使用springcloud微服务了,接下来为大家搭建一个微服务框架,后期可以自己进行扩展.会提供一个小案例: 服务提供者和服务消费者 ,消费者会调用提供者的服务,新 ...

  7. SpringCloud学习笔记(2):使用Ribbon负载均衡

    简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具,在注册中心对Ribbon客户端进行注册后,Ribbon可以基于某种负载均衡算法,如轮询(默认 ...

  8. SpringCloud学习笔记(3):使用Feign实现声明式服务调用

    简介 Feign是一个声明式的Web Service客户端,它简化了Web服务客户端的编写操作,相对于Ribbon+RestTemplate的方式,开发者只需通过简单的接口和注解来调用HTTP API ...

  9. SpringCloud学习笔记(4):Hystrix容错机制

    简介 在微服务架构中,微服务之间的依赖关系错综复杂,难免的某些服务会出现故障,导致服务调用方出现远程调度的线程阻塞.在高负载的场景下,如果不做任何处理,可能会引起级联故障,导致服务调用方的资源耗尽甚至 ...

随机推荐

  1. 关于Spring的常见面试题

    1.Spring是什么? Spring是一个轻量级的IoC和AOP容器框架.是为Java应用程序提供基础性服务的一套框架,目的是用于简化企业应用程序的开发,它使得开发者只需要关心业务需求.常见的配置方 ...

  2. 开发一个健壮的npm包

    项目地址:loan-calculate-utils npm包的发布.更新查看上一篇文章 开发一个基础的npm包 目前我们的目录是这个样子: . ├── source 源代码目录 │   └── ind ...

  3. 意外发现PHP另一个显示转换类型 binary

    竟然不知道除了(string)$a之外,还有(binary)$a知道unset可以不加括号,但不知道还有这种写法(unset) 请看lex文件(php-7.1.8) <ST_IN_SCRIPTI ...

  4. JVM中内存分配策略及堆和栈的比较

    最近愈发对JVM底层的运行 原理产生了兴趣,遂查阅相关资料以备忘. 内存分配策略 根据编译原理的观点,程序运行时的内存分配,有三种策略,分别为静态的.堆式的.栈式的. 静态存储分配指的是在编译时就能确 ...

  5. Make编译Ardupilot源码的两种方法

    编译环境准备 ​ Ardupilot源码下载和PX4 toolchain工具链下载 ​ (见https://www.cnblogs.com/BlogsOfLei/p/7707485.html) ​ 注 ...

  6. Python——图像手绘效果

    1.图像的RGB色彩模式 PIL PIL, Python Image Library PIL库是一个具有强大图像处理能力的第三方库 在命令行下的安装方法: pip install pillow fro ...

  7. javascript实现组合列表框中元素移动效果

    应用背景:在页面中有两个列表框,需要把其中一个列表框的元素移动到另一个列表框 .  实现的基本思想: (1)编写init方法对两个列表框进行初始化: (2)为body添加onload事件调用init方 ...

  8. C 苟富贵

    时间限制 : 15000 MS   空间限制 : 524288 KB 问题描述 你最近买六合彩赚了很多钱,导致一个银行账户存不下了,于是你开设了 N 个账户,第 i 个账户里存有 Ai 元. 你的好友 ...

  9. Java并发基础08. 造成HashMap非线程安全的原因

    在前面我的一篇总结(6. 线程范围内共享数据)文章中提到,为了数据能在线程范围内使用,我用了 HashMap 来存储不同线程中的数据,key 为当前线程,value 为当前线程中的数据.我取的时候根据 ...

  10. on duplicate key update 的用法说明(解决批量操作数据,有就更新,没有就新增)mybatis批量操作数据更新和添加

    项目用的ORM框架是用springdatajpa来做的,有些批量数据操作的话,用这个效率太低,所以用mybatis自己写sql优化一下. 一般情况,我们肯定是先查询,有就修改,没有就添加,这样的话,单 ...