SpringCloud之服务注册中心和提供者(Eureka Server和Eureka Clie
一、使用Eureka Server搭建服务注册中心1.Maven依赖<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> 2.配置文件(application.yml)server: port: 8761 eureka: evictionIntervalTimerInMs: 6000 instance: hostname: localhost client: registerWithEureka: false fetchRegistry: serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ spring: application: name: blog-eureka-server 3.启动类package com.springcloud.blog; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class BlogEurekaServerApplication { static void main(String[] args) { SpringApplication.run( BlogEurekaServerApplication.,args ); } } 4.启动效果二、使用Eureka Client搭建服务提供者1.Maven依赖<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> 2.配置文件(application.yml)# Tomcat server: tomcat: uri-encoding: UTF-8 max-threads: 1000 min-spare-threads: 30 port: 2020 servlet: context-path: /blog-admin ## spring相关配置 spring: application: name: blog-admin profiles: active: dev jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+ servlet: multipart: max-file-size: 100MB max-request-size: 100MB enabled: true eureka: client: serviceUrl: defaultZone: http:localhost:8761/eureka/ 3.启动类package com.springcloud.blog.admin; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication(exclude = DruidDataSourceAutoConfigure.) @EnableEurekaClient BlogAdminApplication{ main(String[] args) { SpringApplication.run(BlogAdminApplication.4.启动后,回过头看服务注册中心,增加了一个服务提供者
2.基础架构Eureka服务治理基础架构的三个核心要素。
3.服务治理机制服务提供者(1)服务注册 (2)服务同步 (3)服务续约 服务消费者(1)获取服务 (2)服务调用 (3)服务下线 服务注册中心(1)失效剔除 (2)自我保护 EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND 实际上,该警告触发了Eureka Server的自我保护机制。 服务注册到Eureka Server之后,会维护一个心跳连接,告诉Eureka Server自己还活着。Eureka Server在运行期间,会统计心跳失败的比例在15分钟之内是否低于85%,如果出现低于的情况(在单机调试的时候很容易满足,实际在生产环境上通常是由于网络不稳定导致),Eureka Server 会将当前的实例注册信息保护起来,让这些实例不会过期,尽可能保护这些注册信息。但是,在这段保护期间内实例若出现问题,那么客户端很容易拿到实际上不存在的服务实例,会出现调用失败的情况,所以客户端必须要有容错机制,比如可以使用请求重试、断路器等机制。 由于本地调试很容易触发注册中心的保护机制,这回使得注册中心维护的服务实例不那么准确。所以,我们在本地进行开发的时候,可以使用eureka.server.enable-self-preservation=false 参数来关闭保护机制,以确保注册中心可以将不可用的实例正确剔除。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |