Eureka -- 入门案例(3)

父 pom

<packaging>pom</packaging>

<!-- 父工程是 2.1.0.RELEASE 版本的 boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
        <!-- 设置全局 SpringCloud 依赖版本为 F.RELEASE -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- 设置全局依赖 SpringBoot、Web -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

建立 Eureka Server 子项目

源码:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-eureka/spring-cloud-eureka-server-simple

父pom中建立 Eureka Server 子项目 module:spring-cloud-eureka-server

pom 依赖

<!-- 父工程 -->
<parent>
    <groupId>com.laiyy.gitee</groupId>
    <artifactId>spring-cloud</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<dependencies>
    <!-- 引入 eureka server 依赖,注意不能少了 starter -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

启动类

@SpringBootApplication
@EnableEurekaServer     // 开启 Eureka Server 
public class SpringCloudEurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaServerApplication.class, args);
    }
}

配置文件

spring:
  application:
    name: eureka-server # 服务名称
server:
  port: 8761 # 服务端口

eureka:
  instance:
    hostname: localhost # host name
  client:
    fetch-registry: false # 是否获取注册表
    service-url:
      defaultZone: http://localhost:${server.port:8761}/eureka/ # 默认 Zone
    register-with-eureka: false # 是否注册自己
  server:
    enable-self-preservation: false # 是否开启自我保护,默认 true。在本机测试可以使用 false,但是在生产环境下必须为 true

验证 Eureka Server 启动结果

在浏览器输入: http://localhost:8761 ,进入 Eureka Server 可视化页面

Eureka Server UI 提示信息

在 Eureka Server 检测到异常时,会在中间以红色加粗字体提示信息。

在没有 Eureka Client 或 Eureka Server 检测心跳的阈值小于指定阈值,且关闭自我保护时:

RENEWALS ARE LESSER THAN THE THRESHOLD. THE SELF PRESERVATION MODE IS TURNED OFF.THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.
提示说明了:1、eureka client 的心跳发送次数小于阈值(没有client,肯定小于);2、自我保护被关闭了

在有 Eureka Client,且关闭了自我保护时:

THE SELF PRESERVATION MODE IS TURNED OFF.THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.

在没有 Eureka Client 或 Eureka Server 检测心跳的阈值小于指定阈值,且开启了自我保护时:

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

在有 Eureka Client 且 Eureka Server 检测心跳的阈值大于指定阈值,且开启了自我保护时,Eureka Server 会认为整个服务正常,不会有任何信息提示。

建立 Eureka Client 项目

源码:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-eureka/spring-cloud-eureka-client-simple

在 在父pom中建立 Eureka Server 子项目 module:spring-cloud-eureka-client-simple,建立一个简单的 eureka client 工程

pom

<!-- parent 都和 Eureka Server 一致,不再赘述 -->

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

启动类

// EurekaClient、DiscoveryClient 本质上都是注册到服务中心的实现,EurekaClient 只针对 Eureka 使用,
// DiscoveryClient 针对不同的注册中心都可以使用。可以说 DiscoveryClient 的 EurekaClient 的一个抽象
@SpringBootApplication
@EnableDiscoveryClient //@EnableEurekaClient 
public class SpringCloudEurekaClientSimpleApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaClientSimpleApplication.class, args);
    }
}

配置文件

spring:
  application:
    name: spring-cloud-eureka-client-simple # 工程名称,也是注册到 server 后的实例名称

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka      # 指定 Zone,需要与 Eureka Server 一样
  instance:
    prefer-ip-address: true # 使用 ip 注册,默认为 false。为 false 时是 机器名
    instance-id: ${spring.application.name}:${server.port} # 注册到 server 后显示的名字,默认是 机器名:name:端口

server:
  port: 8081 # 端口

在 Eureka Server 验证服务注册

可以看到,eureka client 已经注册成功。 status 大致有 5 个状态:UP(正常运行)、DOWN(停机)、STATING(正在启动)、OUT_OF_SERVICE(下线)、UNKNOWN(为止)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章