zoukankan      html  css  js  c++  java
  • Spring Cloud02:Eureka Server注册中心

    一、Eureka是什么

    Eureka是Netflix开源的基于REST的服务治理方案,Spring Cloud集成了Eureka,提供服务治理和服务发现功能,可以和基于Spring Boot搭建的微服务应用轻松完成整合。

    二、Spring Cloud Eureka的组成

    1. Eureka Server,注册中心

    2. Eureka Client,所有要进行注册的微服务通过Eureka Client连接到Eureka Server,完成注册

    三、实战!快速搭建注册中心

    1.创建一个父工程,pom.xml配置如下

    <parent>
      	<groupId>org.springframework.boot</groupId>
      	<artifactId>spring-boot-starter-parent</artifactId>
      	<version>2.0.7.RELEASE</version>
      </parent>
      
      <dependencies>
      	<dependency>
      		<groupId>org.springframework.boot</groupId>
      		<artifactId>spring-boot-starter-web</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>
    

    2.在父工程下创建一个子工程Module,pom.xml配置如下

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

    3.在子工程下创建application.yml,添加Eureka相关的配置如下

    server:
      # 当前Eureka Server服务端口
      port: 8761
    eureka:
      client:
        # 是否将当前的Eureka Server服务作为客户端进行注册
        register-with-eureka: false
        # 是否获取其他Eureka Server服务的数据
        fetch-registry: false
        
        service-url:
          # 注册中心的访问地址
          defaultZone: http://localhost:8761/eureka/
    

    属性说明

    * server.port:当前Eureka Server服务端口

    * eureka.client.register-with-eureka:是否将当前的Eureka Server服务作为客户端进行注册

    * eureka.client.fetch-registry:是否获取其他Eureka Server服务的数据

    * eureka.client.service-url.defaultZone:注册中心的访问地址

    4.创建启动类

    package com.frr;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class EurekServerApplication {
    	public static void main(String[] args) throws Exception {
    		SpringApplication.run(EurekServerApplication.class, args);
    	}
    
    }
    

    注解说明

    * @SpringBootApplication:声明该类是Spring Boot服务的入口

    *@EnableEurekaServer:声明该类是一个Eureka Server微服务,提供服务注册和服务发现的功能,即注册中心

    5.启动成功后的界面

    img

    四、总结

    1.新建父工程,在pom文件中加入公用的依赖

    2.在父工程中创建一个子工程Module,在Module的pom文件中添加自己需要的组件依赖

    3.在Spring Boot里面添加它的端口和Eureka相关的配置

    4.最后创建启动类,在启动类添加注解,让当前这个工程成为一个Eureka Server

  • 相关阅读:
    【翻译/介绍】jump consistent hash:零内存消耗,均匀,快速,简洁,来自Google的一致性哈希算法 [2015-03-13]
    现代密码学实践指南[2015年]
    本博客迁走了
    高性能web系统的架构和系统优化
    vs 2013调试的时候重启的解决方案
    年会与项目管理
    javascript 关闭窗口,弹出新窗口并带有确认关闭对话框解决办法
    成长
    POCO exception
    通过OpenGL ES在iOS平台实践增强现实(二)
  • 原文地址:https://www.cnblogs.com/kylinxxx/p/14443062.html
Copyright © 2011-2022 走看看