概述:
This project provides Netflix OSS integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms. With a few simple annotations you can quickly enable and configure the common patterns inside your application and build large distributed systems with battle-tested Netflix components. The patterns provided include Service Discovery (Eureka), Circuit Breaker (Hystrix), Intelligent Routing (Zuul) and Client Side Load Balancing (Ribbon).
-------转自Spring Cloud官方技术文档(Edgware.SR2)
通过自动配置,绑定spring环境以及其他spring编程模型惯例,并在Spring Boot中集成Netflix OSS。通过一些简单的注解,您能够很快的在您的应用中生效&配置普通块,并用Netflix 的battle-tested 组件创建大型的分布式系统。模块提供了 服务发现,断路由,智能路由,客户端负载均衡
注册中心:
High Availability, Zones and Regions 高可用(一个集群只有一个Region多个Zone)
Standalone Mode 单例模式 客户端已服务端缓存及心跳使得服务中心能灵活处理故障
Peer Awareness 多个服务中心相互注册,服务中心将变得更加灵活,可用
Prefer IP Address 推荐使用本机IP(eureka.instance.preferIpAddress=true默认为false) 而非
eureka.instance.hostname
在微服务架构中,由于每一个服务的粒度相对传统SOA来说要小的多,所以服务的数量会成倍增加。这时如果有效管理服务的注册信息就尤为重要。我们对服务注册中心的期望主要有以下几条:
- 简单易用:最好对开发者透明
- 高可用:几台注册中心坏掉不会导致整个服务瘫痪,注册服务整体持续可用
- 避免跨越机房调用:最好调用优先同一个机房的服务以减少网络延迟
- 跨语言:允许开发者使用多种编程语言构建微服务
- 创建项目
a. 使用springboot插件创建,引入依赖
spring-cloud-starter-eureka-server eureka注册中心组件
spring-boot-starter-actuator 监控组件
spring-boot-starter-security 安全组件
lombok 消除模板代码
spring:
application:
name: eureka_server
security:
basic:
enabled: true
user:
name: username
password: password
management:
security:
enabled: false
eureka:
server:
enable-self-preservation: true
instance:
#服务失效时间
lease-expiration-duration-in-seconds: 90
#服务续约任务的间隔时间
lease-renewal-interval-in-seconds: 30
# hostname: server1
client:
fetch-registry: true
register-with-eureka: true
# serviceUrl:
# defaultZone: http://tzhuang:1314@server2:1112/eureka/
---
spring:
profiles: server1
eureka:
# instance:
# hostname: server1
client:
serviceUrl:
defaultZone: http://username:password@localhost:1112/eureka/
server:
port: 1111
---
spring:
profiles: server2
eureka:
# instance:
# hostname: server2
client:
serviceUrl:
defaultZone: http://username:password@localhost:1111/eureka/
server:
port: 1112
#看到60秒打印一次日志的原因:
#定时任务,每个一段时间(60s)将当前清单中超时90s的服务剔除
#linux执行命令 java -jar book_eurekaserver-0.0.1-SNAPSHOT.jar --spring.profiles.active=server2
若要在linux上执行,并在关闭session后,服务不会被关闭,需执行下面代码
nohup java -jar book_eurekaprovider-0.0.1-SNAPSHOT.jar --spring.profiles.active=server2 >/dev/null &
c.代码:
@EnableEurekaServer
@SpringBootApplication
public class BookEurekaserverApplication {
public static void main(String[] args) {
SpringApplication.run(BookEurekaserverApplication.class, args);
}
}