二、注册核心

1.eureka介绍

eureka是微服务零碎的外围服务
作用:注册和发现
所有服务启动,都要把本人的地址注册到注册核心;要调用其余服务,从注册核心取得注册表,来发现其余服务。
服务id
主机地址
item-service---localhost:8001
user-service---localhost:8101
order-service---localhost:8201

2.eureka的工作机制:

  • 注册

    • 服务提供者启动时,会一次次重复尝试向eureka注册,直到注册胜利为止
  • 拉取

    • 消费者每30秒拉取一次注册表,来刷新注册表
  • 心跳

    • 服务提供者,每30秒向eureka发送一次心跳数据
    • eureka服务器如果间断3次收不到一个服务的心跳,会删除这个服务的注册信息
  • 自我保护模式

    • 如果因为网络不稳固,15分钟内,85%的服务器呈现心跳异样,会主动进入自我保护模式
    • 所有注册信息都不会删除
    • 等网络复原后,能够主动退出保护模式,恢复正常
    • 开发调试期间,能够禁用保护模式,防止影响测试

3.搭建eureka服务器

3.1 创立eurekaserver我的项目

3.2 编辑pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.2.1.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>cn.tedu</groupId>    <artifactId>sp05-eureka</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>sp05-eureka</name>    <description>Demo project for Spring Boot</description>    <properties>        <java.version>1.8</java.version>        <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>    </properties>    <dependencies>        <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-test</artifactId>            <scope>test</scope>            <exclusions>                <exclusion>                    <groupId>org.junit.vintage</groupId>                    <artifactId>junit-vintage-engine</artifactId>                </exclusion>            </exclusions>        </dependency>    </dependencies>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>${spring-cloud.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

3.3 批改application.yml

首先将application.propertie改为application.yml

spring:  application:    name: eureka-server    server:  port: 2001  eureka:  server:    enable-self-preservation: false  instance:    hostname: eureka1  client:    register-with-eureka: false    fetch-registry: false
  • eureka 集群服务器之间,通过 hostname 来辨别
  • eureka.server.enable-self-preservation
    eureka 的自我爱护状态:心跳失败的比例,在15分钟内是否超过85%,如果呈现了超过的状况,Eureka Server会将以后的实例注册信息爱护起来,同时提醒一个正告,一旦进入保护模式,Eureka Server将会尝试爱护其服务注册表中的信息,不再删除服务注册表中的数据。也就是不会登记任何微服务
  • eureka.client.register-with-eureka=false
    不向本身注册
  • eureka.client.fetch-registry=false
    不从本身拉取注册信息
  • eureka.instance.lease-expiration-duration-in-seconds
    最初一次心跳后,距离多久认定微服务不可用,默认90

3.4 批改主程序


  • 增加 @EnableEurekaServer
package cn.tedu.sp05;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class Sp05EurekaApplication {    public static void main(String[] args) {        SpringApplication.run(Sp05EurekaApplication.class, args);    }} 

3.4 批改hosts文件增加eureka域名映射

C:WindowsSystem32driversetchosts
增加内容:

127.0.0.1       eureka1127.0.0.1       eureka2` 

3.5 启动,并拜访测试

  • http://eureka1:2001

4. service provider服务提供者

  • 批改 item-service、user-service、order-service,把微服务注册到 eureka 服务器

4.1 pom.xml 增加eureka依赖

<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

4.2 application.yml 增加eureka注册配置

eureka:  client:    service-url:      defaultZone: http://eureka1:2001/eureka
  • eureka.instance.lease-renewal-interval-in-seconds
    心跳间隔时间,默认 30 秒
  • defaultZone,默认地位,能够批改为具体地理位置,比方:beiJing, shangHai, shenZhen 等,示意 eureka 服务器的部署地位, 须要云服务器提供
  • eureka.client.registry-fetch-interval-seconds
    拉取注册信息间隔时间,默认 30 秒

4.3 启动服务,在eureka中查看注册信息

  • http://eureka1:2001