springCloud之eureka的使用

    随着技术的更新迭代,服务的治理至关重要。传统的项目架构(例如:单体架构,垂直架构)已经不适用。现在大部分公司使用的还是SOA系统架构。

    简单介绍下SOA架构的特点:

        1.SOA可以将重复的公共的部分功能抽取出来当成一个组件,以服务的方式被各个系统调用。

        2.系统与各个服务之间采用webservice,RPC等通讯方式;

        3.使用ESB企业服务总线作为项目与服务之间的桥梁;

这之后,随着技术发展,微服务应运而生,将大型项目拆分为一个个小的服务。各个服务之间采用restful等轻量协议。

拆分的微服务多了以后就得需要服务注册管理中心。springCloud就是将各个服务框架集中到一起。其中在服务注册管理中心上使用的是Netflix的。

    Eureka:服务治理,注册服务与发现服务。它分为服务端和客户端。访问端口号:8761(默认)

    Eureka客户端:

    1.导入依赖:

                    

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

2.导入springCloud:

    

<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>

3.在application中添加eureka的配置:

    

#  设置服务名,因为是服务治理,在eureka中就不用试用ip来说明服务
spring:
  application:
    name: hello-service
#  设置注册中心地址,defaultZone指定了该客户端注册到哪个eureka server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:1111/eureka

4.在启动类中添加

@EnableEurekaClient

Eureka server服务端:

    1.添加服务端server依赖:

<!--eureka服务器端的依赖-->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka-server</artifactId>
   <version>1.4.4.RELEASE</version>
</dependency>

2.添加springCloud依赖

<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>

3.在application中添加server的配置:

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
        defaultZone: http://localhost:1111/eureka
  server:
    # 设为false,关闭自我保护
    enable-self-preservation: false
    # 清理间隔(单位毫秒,默认是60*1000    eviction-interval-timer-in-ms: 1000
4.在启动类上添加
@EnableEurekaServer

注释:

(1)@EnableEurekaClient和@EnableDiscoveryClient的区别:

            1.@EnableEurekaClient只适用于注册中心为Eureka

            2.@EnableDiscoveryClient适用于其他服务注册中心。

(2)springCloud项目是建立于springboot项目的基础上。


Eureka Server是高可用(HA)

    

registerWithEureka:true
fetchRegistry: true

将这两个配置改为true就可以将自己当做服务注册到其他注册中心上去了。












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

毛毛的毛毛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值