第四章 构建服务治理Eureka【2】 搭建Eureka服务器

搭建Eureka服务器

第一步 新建父类的pom,.打开idea  新建project

 

 

选择Spring 初始化,然后在Custom里面输入http://start.spring.io

 

 

选择packaging为pom,因为这个是父类

 

 

信息输入完毕后,点击完成。

 

新建完成的父类项目的目录如下:

 

 

其中pom里面的代码如下:

 

作用主要是定义springcloud版本,定义jdk版本等,建父类的目的是 所有springcloud的子项目可以统一规范的集成父类里面的依赖,避免混乱。

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>pers.cc</groupId>
    <artifactId>springCloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>demo</name>
    <description>springCloud学习</description>

    <!-- 指定spring boot版本 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath />
        <!-- lookup parent from repository -->
    </parent>

    <!-- 指定spring cloud版本和jdk版本 -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <!-- 引入 spring-cloud模块 -->
    <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>

    <dependencies>
        <!-- 引入 spring-boot测试模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- <build>
        <plugins>
            引入 spring-boot插件模块
            <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>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

至此,父类项目我们已经搭建完毕。接下来是搭建EurekaServer服务治理服务器

 

 

选择New Project 选择,点击下一步,如下:

输入项目信息,此处都会,不再赘述。如下图:

 

选择项目依赖,EurekaServer如下,点击Next完成即可:

 

 

 

 

新建完成Eureka项目后,目录结构如下,其实有用的就三个文件,红框所示:

 

 

这三个文件的代码如下:

EurekaserverApplication.java
package com.example.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);
    }

}

 

application.properties

#端口号设置
server.port=1001
server.address=127.0.0.1

#将IP注册到Eureka Server上,如果不配置就是机器的主机名
eureka.instance.prefer-ip-address=true

#设为false,关闭自我保护
eureka.server.enable-self-preservation=false

#表示是否将自己注册到Eureka Server,默认为true
eureka.client.register-with-eureka=false

#表示是否从Eureka Server获取注册信息,默认为true
eureka.client.fetch-registry=false

# 扫描失效服务的间隔时间(单位毫秒,默认是60*1000)即60秒
eureka.server.eviction-interval-timer-in-ms=5000

#设置 eureka server同步失败的等待时间 默认 5分
#在这期间,它不向客户端提供服务注册信息
eureka.server.wait-time-in-ms-when-sync-empty=5

#设置 eureka server同步失败的重试次数 默认为 5 次
eureka.server.number-of-replication-retries=5

#自我保护系数(默认0.85)
eureka.server.renewal-percent-threshold=0.49

 

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>pers.cc</groupId>
        <artifactId>springCloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>eurekaserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eurekaserver</name>
    <description>Demo project for Spring Boot</description>


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


    </dependencies>

</project>

 

直接运行

EurekaserverApplication类即可,因为sp已经帮我们集成了tomcat.

启动没有错误后打开浏览器访问

http://localhost:1001/

会出现如下界面,说明服务搭建成功,没有问题

 

 

 

 

 

 

最后:红色部分提示有安全风险,英文的意思是:

更新低于阈值。自我保护模式已关闭。这可能无法保护

 

#自我保护系数(默认0.85)
eureka.server.renewal-percent-threshold=0.49

我们配置的是0.49

#设为false,关闭自我保护
eureka.server.enable-self-preservation=false

自我保护配置配置为false,因为此时只有一个服务治理服务器,并没有服务,因此此处报警告,因为触发了阈值,但是你的自我保护是关闭的

 

Eureka server和client之间每隔30秒会进行一次心跳通信,告诉server,client还活着。由此引出两个名词:
Renews threshold:server期望在每分钟中收到的心跳次数
Renews (last min):上一分钟内收到的心跳次数。

 

Eurake有一个配置参数eureka.server.renewalPercentThreshold,定义了renews 和renews threshold的比值,默认值为0.85。当server在15分钟内,比值低于percent,即少了15%的微服务心跳,server会进入自我保护状态,Self-Preservation。在此状态下,server不会删除注册信息,这就有可能导致在调用微服务时,实际上服务并不存在。
这种保护状态实际上是考虑了client和server之间的心跳是因为网络问题,而非服务本身问题,不能简单的删除注册信息。

-------------------------------------------------------------

泰山惹风花

风花笑泰山

不要做昙花一现

要坚若磐石,坚持到底
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

知乎关注八戒来了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值