SpringCloud为开发者提供了在分布式系统中的一些常用的组件(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁定,决策竞选,分布式会话集群状态)。使用Spring Cloud开发人员可以快速地完成实现这些模式的服务和应用程序。它们在任何分布式环境中都能很好地工作
学过 Dubbo 的一定不会陌生 ZooKeeper ,那么学 SpringCloud 的就不能陌生 Eureka。它是 Netflix 开源的一款基于REST(Representational State Transfer)风格的服务发现框架,目前已被 SpringCloud 集成在其子项目 spring-cloud-netflix 中,用于 SpringCloud 的服务注册发现功能。
Servlet 应用,jar/war 包部署Jersey 框架实现自身的 RESTfulHTTP 接口JDKTimer 实现 心跳检测、定时清理过期服务、节点同步等功能(动态添加删除节点、弹性)Google 的 guava 包管理内存缓存Finchley 版中 SpringCloud 所采用的是 1.9.x 版本(持续维护),所以完全不用担心,同时 SpringCloud 还提供了 Eureka、 Consul、 ZooKeeper、 Etcd 等多款服务注册发现功能的框架,完全不用担心会在一棵树上吊死Eureka 的 Server 与 Client
EurekaServer: 提供服务注册与发现, Client 节点启动后,会向 EurekaServer 发送注册事件, EurekaServer 注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。基于 Jersey 框架实现自身的 RESTfulHTTP 接口,只要满足规范任何语言程序都能向其进行注册EurekaClient: 任何形式的客户端,只要满足 EurekaServer 订制的注册标准。搭建一个 EurekaServer 是最为容易的,只需要一个依赖即可完成。在 Finchley 版本中 artifactId 与早期的有所差异,所以大家在使用 SpringCloud 一定要注意自己所使用的版本,如果询问它人相关问题,也应该告知对方你所采用的版本
<!-- 此处我只给出了 Eureka Server 需要用到的包依赖,
为了篇幅考虑 spring-boot-starter-parent 及 spring-cloud-dependencies
并未将其粘贴出来,有需要的可以点击文末的 GITHUB 连接进去 copy -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>在 src/main/resources 目录下创建一个 bootstrap.yml 的文件,文中我已经将部分需要注意的配置项罗列出来,并附写好相关说明,更多配置可以参考 EurekaInstanceConfigBean 、 EurekaClientConfigBean 、 EurekaServerConfigBean 。绝大多数情况下对于 EurekaServer 只需了解每个配置的作用及其意义即可,因为错误的配置反而会破坏原有的保护特性
server:
port: 7071
eureka:
instance:
prefer-ip-address: true
# 生产环境中官方是不建议修改默认配置,因为那样会破坏 eureka server 的保护模式
server:
# 关闭保护模式(生产环境不建议修改)
enable-self-preservation: false
# 清理间隔(默认是60 * 1000 毫秒)(生产环境不建议修改)
eviction-interval-timer-in-ms: 60000
# Eureka 拉取服务列表时间(默认:30秒)(生产环境不建议修改)
remote-region-registry-fetch-interval: 5
client:
# eureka server 没必要自己把自己注册上去,所以可以设置成 false
register-with-eureka: false
# 是否从Eureka Server上获取注册信息,默认为true,此处建议修改成 false (单机设置的意义不大,如果设置成 true 启动会去抓取一次注册表,获取不到更新缓存就会出错(该错误不影响 eureka 正常使用))
fetch-registry: false
service-url:
# 默认注册地址 this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
# 划重点:此处的 defaultZone 千万别写成 default-zone
defaultZone: http://localhost:${server.port}/eureka/
# 从 Eureka 服务器端获取注册信息的间隔时间(默认:30秒)
registry-fetch-interval-seconds: 5与往常 SpringBoot 启动类类似,不同的是多了一个 @EnableEurekaServer 注解,用来标识当前工程是 EurekaServer,到此我们 Server 就算是搭建完成了。
package com.battcn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author Levin
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServer {
public static void main(String[] args) {
SpringApplication.run(EurekaServer.class, args);
}
}访问 http://localhost:7071/ 能看到如下内容就代表项目搭建成功,图中红色的表示我们关闭了 Eureka 保护模式,在网络波动的情况下 EurekaServer 可能无法及时的保护客户端实例
搭建一个 EurekaClient 也是相当容易的,只需要两个小小的依赖即可完成。
<!-- 此处我只给出了 Eureka Server 需要用到的包依赖,
为了篇幅考虑 spring-boot-starter-parent 及 spring-cloud-dependencies
并未将其粘贴出来,有需要的可以点击文末的 GITHUB 连接进去 copy -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>下面罗列一部分需要注意的配置项,需要注意的都已经写好相关说明,更多配置可以参考 EurekaInstanceConfigBean 、 EurekaClientConfigBean 、 InetUtilsProperties 以下配置适用于大部分情况
server:
port: 7072
# 如果不写 Eureka Server 中的界面中 Application 就会是 Unknown 尽量写
spring:
application:
name: eureka-client
cloud:
inetutils:
# 忽略指定网卡(支持正则),假设你的电脑有 VM 那么该选项是非常有用的一个选项
ignored-interfaces: #忽略 docker0 网卡以及 veth 开头的网卡
- docker0
- veth.*
preferred-networks: # 使用指定网络地址,选择 eth0 网卡,当然也可以直接写 IP (192.168)
- eth0
eureka:
instance:
# 此处建议写,不写默认是机器名
prefer-ip-address: true
# 优先级小于 application.properties ,如果你想知道当前注册上去的版本必须使用 application.properties 中的配置写法
# 因为 bootstrap.yml 最早初始化,那时候还无法读取到 pom.xml 中的属性
instance-id: ${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
# ip-address 强行指定此实例 IP ,不是很推荐,绝大多数情况 prefer-ip-address 与 preferred-networks 配合都够用了
client:
service-url:
# 划重点:此处的 defaultZone 千万别写成 default-zone
defaultZone: http://localhost:7071/eureka/扩展知识
如果你觉得 EurekaServerUI 中默认显示的 instance-id 让你看着不爽,你可以试着在根目录下创建一个 application.properties 填写如下内容,因为 SpringCloud 和 SpringBoot 启动装载的配置有优先级之分, bootstrap 文件优先于 application 文件之前装载,所以一些配置只在 application 中生效
# 默认值:XB-201704242009:eureka-client:7072
# 修改前(bootstrap 配置的 instance-id):192.168.13.22:7072
# 修改后(application 配置的 instance-id):192.168.13.22:7072:0.0.1-SNAPSHOT
# 是不是后者更为直观,如果要用 @project.version@ 那么必须在 pom.xml 插件做一下小小的配置,具体请看 GITHUB ,当然如果你不需要显示 version 那么该文件你可以忽略
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}:@project.version@与往常 SpringBoot 启动类类似,不同的是多了一个 @EnableDiscoveryClient 注解,用来标识当前工程是 DiscoveryClient。因为 SpringCloud 提供了非常多的服务注册发现的组件,如 euerka、 zookeeper、 consul、 etcd、所以为统一标识客户端身份,所以它用的是 @EnableDiscoveryClient 注解
package com.battcn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author Levin
*/
@EnableDiscoveryClient
@SpringBootApplication
public class MyEurekaClient {
public static void main(String[] args) {
SpringApplication.run(MyEurekaClient.class, args);
}
}目前很多大佬都写过关于 SpringCloud 的教程了,如有雷同,请多多包涵,本教程基于最新的 spring-cloud:Finchley.SR1 编写...
个人博客:http://blog.battcn.com/
全文代码:https://github.com/battcn/spring-cloud2-learning/tree/master/chapter1