组件EurekaServer注册中心

 *Spring-cloud是基于Http协议的,他的优势就是可以不同语言之间的交互,Dubbo局限于JAVA之间的通讯

微服务注册中心提供服务注册和服务发现,方便微服务之间的相互调用。Spring Cloud Eureka 就是对应的微服务注册中心解决方案。

Spring Cloud Eureka 微服务注册中心服务端搭建

  • 第一步 新增Spring Boot项目依赖,新增 Spring Cloud项目依赖

  • 第二步 新增 spring-cloud-starter-netflix-eureka-server依赖

  • 第三步 使用 @EnableEurekaServer 注解启动EurekaServer

  • 第四步 配置 application.yml 文件

  • 第五步 通过浏览器访问 http://localhost:8761

1.首先创建使用SpringBoot作为基本架构,创建父工程,在父工程的pom.xml文件中新增Spring Cloud项目依赖

parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.9.RELEASE</version></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Greenwich.SR3</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>#如果下载速度比较慢,使用阿里的仓库
<repositories><repository><id>aliyun-repos</id><url>http://maven.aliyun.com/nexus/content/groups/public/</url><snapshots><enabled>false</enabled></snapshots></repository>
</repositories><pluginRepositories><pluginRepository><id>aliyun-plugin</id><url>http://maven.aliyun.com/nexus/content/groups/public/</url><snapshots><enabled>false</enabled></snapshots></pluginRepository>
</pluginRepositories>

2.创建子项目,新增 spring-cloud-starter-netflix-eureka-server依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>

3.在子工程中创建启动类,使用 @EnableEurekaServer 注解启动EurekaServer

package com.server;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;/*** wl* 2020/7/19*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class,args);}
}

4.因为使用的Springboot框架,所以在子项目中配置文件中创建,启动默认加载的文件application.yml,并且配置服务注册相关配置。

spring.application.name: eureka-server      #服务端系统名称
server.port: 8761eureka:server:eviction-interval-timer-in-ms: 1000     #服务端每隔1000获取一次服务列表enable-self-preservation: false         #关闭自我保护模式renewal-percent-threshold: 0.85          #自我保护的错误系数,默认为0.85wait-time-in-ms-when-sync-empty: 1       #同步失败的等待时间number-of-replication-retries: 5         #同步失败的重试次数client:serviceUrl:defaultZone: http://localhost:8762/eureka/   # 默认注册中心的地址,所有的微服务在启动的时候就要去找这个地址,在服务的注册时候就会注册在这个地址上register-with-eureka: true #允许自己注册自己instance:                                #指定一个instance-id因为服务之间调用都是通过 instance-id来调用的#hostname:eureka1                       #可以通过hostname指定(同一局域网下可以这样设置,在类似上线的大网是不能用的,可能会出现重复),默认注册到服务中心prefer-ip-address: true              # 启用ip前缀ip-address: 127.0.0.1                 #设置前缀instance-id: ${eureka.instance.ip-address}:${server.port}   # 设置注册服务中心显示的实例名称

5.第五步 通过浏览器访问 http://localhost:8761(注册成功)

6.也可以集群部署:通过开启自己可注册自己,根据application.yml配置完成相互注册,完成集群注册。

1.
spring.application.name: eureka-server      #服务端系统名称
server.port: 8761eureka:server:eviction-interval-timer-in-ms: 1000     #服务端每隔1000获取一次服务列表enable-self-preservation: false         #关闭自我保护模式renewal-percent-threshold: 0.85          #自我保护的错误系数,默认为0.85wait-time-in-ms-when-sync-empty: 1       #同步失败的等待时间number-of-replication-retries: 5         #同步失败的重试次数client:serviceUrl:defaultZone: http://localhost:8762/eureka/   # 默认注册中心的地址,所有的微服务在启动的时候就要去找这个地址,在服务的注册时候就会注册在这个地址上register-with-eureka: true #允许自己注册自己instance:                                #指定一个instance-id因为服务之间调用都是通过 instance-id来调用的#hostname:eureka1                       #可以通过hostname指定(同一局域网下可以这样设置,在类似上线的大网是不能用的,可能会出现重复),默认注册到服务中心prefer-ip-address: true              # 启用ip前缀ip-address: 127.0.0.1                 #设置前缀instance-id: ${eureka.instance.ip-address}:${server.port}   # 设置注册服务中心显示的实例名称2.
spring.application.name: eureka-server      #服务端系统名称
server.port: 8762eureka:server:eviction-interval-timer-in-ms: 1000     #服务端每隔1000获取一次服务列表enable-self-preservation: false         #关闭自我保护模式renewal-percent-threshold: 0.85          #自我保护的错误系数,默认为0.85wait-time-in-ms-when-sync-empty: 1       #同步失败的等待时间number-of-replication-retries: 5         #同步失败的重试次数client:serviceUrl:defaultZone: http://localhost:8761/eureka/   # 默认注册中心的地址,所有的微服务在启动的时候就要去找这个地址,在服务的注册时候就会注册在这个地址上register-with-eureka: true #允许自己注册自己instance:                                #指定一个instance-id因为服务之间调用都是通过 instance-id来调用的#hostname:eureka1                       #可以通过hostname指定(同一局域网下可以这样设置,在类似上线的大网是不能用的,可能会出现重复),默认注册到服务中心prefer-ip-address: true              # 启用ip前缀ip-address: 127.0.0.1                 #设置前缀instance-id: ${eureka.instance.ip-address}:${server.port}   # 设置注册服务中心显示的实例名称

服务端注册搭建完成。

7.Eureka的自我保护机制

自我我保护模式默认是开启的,当注册中心第一个参数大于第二个参数就开启自我保护模式。第二个系数是注册列表所有服务x2。开启保护模式,当第二个参数大于第一个参数,会报一个错误:服务列表的服务不准确。网络分区故障时开启自我保护,服务列表还在,还能正常调用。假如真的有服务宕机后服务列表不会剔除服务,需要进行客户端容错机制。

注意:

自我保护的错误系数,应该设置低一点,因为生产环境我们的自我保护模式是必须开启的,因为出现网络故障可能会认为所有的服务都失效了,从而剔除掉所有的服务。

二、客户端注册服务以及调用。

1.创建客户端子工程,添加pom.xml依赖

#强调是Springboot的web项目
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
#eureka客户端注册依赖<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies>

2.再编写配置文件

spring:application:name: eureka-clienteureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/server:port: 8764instance:                                #指定一个instance-id因为服务之间调用都是通过 instance-id来调用的#hostname:eureka1                       #可以通过hostname指定(同一局域网下可以这样设置,在类似上线的大网是不能用的,可能会出现重复),默认注册到服务中心prefer-ip-address: true              # 启用ip前缀ip-address: 127.0.0.1                 #设置前缀instance-id: ${eureka.instance.ip-address}:${server.port}   # 设置注册服务中心显示的实例名称#最小心跳续约,默认30slease-renewal-interval-in-seconds: 5#90s内没有心跳 则认为该服务挂掉 默认时间90slease-expriation-duration-in-seconds: 10

3.编写启动类,使用 @EnableEurekaClient 注解启动EurekaClient。完成启动注册

package com._01client;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApp {public static void main(String[] args) {SpringApplication.run(EurekaClientApp.class,args);}
}

目前次客户端服务已经注册到服务注册中心,已经被注册中心发现。

编写客户端服务的业务代码完成业务的访问。

package com._01client.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController {@Value("${server.port}")private String port;@RequestMapping(value = "test",method = RequestMethod.GET)public String test1(String costum){return  String.format(port);}
}

这就是微服务的调用过程,下节--------负载均衡、自定义负载均衡,和熔断机制

注意:此处有一个面试问题.

Eureka启动和客户端启动都会生成本地的服务列表缓存 ,缓存到自己的服务中以list集合进行保存,然后在自己的服务内部调用,如果所有的服务都宕机了,我们仍然可以调用,底层代码是缓存好服务列表后,每隔30s会拉取一次服务,如果Eureka宕机了在30s内都可以取调用,最大时间差是30s。

Eureka学习过程相关推荐

  1. 【公益】开放一台Eureka注册中心给各位Spring Cloud爱好者

    这是一篇博客福利! 相信很多关注Spring Cloud的爱好者们,不论是读我的系列文章和书籍还是看其他朋友们写的博客佳文,都不可避免的启动多个项目来体验Spring Cloud带来的整套微服务架构方 ...

  2. 【Spring Cloud 系列】 二、Spring Cloud Eureka 的第一印象

    Eureka : 翻译翻译,找到了!(惊讶语气) Spring CLoud 中的 Spring Cloud Eureka,用于 分布式项目中的服务治理.是对Netflix 套件中的Eureka 的二次 ...

  3. springcloud Eureka服务注册和发现

    一,Eureka基本介绍: Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现(请对比Zookeeper). Eureka 采用了 C-S 的设计架构 ...

  4. SpringCloud源码学习笔记之Eureka客户端——DiscoveryClient接口的层级结构

    1.DiscoveryClient接口和类   在SpringCloud框架中,有一个DiscoveryClient接口和一个同名的DiscoveryClient类,其中:DiscoveryClien ...

  5. 使用feign调用注解在eureka上的微服务,简单学会微服务

    使用feign调用注解在eureka上的微服务. 首先,确保所有服务(调用方与被调用方)都被注册在同一个eureka服务上. 1. 在调用方添加依赖(万事第一步,加依赖) <dependency ...

  6. 【微服务架构】SpringCloud之Eureka入门篇

    什么是Eureka 官方的介绍在这里Eureka wiki.Eureka是Netflix开源的一个RESTful服务,主要用于服务的注册发现.Eureka由两个组件组成:Eureka服务器和Eurek ...

  7. Eureka 注册中心/服务发现框架

    Eureka 注册中心/服务发现框架 Eureka注册中心/服务发现框架 如何使用构建 Eureka Server ? 加入依赖(此处以Maven为例) 创建Eureka Server 主运行类 单机 ...

  8. 目标检测中如何定义正负样本,和正负样本在学习过程中loss计算起的作用

    如何定义正负样本,和正负样本在学习过程中loss计算起的作用 正负样本定义 分类和回归head如何学习和利用划分后的正负样本(loss如何计算) 正负样本在分类中loss计算的处理 正样本在bbox ...

  9. b2b2c源码 java_java B2B2C源码电子商务平台 ---搭建Eureka注册中心

    一 创建一个Spring Boot工程,命名为eureka-server,并在pom.xml中引入必要的依赖,代码如下.愿意了解源码的朋友直接求求交流分享技术:二一四七七七五六三三 org.sprin ...

最新文章

  1. 在java中构建高效的结果缓存
  2. jzoj2136-(GDKOI2004)汉诺塔【找规律,模拟】
  3. d3 tip mysql_mysql
  4. 如何在js中使用ajax请求数据,在 JS 中怎么使用 Ajax 来进行请求
  5. Remove Duplicates from Sorted List leetcode
  6. es6 Generator函数的this
  7. 【笔记】JAVA SE
  8. 採集和输出 DeckLink Studio 4K
  9. SSM Controller 页面之间跳转 重定向,有参 无参问题
  10. 编写一个Linux 64位应用的注册机
  11. Knowledge Graph - NLP
  12. 基于java的铁路售票系统(火车票预订)ssh框架
  13. 自制光猫超级密码解密工具
  14. 射频光纤传输及宽带射频光纤传输系统介绍
  15. spring boot 核心配置文件是什么?
  16. linux sudo命令全称,你知道Linux系统中的sudo 命令吗?
  17. 邱姓女孩五行缺水取名
  18. 一文带你快速了解 Java 线上问题快速诊断神器 Arthas
  19. 从GPT到chatGPT(二):GPT2
  20. win10下安装Ubuntu18.10双系统

热门文章

  1. 通俗易懂的讲讲什么是中间件?
  2. 如何将图片转换、合并为PDF文件?
  3. antV使用教程入门
  4. 如何彻底禁止易升更新Win10自动更新
  5. 服务器dnf虚拟机多开吃显存吗,安卓模拟器多开对显卡有没有什么要求?
  6. maven报错:Failed to execute goal on project ...: Could not resolve dependencies for project ...
  7. 通信协议学习-485通信(2)
  8. android 播放音乐媒体文件(一)
  9. [激光原理与应用-17]:《激光原理与技术》-3- 激光的产生技术 与原理 - 微观粒子、能级、电子、光子、受激辐射
  10. 插入排序超详细讲解C语言