SpringCloud:Eureka服务注册与发现

郎家岭伯爵 2022年06月06日 483次浏览

背景

前面我们搭建了一个简单的微服务,接下来我们将使用第一个组件:Eureka。

Eureka在SpringCloud微服务架构中通常用作注册中心

Eureka包含两个EurekaServer,还有一个与之交互的客户端称之为EurekaClient

如上图所示,其中:

  • ApplicationServer表示服务提供者;
  • ApplicationClient表示服务消费者;
  • MakeRemoteCall表示远程调用。

服务在Eureka上注册,然后每隔30秒发送心跳来更新它们的租约。如果客户端不能多次续订租约,那么它将在大约90秒内从服务器注册表中剔除。注册信息和更新被复制到集群中的所有Eureka节点。来自任何区域的客户端都可以查找注册表信息(每30秒发生一次)来定位它们的服务(可能在任何区域)并进行远程调用。

实现

说明:本文案例是在前一篇的基础上来实现的。

EurekaServer注册中心

创建Eureka模块

注意:

  • 此模块需要创建在总项目的路径下。

编辑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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.langjialing</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-eureka-7001</artifactId>

    <properties>
        <maven.compiler.source>15</maven.compiler.source>
        <maven.compiler.target>15</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <!-- 热部署工具 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

    </dependencies>

</project>

编辑application.yaml文件

server:
  port: 7001

# Eurake配置
eureka:
  instance:
    hostname: localhost # Eureka服务端的实例名称
  client:
    register-with-eureka: false # 是否需要向Eureka注册中心中注册自己
    fetch-registry: false # 如果为false,则表示自己为注册中心
    service-url: # 监控页面
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

创建启动类

package com.langjialing.springcloud;

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

@SpringBootApplication
@EnableEurekaServer // Eureka服务端的启动类,表示接受别的服务注册进来
public class EurekaServer_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class,args);
    }
}

测试模块

访问localhost:7001进行测试:

模块的文件路径

服务注册及配置

本章节的服务提供者是指springcloud-provider-dept-8001模块。

添加Eureka依赖

在服务提供者的pom.xml中添加Eureka的依赖(注意版本与EurekaServer的版本一致,避免报错):

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
     <version>1.4.6.RELEASE</version>
</dependency>

添加Eureka配置

在服务提供者的application.yaml中添加Eureka的配置信息:

# Eureka的配置,服务注册在哪里
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/  # 与EurekaServer中的注册中心地址一致

启动类开启注解

在服务提供者的主启动类开启注解:

测试模块

访问localhost:7001

完善监控信息

添加依赖

在服务提供者的pom.xml中添加依赖:

<!-- 此依赖用于完善监控信息 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

添加配置

在服务提供者的application.yaml中添加配置:

# info配置
info:
  app.name: langjialing-springcloud
  url.name: https://潇雅.com

测试功能

重启服务服务后进行访问:

服务发现

功能代码

在服务提供者的DeptController中添加如下功能代码:

package com.langjialing.springcloud.controller;

import com.langjialing.springcloud.pojo.Dept;
import com.langjialing.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//提供Restful服务
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private DiscoveryClient client;

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept){
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return deptService.queryById(id);
    }

    @GetMapping("/dept/list")
    public List<Dept> queryAll(){
        return deptService.queryAll();
    }

    @GetMapping("/dept/discovery")
    public Object discovery(){

        // 获取微服务列表的清单
        List<String> services = client.getServices();
        System.out.println("discovery=>" + services);

        // 通过具体的微服务id,applicationName得到一个具体微服务的信息
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(
                    instance.getHost()+"\t"+
                    instance.getPort()+"\t"+
                    instance.getUri()+"\t"+
                    instance.getServiceId()+"\t"
            );
        }

        return this.client;
    }
}

启动类开启注解

测试功能

重启服务后访问localhost:8001/dept/discovery

总结

至此,我们实现了Eureka服务注册与发现。

接下来我们将实现Eureka集群及服务集群的搭建,实现微服务的高可用

赞助页面示例