微服务架构之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提供了更优雅的服务调用方式,就是 ...
随机推荐
- STL项目-学校演讲比赛
// 学校演讲比赛.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. // #include "pch.h" #include < ...
- pixi.js v5版本出了。
历尽千辛万苦,pixi.js v5版本出了. 请关注群 881784250, 会尽块出个微信小游戏版. pixi.js的每个函数,每个类都有测试用例的.出的版本都是很稳定的.
- stark - 4 ⇲ 视图函数
✘ list_view 处理表格(默认是显示表结构的所有字段) 1 list_display = self.get_list_display() # 4.1处理表头 header_list = [] ...
- Mac下常用按键符号⌘(command)、⌥(option)、⇧(shift)、⇪(caps lock)、⌃(control)、↩(return)、⌅(enter)
常用符号如下: ⌘(command) ⌥(option) ⇧(shift) ⇪(caps lock) ⌃(control) ↩(return) ⌅(enter) 对应键盘的位置如下: 如果每次都不记得 ...
- wusir 线程间操作无效: 从不是创建控件“”的线程访问它 解决办法
利用FileSystemWatcher设计一个文件监控系统时,如果一个文件被修改或者新建,则文件修改事件会被多次触发而产生多条信息.为了将一个文件被修改一次而产生的多条信息归结为一条,在设计中新开了一 ...
- Linux启动与关闭WIndows服务记录
启动: mono-service -l:/var/run/Myservice-lock.pid MyService.exe (这个-l参数一定要加上) 控制服务: 暂停: kill -USR1 `ca ...
- linux常用多线程下载工具
1.axel 下载安装yum install axel 这个软件下载速度实时显示
- Innosetup中在安装界面左下角添加超链接
在程序的安装界面左下角加上超链接,如下图: 1. 新建一个标签,这里使用的控件是TNewStaticText ,完整的方法是 //该方法传入两个参数: //1. ParentForm:将这个URLLa ...
- Intellij Idea快捷鍵
一.视图查看 Ctrl+F12 查看file,method结构图.类继承机构图 (不知道方法结构,Ctrl+F12一下,方法,参数,返回值,一清二楚的展现出来) Ctrl+shift+Alt+U ...
- php方法重载
php方法重载 <?php/* * php面向对象的重写与重载重写:就是当子类继承父类的一些方法后,子类又在其内部定义了相同的方法,则这个新定义的方法会覆盖继承而来的父类的方法,子类只能调用 ...