Microservice Framework - Introduction to SpringCloud

Spring Cloud is a relatively new microservice framework, and the release version 1.0 was launched in 2016. Although Spring Cloud has the shortest time, compared to RPC frameworks such as Dubbo, Spring Cloud provides a complete set of distributed system solutions.

Spring Cloud provides developers with fast-build solutions in distributed systems (configuration management, service discovery, circuit breaker, routing, micro-proxy, control bus, one-time token, full housekeeping, leader election, distributed session, cluster status). Tools , developers using Spring Cloud can quickly start services or build applications, and can quickly connect with cloud platform resources.

Let's briefly describe the four basic key components of the Spring Cloud Chinese community:

Community address: http://springcloud.cn/

Spring Cloud Config Configuration Center Spring Cloud Config is the configuration center in our usual sense. Spring Cloud Config-Extracts the configuration of the application originally placed in the local file and put it on the central server. The essence is to migrate the configuration information from the local to the cloud . This can provide better management and release capabilities. Spring Cloud Config is divided into server and client. The server is responsible for publishing the configuration file stored in git (svn) as a REST interface , and the client can obtain the configuration from the server REST interface. However, the client cannot actively perceive the change of the configuration, so it can actively obtain the new configuration, which requires each client to trigger its own /refresh through the POST method. Spring Cloud Netflix Service Discovery

Microservice Framework - Introduction to SpringCloud







Microservice Framework - Introduction to SpringCloud
Spring Cloud Eureka provides service discovery and service registration functions in a distributed environment.

Spring Cloud Netflix, which is one of the sub-projects of Spring Cloud, mainly wraps a series of open source products of Netflix, and provides self-configured Netflix OSS integration for Spring Boot applications.

With some simple annotations, developers can quickly configure common modules in applications and build huge distributed systems. The main modules it provides include: service discovery (Eureka), circuit breaker (Hystrix), intelligent routing (Zuul), client load balancing (Ribbon), etc. A Spring cloud Hystrix Cricuit Breaker is a facility that automatically fuses (opens a switch) when a remote service is unavailable, and automatically restores (closes a switch) when the remote service is restored. A circuit breaker (Cricuit Breaker) is a facility that can automatically fuse (open a switch) when a remote service is unavailable, and automatically recover (close a switch) when the remote service is restored. Spring Cloud provides circuit breakers and resource isolation through Netflix's Hystrix component with self-healing function. Spring Cloud Zuul service gateway Spring Cloud Eureka provides service discovery and service registration functions in a distributed environment. Spring Cloud Netflix, which is one of the sub-projects of Spring Cloud, mainly wraps a series of open source products of Netflix, and provides self-configured Netflix OSS integration for Spring Boot applications.



Microservice Framework - Introduction to SpringCloud






Microservice Framework - Introduction to SpringCloud



通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路有(Zuul),客户端负载均衡(Ribbon)等。

当然Spring Cloud还有额外扩展的其它很多组件,包括了服务链路监控和跟踪(很关键的一个功能),消息总线,数据流处理,批量任务处理等。而对于整个Spring Cloud微服务框架简单来说,即是:

你只要划分到你的微服务组件和模块,并定义好需要暴露的API接口,那么剩下的整个开发和传统方式没有太大的区别,你开发完成的组件集成起来就是一个分布式可扩展的微服务环境。里面设计到的接口发布,服务注册,服务调用和路由,服务监控,健康检测和流控等都会由微服务框架来帮你完成。

正是有了成熟的微服务框架,我们才更应该将微服务架构设计重心从技术底层转移到组件划分和接口设计上。


SpringCloud和Dubbo的区别问题

对于两者的区别在如下文章有详细描述可以参考:

http://blog.didispace.com/microservice-framework/

可以看到SpringCLoud能够提供的基础能力要多于Dubbo,Dubbo可以看作是SpringCLoud简单实现。

Dubbo是RPC服务治理框架,和Spring Cloud一样具备服务注册、发现、路由、负载均衡等能力。但是没有配置中心,完整的好用全链路监控,需要采用开源的解决方案定制或者自研。Spring cloud的配置中心,全链路监控等组件。从目前来看,Spring Cloud国内中小型企业用的比较多,大型企业可能需要对其需要的组件进行定制化处理。

但是也需要看到Spring Cloud基于注解的服务发现,服务治理等功能具有代码侵入性,dubbo没有代码侵入性,业务开发人员不需要通过注解的方式去关注框架级别的处理。从中间件或者做基础架构的角度来看,其实服务治理等功能对普通的业务程序员应该是透明的,业务程序员不需要关注服务治理框架的使用,专注于业务代码即可。

基于SpringCLoud微服务框架的实践

对于基于SpringCLoud框架的具体实践,建议参考翟永超博客的系列文章,具体如下:

  • 服务注册和发现:http://blog.didispace.com/springcloud1/
  • 服务消费:http://blog.didispace.com/springcloud2/
  • 服务熔断机制:http://blog.didispace.com/springcloud3/
  • 服务配置中心:http://blog.didispace.com/springcloud4/
  • 服务网关:http://blog.didispace.com/springcloud5/
  • 高可用服务注册中心:http://blog.didispace.com/springcloud6/
  • 消息总线:http://blog.didispace.com/springcloud7/

服务注册和发现

注意这里仍然使用的是SpringBoot框架,并和SpringBoot框架进行了集成,在pom.xml配置文件中增加了对SpringCLoud相关包和组件的依赖。在原有的接口API定义的基础上,我们增加@EnableDiscoveryClient注解后,即可以让服务注册中心很轻松的发现服务提供方以及提供的服务。

服务消费


方式1 - Ribbon是一个基于HTTP和TCP客户端的负载均衡器。Ribbon可以在通过客户端中配置的ribbonServerList服务端列表去轮询访问以达到均衡负载的作用。当Ribbon与Eureka联合使用时,ribbonServerList会被DiscoveryEnabledNIWSServ erList重写,扩展成从Eureka注册中心中获取服务端列表。

方式2 - Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

断路器

首先在pom.xml文件中增加引入对hystrix依赖,同时在消费端Application主类上增加@EnableCircuitBreaker注解开启断路器功能。注意原有的服务消费方式也涉及到修改,增加了服务Callback的回调函数。

服务网关

A service gateway is an integral part of a microservices architecture. In the process of providing REST API to external systems through the service gateway, in addition to service routing and load balancing functions, it also has functions such as permission control. Zuul in Spring Cloud Netflix plays such a role, providing front door protection for the microservice architecture, and at the same time migrating the heavier non-business logic content of permission control to the service routing level, so that the main body of the service cluster can have more High reusability and testability.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325859234&siteId=291194637