配置服务发现组件eureka

配置服务发现eureka并且将提供者和消费者注册到服务发现组件

上一篇使用springcloud微服务
下一篇eureka server 集群

配置服务发现组件 服务端 server 客户端 client

对比着dubbo来看
dubbo— 注册器—zookeeper
springcloud — 服务发现组件 — ecurka (server,client)

搭建 eureka server

springcloud-starter-netflix-eureka-server

搭建 eureka client

springcloud-starter-netflix-eureka-client

微服务注册与发现eureka

服务发现组件介绍

通过前文的介绍,我们知道硬编码提供者地址的方式有不少问题。要想解决这些问题, 服务消费者需要一个 强大的服务发现机制,服务消费者使用这种机制获取服务提供者的网络信息。不仅如此,即使服务提供者的信 息发生变化, 服务消费者也无须修改配置文件。

服务发现组件提供这种能力。在微服务架构中,服务发现组件是一个非常关键的组件。 使用服务发现组件后的架构图,如图:

在这里插入图片描述
服务提供者、服务消费者、服务发现组件这三者之间的关系大致如下:

各个微服务在启动时,将自己的网络地址等信息注册到服务发现组件中,服务发现 组件会存储这些信息。 服务消费者可从服务发现组件查询服务提供者的网络地址,并使用该地址调用服务 提供者的接口。 各个微服务与服务发现组件使用一定机制(例如心跳)通信。服务发现组件如长时 间无法与某微服务实例通 信,就会注销该实例。微服务网络地址发生变更(例如实例增减或者 IP 端口发生变化等)时, 会重新注册到服务发现组件。使用 这种方式, 服务消费者就无须人工修改提供者的网络地址了。

综上, 服务发现组件应具备以下功能:

服务注册表:是 服务发现组件的核心,它 用 来记录各个微服务的信息, 例如微服务的名称、IP、端口 等。服务注册表提供查询API 和管理 API , 查询API 用于查询可用的微服务实例, 管理 API 用于服 务的注册和注销。

服务注册与服务发现: 服务注册是指微服务在启动时, 将自己的信息注册到服务发现组件上的过程。服务发 现是指查询可用微服务列表及其网络地址的机制。
服务检查: 服务发现组件使用一定机制定时检测已注册的服务, 如发现某实例长时间无法访问, 就会从服 务注册表中移除该实例。
综上, 使用服务发现的好处是显而易见的。Spring Cloud 提供了多种服务发现组件的支待, 例如 Eureka、Consul 和 Zookeeper 等。本书将以Eureka 为例, 为大家详细讲解服务注册与发现。

Eureka 包含两个组件: Eureka Server 和 Eureka Client , 它们的作用如下:

Eureka Server 提供服务发现的能力, 各个微服务启动时, 会向 Eureka Server 注册自己的信息(例 如 IP、端口、微服务名称等 ), Eureka Server 会存储这些信息。

Eureka Client 是一个 Java客户端,用 于简化与 Eureka Server 的交互。

微服务启动后, 会周期性( 默认 30 秒)地向 Eureka Server 发送心跳以续约自己的“租期”。 如果 Eureka Server 在一定时间内没有接收到某个微服务 实例的心跳, Eureka Server将会注销该实 例(默认 90 秒)。

编写Eureka Server

默认情况下, Eureka Server 同时也是Eureka Client。多个 Eureka Server 实例, 互相之间通 过复制的方式, 来实现服务注册表中数据的同步。

Eureka Client 会缓存服务注册表中的信息。这种方式有一定的优势一—首先,微服务无须每次请求都查询 Eureka Server, 从而降低了 Eureka Server 的压力; 其次 , 即使Eureka Server 所有节点都宕掉, 服务消费者依然可以使用缓存 中的信息找到服务提供者 并完成调用。

综上, Eureka 通过 心跳检查、客户端缓存等机制, 提高了系统的灵活性、可伸缩 性和可用性。

1 创建一个eureka的springboot项目
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.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.itzz</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>
    <packaging>jar</packaging>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <!--<version>2.1.5.RELEASE</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</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>
            <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR3</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>

2 在主类上加入@EnableEurekaServer注解,声明这是一个Eur eka Server

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.yml(把后缀改为yml)中添加以下内容。

server:
  port: 8761
  enableSelfPreservation: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
    register-with-eureka: false
    fetch-registry: false

微服务注册与发现

简单了解一下 application.yml 中的配置属性:

eureka.client.registerWithEureka: 表示是否将自己注册到 Eureka Server , 默认为true。由于当前应用就是EurekaServer, 故而设为false。 eureka.client.fetchRegistry: 表示是否从 Eureka Server 获取注册信息, 默认为true。因为 这是一个单点的Eureka Server, 不需要同步其他的Eureka

Server 节点的数据, 故而设为 false。 eureka.client.serviceUrl.defaultZone : 设置与
EurekaServer 交互的地址, 查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka 这样一个 Eureka Server 就编写完成了。 测试启动 Eureka Server, 访问http://localhost:8761/

Eureka Server 的首页展示了很多信息, 例如当前实例的系统状态、注册到 Eureka Server 上的服务 实例、常用信息、实例信息等。显然, 当前还没有任何微服务实例被注册到Eureka Server 上。

将提供者、消费者微服务注册到eurekaServer

引入jar包

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>

yml配置文件中内容

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

在主类上加上注解@EnableEurekaClient注解

@SpringBootApplication
@EnableEurekaClient
public class SpringcloudConsumerApplication {
    
    
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
    
    
        return new RestTemplate();
    }
    public static void main(String[] args) {
    
    
        SpringApplication.run(SpringcloudConsumerApplication.class, args);
    }

}

效果如下:你的页面可能跟我不大相似,但是都大差不差吧,只要能看到application下有提供者和消费者的信息就可以了
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_39095899/article/details/107456594