SpringCloud(二)笔记之Eureka
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的更多相关文章
- SpringCloud学习笔记(1)——Eureka
Spring Cloud Spring Cloud为开发者快速构建通用的分布式系统(例如:配置管理.服务发现.断路器.智能路由.微代理.控制总线.一次性的Token.全局锁.领导者选举.分布式会话.集 ...
- SpringCloud学习笔记(6)——Eureka高可用
参考Spring Cloud官方文档第12章12.3.12.5.12.6小节 12.3 High Availability, Zones and Regions 默认情况下,每一个Eureka服务器同 ...
- 浅谈SpringCloud (二) Eureka服务发现组件
上面学习到了如何由一个程序访问另一个程序,那么如果使用SpringCloud来进行访问,该如何访问呐? 可以借助Eureka服务发现组件进行访问. 可以借助官方文档:https://spring.io ...
- Alibaba内部SpringCloud参考笔记,在GitHub一天就标星81.6k?
前言 阿里巴巴,作为国内互联网公司的Top,算是业界的标杆,有阿里背景的程序员,也更具有权威性.作为程序员,都清楚阿里对于员工要求有多高,技术人员掌握的技术水平更是望尘莫及.所以,大厂程序员的很多经验 ...
- SpringCloud服务如何在Eureka安全优雅的下线
如果直接KILL SpringCloud的服务,因为Eureka采用心跳的机制来上下线服务,会导致服务消费者调用此已经kill的服务提供者然后出错,处理这种情况有2中方案. 如需平滑的发布服务请参考: ...
- springcloud微服务实战:Eureka+Zuul+Feign/Ribbon+Hystrix Turbine+SpringConfig+sleuth+zipkin
相信现在已经有很多小伙伴已经或者准备使用springcloud微服务了,接下来为大家搭建一个微服务框架,后期可以自己进行扩展.会提供一个小案例: 服务提供者和服务消费者 ,消费者会调用提供者的服务,新 ...
- SpringCloud学习笔记(2):使用Ribbon负载均衡
简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具,在注册中心对Ribbon客户端进行注册后,Ribbon可以基于某种负载均衡算法,如轮询(默认 ...
- SpringCloud学习笔记(3):使用Feign实现声明式服务调用
简介 Feign是一个声明式的Web Service客户端,它简化了Web服务客户端的编写操作,相对于Ribbon+RestTemplate的方式,开发者只需通过简单的接口和注解来调用HTTP API ...
- SpringCloud学习笔记(4):Hystrix容错机制
简介 在微服务架构中,微服务之间的依赖关系错综复杂,难免的某些服务会出现故障,导致服务调用方出现远程调度的线程阻塞.在高负载的场景下,如果不做任何处理,可能会引起级联故障,导致服务调用方的资源耗尽甚至 ...
随机推荐
- PHP7内核(八):深入理解字符串的实现
在前面大致预览了常用变量的结构之后,我们今天来仔细的剖析一下字符串的具体实现. 一.字符串的结构 struct _zend_string { zend_refcounted_h gc; /* 字符串类 ...
- [React]Context机制
在React中,Context机制是为了方便在组件树间传递数据. 例子 import React from 'react' const themes={ light:"亮色主题", ...
- 请问一下大佬们,我的项目compile编译的时候总是编译报错
这是执行compile编译的结果 这是加上参数-X之后的结果
- Mob 之 短信验证集成 SMSSDK
开相关发中总会遇到短信验证这些操作,这周没有来得及写新的东西,借此分享一篇以前学习短信验证的笔记,本文使用的是 Mob 提供的 SMSSDK . 下载 SMSSDK 官网下载地址:SMSSDK 集成 ...
- Bootstrap 的基本实现
bootstrap: UI插件 YUI, ElementUI Bootstrap 是最受欢迎的 HTML.CSS 和 JS 框架,用于开发响应式布局.移动设备优先的 WEB 项目. 响应式布局 ...
- Educational Codeforces Round 83 (Rated for Div. 2)
A. Two Regular Polygons 题意:给你一个 正n边形,问你能否以这个 n 的其中一些顶点组成一个 m边形, 思路 :如果 n % m == 0 ,就可 收获:边均分 B. Bogo ...
- 微信小程序分享至朋友圈的方法
最近研究怎么实现微信小程序分享至朋友圈,对就是朋友圈. 微信小程序目前没有直接提供方法来将小程序分享至朋友圈,不过可以采用曲线救国的方式来达到目的. 方法分两步: 1.通过浏览器将希望分享的东西风向至 ...
- 中阶 d04 xml 概念及使用
idea新建xml文件https://www.jianshu.com/p/b8aeadae39b0 或https://blog.csdn.net/Hi_Boy_/article/details/804 ...
- 03 GUI界面的错误日志查看及清除
右上角图标,会显示当前使用工具的运行报错信息,点击可在下方查看到实际的错误日志
- 多线程高并发编程(3) -- ReentrantLock源码分析AQS
背景: AbstractQueuedSynchronizer(AQS) public abstract class AbstractQueuedSynchronizer extends Abstrac ...