美文网首页
springcloud(一) 之 Eureka注册中心

springcloud(一) 之 Eureka注册中心

作者: Longer_JzL | 来源:发表于2019-10-27 20:25 被阅读0次

项目结构

image.png
eureka-server:注册中心服务
order-server:订单服务
product-server:商品服务
product-api:商品服务公共信息

说明:在之后的springcloud学习历程中,很多都会以这次建的项目为基础进行学习。

搭建eureka-server项目:

步骤1:使用Spring Initializr创建SpringBoot项目,选择Cloud Discover->Eureka Server

image.png image.png image.png image.png

若使用Spring Initializr创建不了项目的话,则可以直接将一下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.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.cloud</groupId>
    <artifactId>eureka-server</artifactId>
    <version>1.0.0</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.M3</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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

步骤2.启动类上贴上@EnableEurekaServer注解

package com.example.cloud.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}


步骤3:修改application.properties为application.yml文件,添加相关配置信息(为什么使用yml方式呢,是因为官方文档描述的都是使用yml进行配置的。官方文档:https://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#spring-cloud-eureka-server

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

步骤4.运行测试,打开浏览器输入http://localhost:8761

image.png

搭建product-api项目

步骤1.搭建项目

image.png
image.png
image.png
image.png
image.png
image.png

步骤2.创建产品类

package com.example.cloud.productapi.domain;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.math.BigDecimal;

@Getter
@Setter
public class Product implements Serializable {
    private Long id;//商品id
    private String name;//商品名称
    private BigDecimal price;//商品价格
    private int stock;//商品库存
}

说明:Product类为什么要实现Serializable 接口,原因是因为网络之间的通讯是不能直接传实体对象的,需要将实体进行序列化

搭建product-server项目

步骤1.使用Spring Initializr创建SpringBoot项目,选择Cloud Discover->Eureka Discover 和 Web->Web

image.png
image.png

步骤2.引用product-api项目

      <dependency>
            <groupId>com.example.cloud</groupId>
            <artifactId>product-api</artifactId>
            <version>1.0.0</version>
      </dependency>

步骤3.把application.properties修改成application.yml,并添加配置信息 https://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#netflix-eureka-client-starter

server:
  port: 8081
spring:
  application:
    name: product-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

步骤4.启动测试,会在Eureka注册中心控制台页面中看到product-server实例

image.png

eureka的自我保护机制

注意:在上面的截图中,有这么一串红色英文:“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的自我保护机制。
Eureka Server 在运行期间会去统计心跳失败比例在 15 分钟之内是否低于 85%,如果低于 85%,Eureka Server 会将这些实例保护起来,让这些实例不会过期,但是在保护期内如果服务刚好这个服务提供者非正常下线了,此时服务消费者就会拿到一个无效的服务实例,此时会调用失败,对于这个问题需要服务消费者端要有一些容错机制,如重试,断路器等 。

我们在单机测试的时候很容易满足心跳失败比例在 15 分钟之内低于 85%,这个时候就会触发 Eureka 的保护机制,一旦开启了保护机制,则服务注册中心维护的服务实例就不是那么准确了,此时我们可以使用eureka.server.enable-self-preservation=false来关闭保护机制,这样可以确保注册中心中不可用的实例被及时的剔除(不推荐)

服务消费端如果挂了的话,默认是90秒后eureka会自动将该服务从服务列表里删除掉,但是,如果在 15 分钟之内低于 85%的服务eureka没有收到心跳,Eureka Server 会将这些实例保护起来,让这些实例不会过期,这时,即使过了90秒,该服务实例也不会从eureka里剔除。

order-server项目搭建(同product-server项目)

相关文章

网友评论

      本文标题:springcloud(一) 之 Eureka注册中心

      本文链接:https://www.haomeiwen.com/subject/tkrmvctx.html