简介
SpringCloud是一个基于SpringBoot实现的微服务架构开发工具。它为微服务架构中涉及的配置管理、服务治理、断路器、智能路由等操做提供了一种简单的开发方式。html
Spring Cloud 的github地址https://github.com/Netflix/Eurekajava
SpringCloud包含的子项目中Spring Cloud NetFlix :核心组件,对多个Netflix OSS 开源套件进行整合。git
一. 服务注册与发现github
图中Eureka Server一般为框架中构建的注册中心,每一个service consumer 即服务单元向注册中心登记本身提供的服务,将主机与端口号、版本号、通讯协议等一些附加信息告知注册中心。
服务方调用调用服务提供方的接口时,需向服务注册中心咨询服务,并获取服务,以实现访问。web
注册中心Eureka Server :spring
Service Provider缓存
Service Consumertomcat
二 搭建服务注册中心架构
一、首先创建SpringBoot工程,在pom.xml中加入配置信息app
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!--排除内置的tomcat--> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <!--<scope>provided</scope>--> </dependency> </dependencies>
二、经过@EnableEurekaServer 注解启动一个服务注册中心,即在SpringBoot应用中添加便可
@EnableEurekaServer @SpringBootApplication public class RegisterApplication extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(RegisterApplication.class); } public static void main(String[] args) { SpringApplication.run(RegisterApplication.class,args); } }
在默认设置下,避免服务注册中心将本身做为客户端注册本身,因而在application.properties中增长以下配置
server: port: 9000 context-path: /registry spring: application: name: registry-server eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/${server.context-path}/eureka/ security: basic: enable: true user: name: admin password: admin
启动服务,注册界面报错信息:
Eureka server和client之间每隔30秒会进行一次心跳通讯,告诉server,client还活着。由此引出两个名词:
Renews threshold:server指望在每分钟中收到的心跳次数
Renews (last min):上一分钟内收到的心跳次数。
前文说到禁止注册server本身为client,无论server是否禁止,阈值(threshold)是1。client个数为n,阈值为1+2n(此为一个server且禁止自注册的状况)
若是是多个server,且开启了自注册,那么就和client同样,是对于其余的server来讲就是client,是要2的
阈值:1+21
renews:
1)自注册 2 + 21
2)非自注册:2*1
Eurake有一个配置参数eureka.server.renewalPercentThreshold,定义了renews 和renews threshold的比值,默认值为0.85。当server在15分钟内,比值低于percent,即少了15%的微服务心跳,server会进入自我保护状态,Self-Preservation。在此状态下,server不会删除注册信息,这就有可能致使在调用微服务时,实际上服务并不存在。
参考文章https://www.cnblogs.com/breath-taking/articles/7940364.html