Java基础、中级、高级、架构面试资料

Spring Cloud 教程第二章 服务的注册、发现、治理之 Eureka 实战

JAVA herman 2254浏览 0评论
公告:“业余草”微信公众号提供免费CSDN下载服务(只下Java资源),关注业余草微信公众号,添加作者微信:xttblog2,发送下载链接帮助你免费下载!
本博客日IP超过2000,PV 3000 左右,急需赞助商。
极客时间所有课程通过我的二维码购买后返现24元微信红包,请加博主新的微信号:xttblog2,之前的微信号好友位已满,备注:返现
受密码保护的文章请关注“业余草”公众号,回复关键字“0”获得密码
所有面试题(java、前端、数据库、springboot等)一网打尽,请关注文末小程序
视频教程免费领
腾讯云】1核2G5M轻量应用服务器50元首年,高性价比,助您轻松上云

根据 Spring Cloud 的官方文档,我们可以看到 Eureka 排在前面。这是因为微服务架构,离不开服务的注册,治理与发现。因此这也是很多文章将 Eureka 排在第一来写的原因。本文主要写 Eureka 的单机实例,包含客户端与服务端。

本系列文章都是在 idea 下进行的代码编写,最终源码都上传到了我的 github

在开始 Eureka 之前,官方花了大力气的在宣讲 @EnableDiscoveryClient 以及 RestTemplate。因为 Eureka 就离不开这两个组件。下面我们一起真正开启 Spring Cloud 微服务之旅吧!

首先,使用 idea 或 eclipse 新建一个 maven 项目 xttblog-eureka-server。不知道具体怎么建项目的可以看我前面的文章《使用 Maven 创建 SpringBoot 项目》,如果你是导入项目到 idea 失败,可以看我的这些文章《解决 maven 项目 idea 无法导入问题》、《解决 IDEA 导入 Springboot 项目报cannot be resolved to a type错误》、《解决 idea 提示 This file is indented with tabs instead of 4 spaces 问题》然后在 pom.xml 中引入以下的配置:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.13.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.SR4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter</artifactId>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

可以看到,我这里的案例 Spring Cloud 使用的是 Edgware.SR4 版本,对应的 Spring Boot 版本也就是 1.5.13.RELEASE。

做完 pom.xml 配置后,我们在创建一个 EurekaServerApplication,作为整个服务发现程序的 main 入口。代码如下:

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

在官方文档的 How to Run a Eureka Server 章节中,代码可能和我们的不一样,不过作用都是一样的。如果你翻看了源码的话,发现两者用法相同。

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaServerApplication.class).web(true).run(args);
    }
}

最后就是在 resoure 目录下,新建一个 application.yml 文件,加入下面的配置:

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

上面的 port 就是 配置的 Eureka 的端口。hostname 就是主机名,具体可以参考这篇文章《springcloud之eureka配置——eureka.instance》。

配置解释:

  • eureka.client.registerWithEureka:表示是否将自己注册到Eureka Server,默认为true。由于当前应用就是Eureka Server,故而设置为false。
  • eureka.client.fetchRegistry:表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设置为false。
  • eureka.client.serviceUrl.defalseZone:设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址,多个地址可用,用,分割。

这里说一下,关于 Eureka Server 的集群配置,我们后面会有单独的章节来写。

做完上面的配置后,我们就可以运行项目了,等项目启动成功后,我们访问 http://localhost:8761 就能看到 Eureka 的服务管理页面。

Eureka 注册中心

从上图我们可以看到,现在一个服务也没有。本文上面的这个项目,我们是没有把它本身作为客户端来注册的。

下面我们在新建一个 Maven 工程 xttblog-eureka-client,将其作为 Eureka Client 注册到 Eureka Server。其 pom.xml 配置文件的内容如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.13.RELEASE</version>
</parent>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.SR4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
</dependencies>

然后是 application.yml,和前面的内容略有不同。

server:
  port: 8760

eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/

spring:
  application:
    name: xttblog-eureka-client

然后是启动类的代码:

package com.xttblog;

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

/**
 * EurekaClientApplication
 *
 * @author xtt
 * @date 2018/8/5 下午1:54
 */
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

现在,你可以把两个项目分别启动起来。注意,先启动第一个项目,在启动第二个项目。最后在访问 http://localhost:8761,你就会发现有一个服务注册进来了。

最后在说一个问题,你可能在测试 Eureka 的过程中,发现提示“EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.”,关于这个提示,大家可以参考我的这篇文章《解决 Eureka Server 不踢出已关停的节点问题的方法》。另外关于 Eureka 集群的知识,我们下章再讲!

本文源码可以在 https://github.com/xmt1139057136/xttblog-cloud 查看和下载!

参考资料

业余草公众号

最后,欢迎关注我的个人微信公众号:业余草(yyucao)!可加作者微信号:xttblog2。备注:“1”,添加博主微信拉你进微信群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作也可添加作者微信进行联系!

本文原文出处:业余草: » Spring Cloud 教程第二章 服务的注册、发现、治理之 Eureka 实战