如何应用SpringCloud集成Eureka注册中心

发布时间:2021-11-16 13:35:24 作者:iii
来源:亿速云 阅读:116

本篇内容主要讲解“如何应用SpringCloud集成Eureka注册中心”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何应用SpringCloud集成Eureka注册中心”吧!

介绍

SpringCloud中的核心组件Eureka提供了服务注册和服务发现功能,管理分布式系统中的各种服务,比如注册、发现、熔断、负载均衡等。

版本说明

SpringCloud:Greenwich.RELEASE
SpringBoot :2.1.2.RELEASE

实现步骤

1.引入依赖

   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>Greenwich.RELEASE</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

2.添加配置application.properties

   # 项目端口号
   server.port=6001
   # 项目名称
   spring.application.name=service-registry
   # 是否把自己作为服务注册到其他服务注册中心,默认true
   eureka.client.register-with-eureka=false
   # 是否从其他的服务中心同步服务列表,默认true
   eureka.client.fetch-registry=false
   # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
   eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

3.启动类添加启动注册中心

    @SpringBootApplication
    @EnableEurekaServer // 启用Eureka服务
    public class Application {
        public static void main(String[] args){
            SpringApplication.run(Application.class,args);
        }
    }

集群注册中心

在分布式系统中,注册中心是最重要的基础部分,为了防止因为注册中心故障导致毁灭性灾难,必须保证注册中心的高可用性,我们可以使用集群的方式。

1.创建项目

按照以上介绍创建3个项目
registry-1
registry-2
registry-3
2.修改操作系统的host文件

添加如下配置:

   127.0.0.1       service-registry1
   127.0.0.1       service-registry2
   127.0.0.1       service-registry3

3、配置文件改造

   #registry-1.application.properties
   # 项目端口号
   server.port=6001
   # 项目名称
   spring.application.name=service-registry
   
   #服务注册中心相互注册一定要显示的设置register-with-eureka 和fetch-registry的值为true,否则会服务不可用
   # 是否把自己作为服务注册到其他服务注册中心
   eureka.client.register-with-eureka=true
   # 是否从其他的服务中心同步服务列表
   eureka.client.fetch-registry=true
   # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
   eureka.client.serviceUrl.defaultZone=http://service-registry2:6002/eureka/,http://service-registry3:6003/eureka/
   
   eureka.instance.appname=service-registry
   #主机名称
   eureka.instance.hostname=service-registry1
   #服务注册中心地址
   #registry-2.application.properties
   # 项目端口号
   server.port=6002
   # 项目名称
   spring.application.name=service-registry
   
   #服务注册中心相互注册一定要显示的设置register-with-eureka 和fetch-registry的值为true,否则会服务不可用
   # 是否把自己作为服务注册到其他服务注册中心
   eureka.client.register-with-eureka=true
   # 是否从其他的服务中心同步服务列表
   eureka.client.fetch-registry=true
   # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
   eureka.client.serviceUrl.defaultZone=http://service-registry1:6001/eureka/,http://service-registry3:6003/eureka/
   
   eureka.instance.appname=service-registry
   #主机名称
   eureka.instance.hostname=service-registry2
   #registry-3.application.properties
   # 项目端口号
   server.port=6003
   # 项目名称
   spring.application.name=service-registry
   
   #服务注册中心相互注册一定要显示的设置register-with-eureka 和fetch-registry的值为true,否则会服务不可用
   # 是否把自己作为服务注册到其他服务注册中心
   eureka.client.register-with-eureka=true
   # 是否从其他的服务中心同步服务列表
   eureka.client.fetch-registry=true
   # 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
   eureka.client.serviceUrl.defaultZone=http://service-registry1:6001/eureka/,http://service-registry2:6003/eureka/
   
   eureka.instance.appname=service-registry
   #主机名称
   eureka.instance.hostname=service-registry3

注意

1.服务注册中心集群相互注册一定要开启

  register-with-eureka: true
  fetch-registry: true

2.服务注册中心集群的spring.application.name一定要一样
3.eureka.client.serviceUrl.defaultZone:不能出现 localhost,一定要使用host指定主机名

到此,相信大家对“如何应用SpringCloud集成Eureka注册中心”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. 2、eureka注册中心集群
  2. 1、eureka注册中心单机

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

eureka spring cloud

上一篇:MySQL 5.7组复制的要求和限制有哪些

下一篇:在Redis中如何对集群进行扩容

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》