SpringCloud--服务治理Eureka

一、Eureka概述

1、Eureka特点

  • 只需通过简单引入依赖和注解配置,就能让SpringBoot构建的微服务应用轻松地与Eureka服务治理体系进行整合。
  • Eureka负责服务治理,即:微服务实例的自动化注册与发现。服务注册与发现都是以应用服务名的形式实现。
  • 遵循AP原则(高可用,分区容错性),使用了自我保护机制保证了高可用。实现服务发现和故障转移。

 

2、Eureka两大组件三大角色

  • 两大组件:Eureka Server(提供注册服务)、 Eureka Client(生产消费服务)。
  • 三大角色:Service Provider和Service Consumer不是严格的概念,Service Consumer也可以随时向Eureka Server注册,来让自己变成一个Service Provider。Eureka Client启动后,马上向服务注册中心注册。同时,会从Eureka Server上获取所有实例的注册信息,包括IP地址、端口等,并缓存到本地。这些信息默认每30秒更新一次。如果与Eureka Server通信中断,Service Consumer仍然可以通过本地缓存与Service Provider通信。
    • Eureka Server:服务的注册中心,负责维护注册的服务列表。
    • Service Provider:服务提供方,作为一个Eureka Client,向Eureka Server做服务注册、续约和下线等操作,注册的主要数据包括服务名、机器ip、端口号、域名等等。
    • Service Consumer:服务消费方,作为一个Eureka Client,向Eureka Server获取Service Provider的注册信息(获取所有服务的实例清单缓存本地),并通过远程调用与Service Provider进行通信。   
  • Eureka Client通过发送心跳请求实现服务注册、续约。另外,Eureka Server还需要以心跳的形式去监测服务清单中的服务是否可用,若不可用需要从服务清单中剔除,达到排除故障服务的效果。

 

二、搭建服务注册中心-Eureka Server

1、pom.xml

  首先,创建一个基础的Spring Boot工程,并引入必要的pom依赖:

<?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.2.RELEASE</version>
        <relativePath/>  
    </parent>
    
    <groupId>com.github.gavincoder</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name> 

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--eureka-server服务端引入 -->
       <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
       </dependency>
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

 

 2、application.yml

#server
server:
  port: 8888

#eureka
eureka:
  instance:
    hostname: localhost
  client: #声明自己是个服务端
    register-with-eureka: false #false表示不向注册中心注册自己
    fetch-registry: false       #false表示自己就是注册中心,职责是维护服务清单,不参与检索
    service-url:                #设置eureka server对外暴露的地址
      default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

3、启动类

  通过@EnableEurekaServer注解开启服务注册中心功能。

package com.github.gavincoder.server;

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


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

}

注:

 

4、运行效果

  访问Eureka Server对外暴露的注册中心地址:http://${eureka.instance.hostname}:${server.port}

 

 三、搭建Eureka-Client---服务提供者

1、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.2.RELEASE</version>
        <relativePath/>  
    </parent>
    <groupId>com.github.gavincoder</groupId>
    <artifactId>product</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>product</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

 注:

  • 必须添加web依赖,不然Eureka Client启动时自动停止,报错:“Shutting down DiscoveryClient ...”
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  

 

2、application.yml

#server
server:
  port: 8886
  
#spring
spring:
  application:
    name: eureka-client-product-service

#eureka
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
#eureka:
#  instance:
#    service-url:                
#      default-zone: http://localhost:8888/eureka/

注:

  • yml属性配置文件务必检查是否必要属性都是有效状态,无效状态无法读取该属性值。
  •  示例:如下配置造成eureka client无法找到注册中心地址进行注册,注册失败。

启动报错:com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect

 

3、启动类

  添加注解:@EnableEurekaClient

package com.github.gavincoder.product;

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

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

}

 

四、搭建Eureka-Client---服务消费方

1、pom.xml

  ---同服务提供方

 

2、application.yml

#server
server:
  port: 8885
  
#spring
spring:
  application:
    name: eureka-client-order-service

#eureka
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/

 

3、启动类

package com.github.gavincoder.order;

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

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

}

 

四、Eureka运行效果

 

 五、实战代码

  实战实例代码已经上传至我的代码库,地址见博客公告中的GitHub、Gitee。

 

 

 

 

 

 

 

 

 

 

posted @ 2019-06-16 17:09  马非白即黑  阅读(328)  评论(0编辑  收藏  举报