学习Spring Cloud之Eureka------笔记一

概述:

        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  推荐使用本机IPeureka.instance.preferIpAddress=true默认为false) 而非

eureka.instance.hostname


在微服务架构中,由于每一个服务的粒度相对传统SOA来说要小的多,所以服务的数量会成倍增加。这时如果有效管理服务的注册信息就尤为重要。我们对服务注册中心的期望主要有以下几条:

  • 简单易用:最好对开发者透明
  • 高可用:几台注册中心坏掉不会导致整个服务瘫痪,注册服务整体持续可用
  • 避免跨越机房调用:最好调用优先同一个机房的服务以减少网络延迟
  • 跨语言:允许开发者使用多种编程语言构建微服务
  1. 创建项目

                a. 使用springboot插件创建,引入依赖

                        spring-cloud-starter-eureka-server eureka注册中心组件

                        spring-boot-starter-actuator  监控组件

                         spring-boot-starter-security     安全组件    

                        lombok  消除模板代码

                             getter、setter、构造器、toString()、equals()
                               便捷的生成比较复杂的代码,例如一个POJO要转化成构建器模式的形式,只需要一个注解。
           
 b.配置
                    

     

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);
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值