【Spring Cloud】第一篇 Service Discovery | 服务发现 - Eureka

Eureka 提供服务的注册,服务可以通过注册到Eureka然后被其他应用调用。

看到Spring Cloud 的文档里面是先讲的是Spring Cloud Config ,为了方便,或者说参考其他大佬的教程,我也会把Config放到后面写。

word & phrase

  • include 确实是包含的意思,但是我觉得翻译(根据语境去翻译)成使用是不是更舒服些
  • setting up 配置
  • provides 提供
  • meta-data 元数据
  • such as 例如
  • Note 注意

Spring Cloud Netflix

Netflix 是一家媒体提供商,应该很厉害!

官网文档中先列出的是Eureka客户端,我尝试先写Eureka Client 发现会提示以下错误,应该是在请求 Eureka server的时候报错, 因为我们根本没有创建。所以我先看Eureka server

NFO 25402 --- [main] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
ERROR 25402 --- [main] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error

说明

官方说明 Finchley的构建和工作环境应该是 Spring Boot 2.0.x, 预计不会在Spring Boot 1.5.x 中使用。

Finchley builds and works with Spring Boot 2.0.x, and is not expected to work with Spring Boot 1.5.x.

Spring Cloud 需要使用Spring Boot,如果不会创建Spring Boot项目的看这里《Spring Boot | 使用Spring Initializr快速创建》!

创建项目

  • 新建好Spring Boot后,删除src文件
    【Spring Cloud】第一篇 Service Discovery | 服务发现 - Eureka_第1张图片

  • pom.xml 中添加 Spring Cloud 配置


<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.0modelVersion>

    <groupId>com.cyinfotechgroupId>
    <artifactId>sc-f-e-01artifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>sc-f-e-01name>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.5.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
    
         <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Finchley.SR1version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

Service Discovery: Eureka Server | 服务发现:Eureka服务端

How to Include Eureka Server | 项目中如何包含 Eureka Server

  • 右键项目 > New > Module 新建 eureka-server 子工程
    【Spring Cloud】第一篇 Service Discovery | 服务发现 - Eureka_第2张图片
    【Spring Cloud】第一篇 Service Discovery | 服务发现 - Eureka_第3张图片

  • 修改父工程pom.xml文件, 添加子工程。

    <modules>
        <module>eureka-servermodule>
    modules>

To include Eureka Server in your project, use the starter with a group ID of org.springframework.cloud and an artifact ID of spring-cloud-starter-netflix-eureka-server. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

在项目中使用Eureka Server,需要配置 pom.xml, 根据你当前的Spring Cloud 版本在Spring Cloud Project page中可以查看详细说明。

eureka-serverpom.xml


<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.0modelVersion>

    <groupId>com.cyinfotechgroupId>
    <artifactId>eureka-serverartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>eureka-servername>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>com.cyinfotechgroupId>
        <artifactId>sc-f-e-01artifactId>
        <version>0.0.1-SNAPSHOTversion>
        <relativePath/> 
    parent>

    <dependencies>
        
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
        
    dependencies>

project>

How to Run a Eureka Server | 如何启动 Eureka Server

在启动类添加 注解 @EnableEurekaServer

  • Modify EurekaServerApplication
package com.cyinfotech.eurekaserver;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

Standalone Mode | 单机模式

  • Add application.yml
server:
  port: 8761 # 端口

eureka:
  instance:
    hostname: localhost # 主机
  client:
    registerWithEureka: false #是否注册自己
    fetchRegistry: false #是否注册自己
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 服务地址
  • Start
    【Spring Cloud】第一篇 Service Discovery | 服务发现 - Eureka_第4张图片

  • Open http://127.0.0.1:8761/ in your browser
    【Spring Cloud】第一篇 Service Discovery | 服务发现 - Eureka_第5张图片

Instances currently registered with Eureka 是空的, 因为有没有服务注册!

Service Discovery: Eureka Clients | 服务发现:Eureka客户端

Eureka客户端可以理解为服务注册实际项目中需要曝光的服务。

How to Include Eureka Client | 如何使用Eureka客户端

  • 右键项目 > New > Module 新建 eureka-client 子工程,和添加 server 一样

  • 修改父工程pom.xml文件, 添加子工程。

    <module>eureka-clientmodule>

To include the Eureka Client in your project, use the starter with a group ID of org.springframework.cloud and an artifact ID of spring-cloud-starter-netflix-eureka-client. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

要使用Eureka Client 就要添加配置文件,完整pom.xml配置文件如下:


<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.0modelVersion>

    <groupId>com.cyinfotechgroupId>
    <artifactId>eureka-clientartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>eureka-clientname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>com.cyinfotechgroupId>
        <artifactId>sc-f-e-01artifactId>
        <version>0.0.1-SNAPSHOTversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

    dependencies>

project>

Registering with Eureka | 注册 Eureka

When a client registers with Eureka, it provides meta-data about itself — such as host, port, health indicator URL, home page, and other details. Eureka receives heartbeat messages from each instance belonging to a service. If the heartbeat fails over a configurable timetable, the instance is normally removed from the registry.

当注册Eureka时,它提供一些包括自己的源数据,例如:主机、端口、监听(心跳)地址、主页和其它详细信息。Eureka 通过每个实例的服务接受心跳消息。 如果心跳在配置的时间失败结束,那这个实例通常会删除注册。

修改启动Eureka Client 启动类

package com.cyinfotech.eurekaclient;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class EurekaClientApplication {

    @RequestMapping("/")
    public String home() {
        return "Hello world";
    }

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

Note that the preceding example shows a normal Spring Boot application. By having spring-cloud-starter-netflix-eureka-client on the classpath, your application automatically registers with the Eureka Server. Configuration is required to locate the Eureka server, as shown in the following example:

注意前面的正常的 Spring boot 程序, 你的应用程序中添加spring-cloud-starter-netflix-eureka-client后将自动注册到Eureka Server, 如下所示:

server:
  port: 8781
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

启动步骤:
1.EurekaServerApplication
2.EurekaClientApplication

http://localhost:8761/
【Spring Cloud】第一篇 Service Discovery | 服务发现 - Eureka_第6张图片

http://192.168.5.165:8781/
【Spring Cloud】第一篇 Service Discovery | 服务发现 - Eureka_第7张图片

以上 Eureka 的服务端和客户端(注册与发现)就讲完了,我也是第一次这么认真看官方文档,其实看下来发现也不难,其他人的文档里面也无非就直译了官方文档,但是每个人的理解不一样,所以还是推荐自己去看原版文档, 没有想象的那么难。

欢迎关注我的公众号,跟我留言。


博客地址:https://blog.aprcode.com/sc-f-e-01/
教程源码Github地址:sc-f-e-01
教程源码Gitee地址:sc-f-e-01

你可能感兴趣的:(【Spring Cloud】第一篇 Service Discovery | 服务发现 - Eureka)