Spring Cloud Eureka 服务注册与发现中心

Eureka 介绍

Eureka 是 Spring Cloud 的组件 Spring Cloud Netflix 中的一个模块,负责完成对微服务架构的服务治理功能。

Eureka 服务的三个角色

Eureka 服务端

Eureka 提供的服务端,负责服务的注册与发现功能,一般称为 eureka server。

服务提供者

提供服务的应用,将自己提供的服务注册到 Eureka,以供其他应用发现。

服务消费者

消费者应用从服务注册中心获取提供的服务列表, 从而使消费者可以知道去何处调用其所需要的服务。

引入依赖

pom.xml 中主要添加依赖 spring-cloud-starter-netflix-eureka-server

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

完整 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.0.3.RELEASE</version>
  </parent>

  <groupId>cloud.antoniopeng</groupId>
  <artifactId>hello-spring-cloud-dependencies</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <properties>
    <!-- Environment Settings -->
    <java.version>1.8</java.version>
  </properties>

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

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

相关配置

application.yml 中添加以下配置

spring:
  application:
    # 指定唯一服务名称以供调用
    name: hello-spring-cloud-netflix-eureka

server:
  # 指定服务器端口号
  port: 8761

eureka:
  instance:
    # 指定服务器IP地址
    hostname: localhost
  client:
    # 声明为 Eureka Server , 否则会被认为是Eureka Client
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      # 指定服务器完整路径
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Application 入口类中加上注解 @EnableEurekaServer 开启 Eureka 服务

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

启动项目

访问 Eureka 操作界面 http://localhost:8761

《Spring Cloud Eureka 服务注册与发现中心》

该页面目前没有显示任何服务,因为我们还没有将其它服务注册到 Eureka 中。

原文地址:https://www.cnblogs.com/antoniopeng/p/12687475.html