Spring Cloud Eureka Server

发布时间:2020-07-22 21:30:27 作者:zhuwensheng
来源:网络 阅读:400

单实例Eureka Server配置

1. 依赖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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zws.cloud</groupId>
    <artifactId>Eurka-Server1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Eurka-Server1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <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>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
2. 配置application.yml
server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    # 下面两行表明自己是一个EurekaServer服务
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: Eureka-Server
3. 启动类
package com.zws.cloud.server;

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}
4. 访问:
http://localhost:8761/

Eureka Server集群配置

集群配置只在配置文件上有区别

1.Server1

server:
  port: 8761

eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2:8762/eureka/,http://peer3:8763/eureka/

spring:
  application:
    name: Eureka-Server

2.Server2

server:
  port: 8762

eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer3:8763/eureka/

spring:
  application:
    name: Eureka-Server

3.Server3

server:
  port: 8763

eureka:
  instance:
    hostname: peer3
  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/

spring:
  application:
    name: Eureka-Server

4./etc/hosts配置

127.0.0.1 peer1
127.0.0.1 peer2
127.0.0.1 peer3

Eureka工作原理

1. 服务注册

当Eureka Client向Eureka Server注册时,Eureka Client提供自身的元数据,比如 IP地址、 端口、运行状况指标的Url、主页地址等信息。

2. 服务续约

Eureka Client在默认的情况下会每隔30秒发送一次心跳来进行服务续约。通过服务续约来告知Eureka Serve :该 Eureka Client 仍然可用,没有出现故障。正常情况下,如果Eureka Server在90秒内没有收到Eureka Client的心跳,Eureka Server会将Eureka Client 实例从注册列表中删除。注意:官网建议不要更改服务续约的间隔时间。

3. 获取服务注册列表信息

Eureka Client从Eureka Server获取服务注册表信息,并将其缓存在本地。Eureka Client 会使用服务注册列表信息查找其他服务的信息,从而进行远程调用。该注册列表信息定时(每30秒)更新一次,每次返回注册列表信息可能与Eureka Client 的缓存信息不同,Eureka Client会自己处理这些信息。如果由于某种原因导致注册列表信.息不能及时匹配,Eureka Client 会重新获取整个注册表信息。 Eureka server 缓存了所有的服务注册列表信息,并将整个注册列表以及每个应用程序的信息进行了压缩,压缩内容和没有压缩的内容完全相同。Eureka Client和Eureka server可以使用Json和XML数据格式进行通信。在默认的情况下,Eureka client使用Json格式的方式来获取服务注册列表的信息。

4. 服务下线

Eureka client在程序关闭时可以向Eureka Server发送下线请求。发送请求后,实例信息将从 Eureka server的服务注册列表中删除。该下线请求不会自动完成,关闭时调用以下代码:该客户端的需要在程序 DiscoveryManager.getInstance().shutdowncomponent();

5. 服务剔除

在默认情况下,当 Eureka client 连续 90 秒没有向 Eureka server 发送服务续约(即心跳),EurekaServer会将该服务实例从服务注册列表删除,即服务剔除。

推荐阅读:
  1. Spring Cloud Eureka Client依赖实例分析
  2. spring cloud config

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring spring cloud spring cloud eureka

上一篇:第二讲、实例变量可见度、方法

下一篇:挂载有问题,开机启动不了,进入单用户模式进入修改fstab挂载文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》