微服务架构之spring cloud eureka
Spring Cloud Eureka是spring cloud的核心组件,负责服务治理功能,起到中心枢纽作用,其它组件都依赖eureka来获取服务,然后再根据项目需求实现自己的业务,eureka在整个微服务架构中的位置绝对是核心地位。
(一) 版本说明
a) Spring boot 2.0.6.RELEASE
b) Spring cloud Finchley.SR2
c) Java version 1.8
(二) 项目设置
- 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>
- 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
- 主要参数说明
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 每次版本迭代都有配置参数的变更,最好是参考相对应的版本参数说明
(三) 项目运行
- 环境设置
修改/etc/hosts 文件,添加如下设置
127.0.0.1 server1
127.0.0.1 server2
127.0.0.1 server3
- 项目运行
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运行,运行后,可以查看运行效果

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

a) 可以看到3个服务都在运行,其实你可以访问3个端口的任何一个,都可以看到该效果,同时该界面也显示了一些环境信息,比如有效内存、已经运行的时长 等等。
b) 关于docker镜像生成、docker 私有仓库请翻阅我以前的文章。
这样spring cloud eureka服务自理就介绍完了,如果在开发中遇到问题,也可以留言共同探讨共同进步。
微服务架构之spring cloud eureka的更多相关文章
- 微服务架构集大成者—Spring Cloud (转载)
软件是有生命的,你做出来的架构决定了这个软件它这一生是坎坷还是幸福. 本文不是讲解如何使用Spring Cloud的教程,而是探讨Spring Cloud是什么,以及它诞生的背景和意义. 1 背景 2 ...
- 微服务架构-选择Spring Cloud,放弃Dubbo
Spring Cloud 在国内中小型公司能用起来吗?从 2016 年初一直到现在,我们在这条路上已经走了一年多. 在使用 Spring Cloud 之前,我们对微服务实践是没有太多的体会和经验的.从 ...
- 微服务架构之spring cloud 介绍
在当前的软件开发行业中,尤其是互联网,微服务是非常炽热的一个词语,市面上已经有一些成型的微服务框架来帮助开发者简化开发工作量,但spring cloud 绝对占有一席之地,不管你是否为java开发,大 ...
- 微服务架构之spring cloud ribbon
现在负载均衡是通用的解决分压的技术方案,实现方式一般分为服务端或者客户端,服务端大部分是使用中间件实现,spring cloud ribbon 是一个客户端负载均衡组件.跟spring cloud e ...
- 微服务架构之spring cloud zipkin
Spring Cloud Zipkin是微服务的链路跟踪组件,帮助详细了解一次request&response的总计时,及每个微服务的消耗时间.微服务名称.异常信息等等过程信息. (一) 版本 ...
- 微服务架构之spring cloud turbine
在前面介绍了spring cloud hystrix及其hystrix dashboard,但都是对单个项目的监控,对于一个为项目而言,必定有很多微服务,一个一个去看非常的不方便,如果有一个能集中熔断 ...
- 微服务架构之spring cloud hystrix&hystrix dashboard
在前面介绍spring cloud feign中我们已经使用过hystrix,只是没有介绍,spring cloud hystrix在spring cloud中起到保护微服务的作用,不会让发生的异常无 ...
- 微服务架构之spring cloud gateway
Spring Cloud Gateway是spring cloud中起着非常重要的作用,是终端调用服务的入口,同时也是项目中每个服务对外暴露的统一口径,我们可以在网关中实现路径映射.权限验证.负载均衡 ...
- 微服务架构之spring cloud feign
在spring cloud ribbon中我们用RestTemplate实现了服务调用,可以看到我们还是需要配置服务名称,调用的方法 等等,其实spring cloud提供了更优雅的服务调用方式,就是 ...
随机推荐
- shell-005:备份。
# 这个案例感觉有点绕,如果是本地机器备份完全没必要.下面带颜色的写法值得我们借鉴,所以还是做下笔记吧 #!/bin/bash # 找出//目录下所有txt结尾的文件,且形成一个列表清单的文件 fi ...
- 【HADR】How to reestablish HADR from scratch after a failure on Standby
转载 http://www-01.ibm.com/support/docview.wss?uid=swg21514783 Cause Have a HADR pair with Primary on ...
- 事件委托(js实现)
1.事件委托的作用 事件委托的意义:,事件就是onclick,onmouseover,onmouseout等就是事件,委托呢,就是让别人来做,这个事件本来是加在某些元素上的,然而你却加到别人身上来做, ...
- Cygwin、Msys、MinGW、Msys2的区别与联系(转)
网上有很多文章讲它们之间的区别与联系,初学者看的一头雾水,在知乎上看到这篇文章讲的很清楚,容易理解. 在讲区别联系之前,我们先看一下这几个东东的前世今生. Cygwin,原 Cygnus 出品(已 ...
- nginx源码编译以及源码编译过程中遇到的问题
本文主要讲nginx安装以及安装过程中遇到的问题. 谈到nginx 必须聊聊它的起源和发展. nginx是由俄罗斯工程师Igor Sysoev 用C语言开发的一个免费开源的Web服务器软件,于2004 ...
- Session 与 Cookie
Session 与 Cookie 的作用都是为了保持访问用户与后端服务器的交互状态.它们本身只是HTTP中的一个配置项,在servlet规范中也只对应一个类而已. 理解 Cookie 通俗地说就是当一 ...
- C#(winform)设置窗体的启动位置
只需要设置窗体的StartPosition属性: registerForm.StartPosition = FormStartPosition.CenterScreen; FormStartPosit ...
- 转载:怎么用eclipse开发C++程序(以后备用,待实现),使用CDT
一:准备工作:需下载以下三个软件包 a).Eclipse 3.1 官方站点: http://www.eclipse.org 工具下载地址:http://www.eclipse.org/download ...
- *2-3-7-加入field_automation机制
在2.3.3节中引入my_mointor时,在my_transaction中加入了my_print函数: 在2.3.5节中引入reference model时,加入了my_copy函数: 在2.3.6 ...
- AutoDetectChangesEnabled及AddRange解决EF插入的性能问题
转自:http://www.cnblogs.com/nianming/archive/2013/06/07/3123103.html#2699851 记录下. 园友莱布尼茨写了一篇<Entity ...