zoukankan      html  css  js  c++  java
  • Spring Cloud 之 服务注册与发现

      作为微服务框架,提供服务注册发现是最基本的功能。Spring Cloud 针对服务注册发现 提供了 Eureka版本的实现 、Zookeeper版本的实现、Consul版本的实现。由于历史原因 Eureka版本是被使用最多的。但由于 Eureka 2.x的开发失败,目前还是只能使用  Eureka 1.9.x 。从目前趋势上看 Consul版本 将是未来的主流。本文还是介绍Eureka的使用。

           为了演示Eureka的使用,需要写三个项目,一个Server端项目、二个Client端项目( 分别是服务提供者provider和服务消费者consumer )

           一. Server端项目

      1. 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.1.3.RELEASE</version>
            <relativePath/> 
        </parent>
        .............
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
        </properties>
        
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
        </dependencies>
    
    </project>

      2. 项目配置文件 application.yml

    spring: 
      application: 
        name: test          
    server:
      port: 8761
    
    eureka:
      instance:
        hostname: eurekaServer      //注意:测试时应在/etc/hosts文件中设置hostname与实际ip的映射
        appname: eurekaServer    //代表服务的标识,如果没有配置则取spring.application.name的配置值
      client:
        registerWithEureka: true   //则向 defaultZone 配置的地址去进行服务注册。
        fetchRegistry: true     //是否从其注册的eureka上获取注册信息
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

     注意:在上面的配置中hostname和appname配的都是同样的值,但是含义和作用不一样,hostname的配置其实是一个域名,为了让其他的server能与之通信。而appname 是一个服务应用的标识,代表着一个注册的服务。

     3.启动类代码

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

       

     以上演示的只是单个Server的情况。在生产环境下,应该启动多个Server(在不同的机器上),每个Server启动后,都会有一个注册中心URL。而它的 defaultZone属性必须要指向一个Server的注册中心地址,这个URL地址可以是它自身的注册中心URL地址,也可以是其它注册中心的URL,如果是集群高可用模式,defaultZone应配成指向其他的注册中心URL,这样运行时每个Server就会以应用服务的形式向其他的Server进行注册,并和该Server 进行注册信息的同步复制。

    在eureka的源码地址上 https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance 有一张架构图:

    从上图能看出这么几点:

      1. Eureka Server集群之间,每个Server的地位都是对等的,相互之间两两进行注册信息的复制

            2. 应用服务(即Eureka Client)启动后向 Eureka Server集群 进行如下操作:

      • 注册
      • 续租
      • 取消 
      • 获取注册信息  

      3. 在Eureka Client之中,服务消费者通过http的方式远程调用服务生产者提供的Restful服务

  • 相关阅读:
    LeetCode-Palindrome Partitioning II
    LeetCode-Palindrome Partitioning
    LeetCode-Permutation Sequence
    LeetCode-Anagrams
    LeetCode-Text Justification
    LeetCode-Best Time to Buy and Sell Stock III
    LeetCode-Best Time to Buy and Sell Stock II
    LeetCode-Best Time to Buy and Sell Stock
    LeetCode-N-Queens II
    BZOJ 5390: [Lydsy1806月赛]糖果商店
  • 原文地址:https://www.cnblogs.com/hzhuxin/p/10563616.html
Copyright © 2011-2022 走看看