Spring Cloud(一):服务注册与发现

Spring Cloud是什么

Spring Cloud是一系列框架的有序集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,均可以用Spring Boot的开发风格作到一键启动和部署。Spring并无重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,经过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。web

微服务是能够独立部署、水平扩展、独立访问(或者有独立的数据库)的服务单元,springcloud就是这些微服务的大管家,采用了微服务这种架构以后,项目的数量会很是多,springcloud作为大管家须要管理好这些微服务,天然须要不少小弟来帮忙。spring

和Spring Boot 是什么关系

Spring Boot 是 Spring 的一套快速配置脚手架,能够基于Spring Boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具;Spring Boot专一于快速、方便集成的单个个体,Spring Cloud是关注全局的服务治理框架;Spring Boot使用了默认大于配置的理念,不少集成方案已经帮你选择好了,能不配置就不配置,Spring Cloud很大的一部分是基于Spring Boot来实现,能够不基于Spring Boot吗?不能够。数据库

Spring Boot能够离开Spring Cloud独立使用开发项目,可是Spring Cloud离不开Spring Boot,属于依赖的关系。springboot

    spring -> spring booot > Spring Cloud 这样的关系。架构

Spring Cloud的优点

微服务的框架那么多好比:dubbo、Kubernetes,为何就要使用Spring Cloud的呢?app

  • 产出于spring你们族,spring在企业级开发框架中无人能敌,来头很大,能够保证后续的更新、完善。好比dubbo如今就差很少死了
  • 有Spring Boot 这个独立干将能够省不少事,大大小小的活Spring Boot都搞的挺不错。
  • 做为一个微服务治理的你们伙,考虑的很全面,几乎服务治理的方方面面都考虑到了,方便开发开箱即用。
  • Spring Cloud 活跃度很高,教程很丰富,遇到问题很容易找到解决方案
  • 轻轻松松几行代码就完成了熔断、均衡负责、服务中心的各类平台功能

Spring Cloud对于中小型互联网公司来讲是一种福音,由于这类公司每每没有实力或者没有足够的资金投入去开发本身的分布式系统基础设施,使用Spring Cloud一站式解决方案能在从容应对业务发展的同时大大减小开发成本。同时,随着近几年微服务架构和Docker容器概念的火爆,也会让Spring Cloud在将来愈来愈“云”化的软件开发风格中立有一席之地,尤为是在目前五花八门的分布式解决方案中提供了标准化的、全站式的技术方案,意义可能会堪比当前Servlet规范的诞生,有效推动服务端软件系统技术水平的进步。负载均衡

Spring Cloud Eureka

咱们用Spring Cloud Eureka来实现服务治理。框架

建立“服务注册中心”

建立一个基础的Spring Boot工程,命名为springboot-register,并在pom.xml中引入须要的依赖内容:分布式

 1 <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.boot</groupId>
 4             <artifactId>spring-boot-starter-web</artifactId>
 5         </dependency>
 6 
 7         <dependency>
 8             <groupId>org.springframework.cloud</groupId>
 9             <artifactId>spring-cloud-starter-eureka-server</artifactId>
10         </dependency>
11         <dependency>
12             <groupId>org.springframework.boot</groupId>
13             <artifactId>spring-boot-starter-test</artifactId>
14             <scope>test</scope>
15         </dependency>
16     </dependencies>
17     <dependencyManagement>
18         <dependencies>
19             <dependency>
20                 <groupId>org.springframework.cloud</groupId>
21                 <artifactId>spring-cloud-dependencies</artifactId>
22                 <version>Brixton.SR5</version>
23                 <type>pom</type>
24                 <scope>import</scope>
25             </dependency>
26         </dependencies>
27     </dependencyManagement>

经过@EnableEurekaServer注解启动一个服务注册中心提供给其余应用进行对话:spring-boot

@EnableEurekaServer @SpringBootApplication public class ApplicationRegister { public static void main(String[] args) { SpringApplication.run(ApplicationRegister.class, args); } }

application.properties配置文件中增长以下信息:

server.port=1111 eureka.server.enable-self-preservation=false eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

访问:http://localhost:1111/ ,如图所示:

页面中尚未任何服务!

 

注册“服务提供方”

建立一个基本的Spring Boot应用。命名为spring boot-client,在pom.xml中,加入以下配置:

 1 <dependency>
 2             <groupId>org.springframework.boot</groupId>
 3             <artifactId>spring-boot-starter-web</artifactId>
 4         </dependency>
 5 
 6         <dependency>
 7             <groupId>org.springframework.cloud</groupId>
 8             <artifactId>spring-cloud-starter-eureka-server</artifactId>
 9         </dependency>
10         <dependency>
11             <groupId>org.springframework.boot</groupId>
12             <artifactId>spring-boot-starter-test</artifactId>
13             <scope>test</scope>
14         </dependency>
15     </dependencies>
16     <dependencyManagement>
17         <dependencies>
18             <dependency>
19                 <groupId>org.springframework.cloud</groupId>
20                 <artifactId>spring-cloud-dependencies</artifactId>
21                 <version>Brixton.SR5</version>
22                 <type>pom</type>
23                 <scope>import</scope>
24             </dependency>
25         </dependencies>
26     </dependencyManagement>

编写一个controller:

 1 @RestController  2 public class HelloController {  3     private final Logger logger = Logger.getLogger(getClass());  4 
 5  @Autowired  6     private DiscoveryClient client;  7 
 8     @RequestMapping(value = "/hello", method = RequestMethod.GET)  9     public String index() throws Exception{ 10        
11         return "Hello World"; 12  } 13 }

最后在应用主类中经过加上@EnableDiscoveryClient注解,实现Controller中对服务信息的输出:

1 @EnableDiscoveryClient 2 @SpringBootApplication 3 public class ApplicationClient { 4 
5     public static void main(String[] args) { 6         SpringApplication.run(ApplicationClient.class, args); 7  } 8 
9 }

在application.properties配置文件中增长以下信息:

server.port=2224 spring.application.name=hello-service eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

启动该工程后,再次访问:http://localhost:1111/ ,如图:

咱们也能够经过直接访问spring boot-client服务提供的/hello接口,访问:http://localhost:2224/hello,获得以下输出:

这样咱们的服务注册与发现就完成了。

 

剔除过时等不健康实例(生产环境不建议使用)

server端:

  

1.关闭注册中心自我保护机制 eureka.server.enable-self-preservation:false
2.注册中心清理间隔(单位毫秒,默认60*1000) eureka.server.eviction-interval-timer-in-ms:10000

client端:

  

1.开启健康检查(须要spring-boot-starter-actuator依赖) eureka.client.healthcheck.enabled:true
2.租期更新时间间隔(默认30秒) eureka.instance.lease-renewal-interval-in-seconds=10
3.租期到期时间(默认90秒) eureka.instance.lease-expiration-duration-in-seconds=15

以上参数配置下来,从服务中止,到注册中心清除不健康实例,时间大约在30秒左右。租期到期时间为30秒时,清除时间大约在59秒,若采用默认的30-60配置,清除时间大约在2分半(以上均在关闭保护机制状况下),生产环境建议采用默认配置,服务中止到注册中心清除实例之间有一些计算什么的。

相关文章
相关标签/搜索