|
| 1 | +**1. Spring Cloud简介** |
| 2 | + |
| 3 | +Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单的开发方式。 |
| 4 | + |
| 5 | +Spring Cloud 全家桶: |
| 6 | + - | Spring Cloud |
| 7 | +|---|---| |
| 8 | +服务注册中心 | Spring Cloud Netflix Eureka |
| 9 | +服务调用方式 | REST API |
| 10 | +服务网关 | Spring Cloud Netflix Zuul |
| 11 | +断路器 | Spring Cloud Netflix Hystrix |
| 12 | +分布式配置 | Spring Cloud Config |
| 13 | +服务跟踪 | Spring Cloud Sleuth |
| 14 | +消息总线 | Spring Cloud Bus |
| 15 | +数据流 | Spring Cloud Stream |
| 16 | +批量任务 | Spring Cloud Task |
| 17 | + |
| 18 | +**2. 微服务架构** |
| 19 | + |
| 20 | +微服务架构就是将一个完整的应用从数据存储开始垂直拆分成多个不同的服务,每个服务都能独立部署、独立维护、独立扩展,服务与服务间通过诸如RESTful API的方式互相调用。各个微服务之间是松耦合的,每个微服务仅关注于完成一件任务,每个任务代表着一个小的业务能力。 |
| 21 | + |
| 22 | +**3. 服务治理** |
| 23 | + |
| 24 | +服务治理是实现微服务的关键。那么有没有好的服务治理方案呢?我想大家都听过或者使用过dubbo,dubbo就是一个带有服务治理功能的RPC框架。dubbo提供了一套较为完整的服务治理方案,所以企业如果要实现服务化的话,dubbo是很好的一个选择。 |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +服务发现与注册作为服务治理最最重要的问题,dubbo中引入了一个注册中心的概念,而zookeeper作为dubbo推荐的注册中心,承担了及其重要的作用。 |
| 29 | + |
| 30 | +那么我们如何使用Spring Cloud来实现服务治理呢?答案就是Spring Cloud Eureka,也就是本篇博客要介绍的重点。Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块。而Spring Cloud Netflix项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等。 |
| 31 | + |
| 32 | +以下是笔者翻阅资料后对zookeeper和eureka进行的比较总结: |
| 33 | + |
| 34 | +* Eureka 保证AP |
| 35 | +Eureka中各个节点都是平等的,几个节点挂掉不会影响正常节点的工作,剩余节点依然可以提供注册和查询服务。而Eureka的客户端在向某个Eureka注册时如果发现连接失败,则会自动切换至其它节点,从而保证注册服务可用(保证可用性),只不过查到的信息可能不是最新的(不保证强一致性)。除此之外,Eureka还有一种自我保护机制,如果在15分钟内超过85%的节点都没有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障,此时会出现以下几种情况: |
| 36 | + 1. Eureka不再从注册列表中移除因为长时间没收到心跳而应该过期的服务。 |
| 37 | + 2. Eureka仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上(即保证当前节点依然可用)。 |
| 38 | + 3. 当网络稳定时,当前Eureka新的注册信息会被同步到其它节点中。 |
| 39 | + |
| 40 | + **Eureka还有客户端缓存功能** |
| 41 | + |
| 42 | +* <font size=3>Zookeeper 保证CP |
| 43 | +当master节点因为网络故障与其他节点失去联系时,剩余节点会重新进行leader选举。在选举期间整个zk集群都是不可用的,这就导致在选举期间注册服务瘫痪。</font> |
| 44 | + |
| 45 | + |
| 46 | +**4. 动手搭服务发现和注册** |
| 47 | + |
| 48 | +**第一步:创建服务注册中心** |
| 49 | + |
| 50 | +创建Spring boot项目,命名为eureka-server,并在pom.xml中引入需要的依赖内容: |
| 51 | +```xml |
| 52 | +<parent> |
| 53 | + <groupId>org.springframework.boot</groupId> |
| 54 | + <artifactId>spring-boot-starter-parent</artifactId> |
| 55 | + <version>1.5.9.RELEASE</version> |
| 56 | + <relativePath/> <!-- lookup parent from repository --> |
| 57 | +</parent> |
| 58 | + |
| 59 | +<!-- 引入Eureka服务包--> |
| 60 | +<dependencies> |
| 61 | + <dependency> |
| 62 | + <groupId>org.springframework.cloud</groupId> |
| 63 | + <artifactId>spring-cloud-starter-eureka-server</artifactId> |
| 64 | + </dependency> |
| 65 | +</dependencies> |
| 66 | + |
| 67 | +<dependencyManagement> |
| 68 | + <dependencies> |
| 69 | + <dependency> |
| 70 | + <groupId>org.springframework.cloud</groupId> |
| 71 | + <artifactId>spring-cloud-dependencies</artifactId> |
| 72 | + <version>Dalston.RELEASE</version> |
| 73 | + <type>pom</type> |
| 74 | + <scope>import</scope> |
| 75 | + </dependency> |
| 76 | + </dependencies> |
| 77 | +</dependencyManagement> |
| 78 | +``` |
| 79 | + |
| 80 | +通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话。在Spring Boot启动类加上这个注解即可,如下: |
| 81 | +```java |
| 82 | +@SpringBootApplication |
| 83 | +@EnableEurekaServer |
| 84 | +public class EurekaServerApplication { |
| 85 | + public static void main(String[] args) { |
| 86 | + SpringApplication.run(EurekaServerApplication.class, args); |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties配置文件中增加如下信息: |
| 92 | +```java |
| 93 | +spring.application.name=eureka-server |
| 94 | +server.port=8761 |
| 95 | +eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka |
| 96 | +#表示是否将自己注册到Eureka Server上,默认为true |
| 97 | +eureka.client.registerWithEureka=false |
| 98 | +#表示是否从Eureka Server上获取注册信息,默认为true |
| 99 | +eureka.client.fetchRegistry=false |
| 100 | +``` |
| 101 | + |
| 102 | +启动工程,访问 http://localhost:8761/ ,可以看到下面的页面,其中还没有发现任何服务。 |
| 103 | + |
| 104 | +**第二步:创建服务提供方** |
| 105 | + |
| 106 | +下面我们创建提供服务的客户端,并向服务注册中心注册自己。本文我们主要介绍服务的注册与发现,所以我们不妨在服务提供方中尝试着提供一个接口来获取当前所有的服务信息。 |
| 107 | + |
| 108 | +首先,创建一个基本的Spring Boot应用。命名为eureka-order,在pom.xml中,加入如下配置: |
| 109 | +```xml |
| 110 | +<parent> |
| 111 | + <groupId>org.springframework.boot</groupId> |
| 112 | + <artifactId>spring-boot-starter-parent</artifactId> |
| 113 | + <version>1.5.9.RELEASE</version> |
| 114 | + <relativePath/> <!-- lookup parent from repository --> |
| 115 | +</parent> |
| 116 | + |
| 117 | +<dependencies> |
| 118 | + <dependency> |
| 119 | + <groupId>org.springframework.cloud</groupId> |
| 120 | + <artifactId>spring-cloud-starter-eureka</artifactId> |
| 121 | + </dependency> |
| 122 | + <dependency> |
| 123 | + <groupId>org.springframework.boot</groupId> |
| 124 | + <artifactId>spring-boot-starter-web</artifactId> |
| 125 | + </dependency> |
| 126 | +</dependencies> |
| 127 | + |
| 128 | +<dependencyManagement> |
| 129 | + <dependencies> |
| 130 | + <dependency> |
| 131 | + <groupId>org.springframework.cloud</groupId> |
| 132 | + <artifactId>spring-cloud-dependencies</artifactId> |
| 133 | + <version>Dalston.RELEASE</version> |
| 134 | + <type>pom</type> |
| 135 | + <scope>import</scope> |
| 136 | + </dependency> |
| 137 | + </dependencies> |
| 138 | +</dependencyManagement> |
| 139 | +``` |
| 140 | + |
| 141 | +在应用启动类中通过加上@EnableEurekaClient(该注解上有@EnableDiscoveryClient)注解,该注解能激活Eureka中的DiscoveryClient实现,这样才能实现Controller中对服务信息的输出。 |
| 142 | +```java |
| 143 | +@SpringBootApplication |
| 144 | +@EnableEurekaClient |
| 145 | +public class EurekaOrderApplication { |
| 146 | + public static void main(String[] args) { |
| 147 | + SpringApplication.run(EurekaOrderApplication.class, args); |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +我们在完成以上工作后,再继续对eureka-order的application.properties做一些配置工作,具体如下: |
| 153 | +```java |
| 154 | +spring.application.name=eureka-order |
| 155 | +server.port=8100 |
| 156 | +eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka |
| 157 | +``` |
| 158 | + |
| 159 | +通过spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。为了在本机上测试区分服务提供方和服务注册中心,使用server.port属性设置不同的端口。 |
| 160 | + |
| 161 | +同时启动两个服务,再访问 http://localhost:8761/ 出现下图内容则表示服务注册成功: |
| 162 | + |
| 163 | + |
| 164 | +这样就基本完成了基础的Spring Cloud搭建,读者可以根据需要查看代码,github地址:[https://github.com/coderqianlq/spring-cloud](https://github.com/coderqianlq/spring-cloud)。 |
| 165 | + |
| 166 | + |
0 commit comments