温馨提示×

温馨提示×

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

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

SpringCloud Eureka服务注册中心应用入门实例分析

发布时间:2022-07-12 14:27:15 来源:亿速云 阅读:177 作者:iii 栏目:开发技术

SpringCloud Eureka服务注册中心应用入门实例分析

引言

在微服务架构中,服务注册与发现是一个至关重要的组件。Spring Cloud Eureka 是 Netflix 开源的一个服务注册与发现组件,它能够帮助我们在分布式系统中实现服务的自动注册与发现。本文将详细介绍如何使用 Spring Cloud Eureka 构建一个简单的服务注册中心,并通过实例分析其应用。

1. Spring Cloud Eureka 简介

1.1 什么是 Eureka?

Eureka 是 Netflix 开源的一个基于 REST 的服务注册与发现组件,主要用于 AWS 云中的服务定位。Spring Cloud 将其集成到 Spring 生态系统中,使得在 Spring Boot 应用中能够轻松使用 Eureka 来实现服务注册与发现。

1.2 Eureka 的核心概念

  • Eureka Server:服务注册中心,负责管理所有注册的服务实例。
  • Eureka Client:服务提供者和消费者,负责向 Eureka Server 注册自己,并从 Eureka Server 获取其他服务的实例信息。

2. 环境准备

在开始之前,确保你已经安装了以下工具:

  • JDK 1.8 或更高版本
  • Maven 3.x 或更高版本
  • IntelliJ IDEA 或 Eclipse IDE

3. 创建 Eureka Server

3.1 创建 Spring Boot 项目

首先,我们需要创建一个 Spring Boot 项目作为 Eureka Server。可以使用 Spring Initializr 快速生成项目。

  1. 打开 Spring Initializr
  2. 选择 Maven 项目,语言选择 Java,Spring Boot 版本选择 2.5.6(或更高版本)。
  3. 在依赖项中添加 Eureka Server
  4. 点击生成并下载项目。

3.2 配置 Eureka Server

下载并解压项目后,使用 IDE 打开项目。在 src/main/resources/application.yml 文件中添加以下配置:

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • server.port:指定 Eureka Server 的端口号,默认为 8761。
  • eureka.client.register-with-eureka:设置为 false,表示 Eureka Server 不需要向自己注册。
  • eureka.client.fetch-registry:设置为 false,表示 Eureka Server 不需要从自己获取注册表。
  • eureka.client.service-url.defaultZone:指定 Eureka Server 的地址。

3.3 启动 Eureka Server

src/main/java/com/example/eurekaserver/EurekaServerApplication.java 文件中,添加 @EnableEurekaServer 注解以启用 Eureka Server:

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);
    }

}

运行 EurekaServerApplication,Eureka Server 将会启动,并监听 8761 端口。打开浏览器,访问 http://localhost:8761,你将看到 Eureka Server 的管理界面。

4. 创建 Eureka Client

4.1 创建 Spring Boot 项目

接下来,我们需要创建一个 Spring Boot 项目作为 Eureka Client。同样使用 Spring Initializr 生成项目。

  1. 打开 Spring Initializr
  2. 选择 Maven 项目,语言选择 Java,Spring Boot 版本选择 2.5.6(或更高版本)。
  3. 在依赖项中添加 Eureka Discovery Client
  4. 点击生成并下载项目。

4.2 配置 Eureka Client

下载并解压项目后,使用 IDE 打开项目。在 src/main/resources/application.yml 文件中添加以下配置:

server:
  port: 8081

spring:
  application:
    name: eureka-client

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  • server.port:指定 Eureka Client 的端口号,这里设置为 8081。
  • spring.application.name:指定服务的名称,这里设置为 eureka-client
  • eureka.client.service-url.defaultZone:指定 Eureka Server 的地址。

4.3 启动 Eureka Client

src/main/java/com/example/eurekaclient/EurekaClientApplication.java 文件中,添加 @EnableDiscoveryClient 注解以启用 Eureka Client:

package com.example.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

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

}

运行 EurekaClientApplication,Eureka Client 将会启动,并向 Eureka Server 注册自己。

4.4 验证服务注册

打开浏览器,访问 http://localhost:8761,你将看到 eureka-client 服务已经成功注册到 Eureka Server。

5. 创建服务消费者

5.1 创建 Spring Boot 项目

接下来,我们需要创建一个 Spring Boot 项目作为服务消费者。同样使用 Spring Initializr 生成项目。

  1. 打开 Spring Initializr
  2. 选择 Maven 项目,语言选择 Java,Spring Boot 版本选择 2.5.6(或更高版本)。
  3. 在依赖项中添加 Eureka Discovery ClientSpring Web
  4. 点击生成并下载项目。

5.2 配置服务消费者

下载并解压项目后,使用 IDE 打开项目。在 src/main/resources/application.yml 文件中添加以下配置:

server:
  port: 8082

spring:
  application:
    name: service-consumer

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  • server.port:指定服务消费者的端口号,这里设置为 8082。
  • spring.application.name:指定服务的名称,这里设置为 service-consumer
  • eureka.client.service-url.defaultZone:指定 Eureka Server 的地址。

5.3 创建 REST 控制器

src/main/java/com/example/serviceconsumer/ServiceConsumerApplication.java 文件中,添加 @EnableDiscoveryClient 注解以启用 Eureka Client:

package com.example.serviceconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceConsumerApplication {

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

}

接下来,创建一个 REST 控制器来调用 eureka-client 服务。在 src/main/java/com/example/serviceconsumer/controller/ConsumerController.java 文件中添加以下代码:

package com.example.serviceconsumer.controller;

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.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class ConsumerController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/call-client")
    public String callClient() {
        List<ServiceInstance> instances = discoveryClient.getInstances("eureka-client");
        if (instances.isEmpty()) {
            return "No instances available";
        }
        String uri = instances.get(0).getUri().toString();
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(uri + "/hello", String.class);
    }
}

5.4 启动服务消费者

运行 ServiceConsumerApplication,服务消费者将会启动,并向 Eureka Server 注册自己。

5.5 验证服务调用

打开浏览器,访问 http://localhost:8082/call-client,你将看到服务消费者成功调用了 eureka-client 服务并返回了结果。

6. 总结

通过本文的介绍,我们学习了如何使用 Spring Cloud Eureka 构建一个简单的服务注册中心,并通过实例分析了其应用。Eureka 作为服务注册与发现的核心组件,在微服务架构中扮演着重要的角色。希望本文能够帮助你快速入门 Spring Cloud Eureka,并在实际项目中应用它。

7. 参考资料


以上是关于 Spring Cloud Eureka 服务注册中心应用入门的实例分析。通过本文的学习,你应该能够掌握如何使用 Eureka 构建服务注册中心,并在微服务架构中实现服务的自动注册与发现。希望本文对你有所帮助!

向AI问一下细节

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

AI