Spring Cloud Eureka是spring cloud的核心组件,负责服务治理功能,起到中心枢纽作用,其它组件都依赖eureka来获取服务,然后再根据项目需求实现自己的业务,eureka在整个微服务架构中的位置绝对是核心地位。

(一)   版本说明

  a)         Spring boot   2.0.6.RELEASE

  b)        Spring cloud  Finchley.SR2

  c)         Java version   1.8

(二)   项目设置

  1. Pom文件

    

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

    <version>2.0.2.RELEASE</version>

</dependency>

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-actuator</artifactId>

</dependency>
  1. application.yml配置文件

    

spring:

  profiles:

    active: test-1001

  application:

    name: discover-services

eureka:

  datacenter: ctm

  instance:

  environment: dev

  server:

  client:

management:

  endpoints:

    web.exposure.include: "*"

  endpoint:

    health:

      show-details: ALWAYS

---

server:

  port: 1001

eureka:

  server:

    enable-self-preservation: false

    eviction-interval-timer-in-ms: 60000

    renewal-percent-threshold: 0.85

  client:

    service-url:

      defaultZone: http://${eureka.instance.hostname}:1002/eureka/,http://${eureka.instance.hostname}:1003/eureka/

    register-with-eureka: true

    fetch-registry: true

    healthcheck:

      enabled: true

  instance:

    hostname: ${eureka.instance.ip-address}

    prefer-ip-address: true

    ip-address: 192.168.1.129

    lease-renewal-interval-in-seconds: 10

    lease-expiration-duration-in-seconds: 30

    instance-id: ${eureka.instance.ip-address}:${server.port}

spring:

  profiles: test-1001

---

server:

  port: 1002

eureka:

  server:

    enable-self-preservation: false

    eviction-interval-timer-in-ms: 60000

    renewal-percent-threshold: 0.85

  instance:

    hostname: ${eureka.instance.ip-address}

    prefer-ip-address: true

    ip-address: 192.168.1.129

    lease-renewal-interval-in-seconds: 10

    lease-expiration-duration-in-seconds: 30

    instance-id: ${eureka.instance.ip-address}:${server.port}

  client:

    service-url:

      defaultZone: http://${eureka.instance.hostname}:1001/eureka/,http://${eureka.instance.hostname}:1003/eureka/

    register-with-eureka: true

    fetch-registry: true

    healthcheck:

      enabled: true

spring:

  profiles: test-1002

---

server:

  port: 1003

eureka:

  server:

    enable-self-preservation: false

    eviction-interval-timer-in-ms: 60000

    renewal-percent-threshold: 0.85

  instance:

    hostname: ${eureka.instance.ip-address}

    prefer-ip-address: true

    ip-address: 192.168.1.129

    lease-renewal-interval-in-seconds: 10

    lease-expiration-duration-in-seconds: 30

    instance-id: ${eureka.instance.ip-address}:${server.port}

  client:

    service-url:

      defaultZone: http://${eureka.instance.hostname}:1001/eureka/,http://${eureka.instance.hostname}:1002/eureka/

    register-with-eureka: true

    fetch-registry: true

    healthcheck:

      enabled: true

spring:

  profiles: test-1003
  1. 主要参数说明

    a)         spring.profiles.active     设置当前使用的配置项,一般多配置场景下使用,这里为了eureka高可用,设置了3个实例,在运行实例的时候,可以动态设置。

    b)        spring.application.name 项目名称

    c)         server.port     运行端口号

    d)        eureka.server.enable-self-preservation 是否启用自我保护功能,该功能默认是启用,但为了快速的响应服务的上下线,一般在开发环境把自我保护功能禁用

    e)         client.client.service-url.defaultZone    服务注册中心地址,这里是交叉设置3个服务自理实例

    f)          client.instance.lease-renewal-interval-in-seconds   发送心跳的频率

    g)        client.instance.lease-expiration-duration-in-seconds      失效间隔,这个主要是判断客户端还活着,一般设置为client.instance.lease-renewal-interval-in-seconds的3倍。

    h)        其它参数说明可以参考官方说明,需要说明的是spring cloud 每次版本迭代都有配置参数的变更,最好是参考相对应的版本参数说明

(三)   项目运行

  1. 环境设置

修改/etc/hosts 文件,添加如下设置

127.0.0.1 server1

127.0.0.1 server2

127.0.0.1 server3

  1. 项目运行

      

sudo docker run --name discoverservice- -d -p : -e ACTIVE=" --eureka.client.service-url.defaultZone=http://server2:1002/eureka/,http://server3:1003/eureka/  --eureka.instance.hostname=server1 --eureka.instance.ip-address=server1 --spring.profiles.active=dev-1001 -Xms128m -Xmx512m" discoverservice/discoverservice

sudo docker run --link discoverservice-:server1 --name discoverservice- -d -p : -e ACTIVE=" --eureka.client.service-url.defaultZone=http://server1:1001/eureka/,http://server3:1003/eureka/  --eureka.instance.hostname=server2 --eureka.instance.ip-address=server2 --spring.profiles.active=dev-1002 -Xms128m -Xmx512m" discoverservice/discoverservice

sudo docker run --link discoverservice-:server2 --link discoverservice-:server1  --name discoverservice- -d -p : -e ACTIVE=" --eureka.client.service-url.defaultZone=http://server1:1001/eureka/,http://server2:1002/eureka/  --eureka.instance.hostname=server3 --eureka.instance.ip-address=server3 --spring.profiles.active=dev-1003 -Xms128m -Xmx512m" discoverservice/discoverservice

我这里是docker运行,运行后,可以查看运行效果

   

  1. 在浏览器中输入你的主机的IP:端口号,就可以看到eureka服务自理的运行界面如下图所示

   

  a)         可以看到3个服务都在运行,其实你可以访问3个端口的任何一个,都可以看到该效果,同时该界面也显示了一些环境信息,比如有效内存、已经运行的时长 等等。

  b)        关于docker镜像生成、docker 私有仓库请翻阅我以前的文章。

  这样spring cloud eureka服务自理就介绍完了,如果在开发中遇到问题,也可以留言共同探讨共同进步。

微服务架构之spring cloud eureka的更多相关文章

  1. 微服务架构集大成者—Spring Cloud (转载)

    软件是有生命的,你做出来的架构决定了这个软件它这一生是坎坷还是幸福. 本文不是讲解如何使用Spring Cloud的教程,而是探讨Spring Cloud是什么,以及它诞生的背景和意义. 1 背景 2 ...

  2. 微服务架构-选择Spring Cloud,放弃Dubbo

    Spring Cloud 在国内中小型公司能用起来吗?从 2016 年初一直到现在,我们在这条路上已经走了一年多. 在使用 Spring Cloud 之前,我们对微服务实践是没有太多的体会和经验的.从 ...

  3. 微服务架构之spring cloud 介绍

    在当前的软件开发行业中,尤其是互联网,微服务是非常炽热的一个词语,市面上已经有一些成型的微服务框架来帮助开发者简化开发工作量,但spring cloud 绝对占有一席之地,不管你是否为java开发,大 ...

  4. 微服务架构之spring cloud ribbon

    现在负载均衡是通用的解决分压的技术方案,实现方式一般分为服务端或者客户端,服务端大部分是使用中间件实现,spring cloud ribbon 是一个客户端负载均衡组件.跟spring cloud e ...

  5. 微服务架构之spring cloud zipkin

    Spring Cloud Zipkin是微服务的链路跟踪组件,帮助详细了解一次request&response的总计时,及每个微服务的消耗时间.微服务名称.异常信息等等过程信息. (一) 版本 ...

  6. 微服务架构之spring cloud turbine

    在前面介绍了spring cloud hystrix及其hystrix dashboard,但都是对单个项目的监控,对于一个为项目而言,必定有很多微服务,一个一个去看非常的不方便,如果有一个能集中熔断 ...

  7. 微服务架构之spring cloud hystrix&hystrix dashboard

    在前面介绍spring cloud feign中我们已经使用过hystrix,只是没有介绍,spring cloud hystrix在spring cloud中起到保护微服务的作用,不会让发生的异常无 ...

  8. 微服务架构之spring cloud gateway

    Spring Cloud Gateway是spring cloud中起着非常重要的作用,是终端调用服务的入口,同时也是项目中每个服务对外暴露的统一口径,我们可以在网关中实现路径映射.权限验证.负载均衡 ...

  9. 微服务架构之spring cloud feign

    在spring cloud ribbon中我们用RestTemplate实现了服务调用,可以看到我们还是需要配置服务名称,调用的方法 等等,其实spring cloud提供了更优雅的服务调用方式,就是 ...

随机推荐

  1. 1091 N-自守数 (15 分)

    // 建一个判断函数,接受两个整形的变量,再通过循环按位判断相等与否,主体函数中调用被调函数,建立一个判断变量.#include <iostream> using namespace st ...

  2. 使用nginx+uwsgi+Django环境部署

    环境准备 Python点这里 nginx点这里 uwsgi点这里

  3. 代码版本控制:git使用

    1.https://github.com/ 注册账号 2. 点击 Start a project 3. 4. 5.      Clone or download 6.      安装git 7.    ...

  4. 微信 oauth 登录 ,回调两次,一个坑,记录一下。

    在做微信某个功能的时候,大致需求是:静默授权,得到openId ,然后拿着openId调用接口,判断是否关注.如果是关注的,则发放礼券.每个我网站的会员只会发放一次礼券.如果第二次则会提示已领取过礼券 ...

  5. Openerp 修改 tree view 的列宽

    在 tree 的后边添加自定义css 列:“my_class" 然后在对应的css文件中,添加样式: 保存,重新刷新页面即可.

  6. github里如何删除一个repository仓库

    高手请绕行,新手往下走. 作为一个刚接触github(https://github.com/)的新手,除了感叹开源的丰富和强大之外,自己肯定也想试用一下,因此申请帐号神马的.今天自己创建一个Repos ...

  7. 模块和处理程序之通过HttpModule和HttpHandler拦截入站HTTP请求执行指定托管代码模块

    1.简介 大多数情况下,作为一个asp.net web开发对整个web应用程序的控制是十分有限的,我们的控制往往只能做到对应用程序(高层面)的基本控制.但是,很多时候,我们需要能够低级层面进行交互,例 ...

  8. IO流(二)字符流

    1.字符流:在字节流的基础上添加了编码机制.很大程度上和字节流操作一样的,字符流只能操作文本类文件,准确的说应该是纯文本类文件.例如.txt,.java,.cpp,.html等 编码:每一个国家都有自 ...

  9. You have not concluded your merge. (MERGE_HEAD exists)。(转)

    自己简直就是一个git小白,碰到问题,一点点的解决吧,可能不太系统,但也只能勤能补拙了 Git本地有修改如何强制更新 本地有修改和提交,如何强制用远程的库更新更新.我尝试过用git pull -f,总 ...

  10. Hardware Emulation Platform (硬件仿真平台) 在IC验证中的运用情况如何?

    三个EDA公司都有自己的hardware emulation verification platform: Cadence Palladium, Synopsys ZeBu, Mentor Veloc ...