Springcloud Distributed Development Understanding

Source: Internet
Author: User

When it comes to Springcloud's recent exposure to the five "divine Beasts", the first contact with distributed development is through Dubbo RPC remote Procedure calls, and Dubbo gives me the feeling that although all the hosts are physically distributed, it is as if the user is a whole. And what is the meaning of distributed development for our Java developers? That is to say where to use distributed development, in fact, in any project (MVC design mode), business operations are the most central part, and all projects in which your business operations are completed directly determine whether your project is robust. Too young before ... Always feel that the business of a pursuit of technology to cultivate the scholar, the big deal is a crud, and later found that the idea is like a lion does not know where their prey, but also not clear their prey characteristics, it is urgent to eat its flesh, but nothing is absolutely, If it's a business build-up, a heap of low-tech business code crud is another story ...

Back to "The Five great Beasts of God":
    • Service Discovery--netflix Eureka
    • Customer-side Load Balancing--netflix Ribbon (feign)
    • Circuit Breaker--netflix Hystrix
    • Service Gateway--netflix Zuul
    • Distributed Configuration--spring Cloud Config
Let's look at the Spingcloud distributed in one of these five key points: Eureka:
一个RESTful风格的服务,用来定位运行在业务流程管理开发平台(AWS Enterprise BPM Platform)层的中间层服务。1.Eureka服务器用作服务的注册中心。(对比dubbo的注册中心:zk redis等);2.Eureka客户端其实就是一个java客户端,负责与服务器的交互,用作轮询负责均衡,故障切换支持等。Netflix在其生产环境中使用的是另外的客户端,它提供基于流量、资源利用率以及出错状态的加权负载均衡。

Depend on:

    <!--eureka server -->    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-eureka-server</artifactId>    </dependency>

Annotations:

    • @EnableEurekaServer: This annotation needs to be added to the startup application class of the Springboot project to start a service registration center;
    • @EnableEurekaClient: To show oneself to be a eurekaclient;

Configuration:

    • Registration Center Server:
server:  port: 8761# 默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml 通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.eureka:  instance:    hostname: localhost  client:    registerWithEureka: false    fetchRegistry: false    serviceUrl:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    • Client server:
eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/server:  port: 8762spring:  application:    name: service-hi
Ribbon:
主要提供客户侧的软件负载均衡算法。Ribbon客户端组件提供一系列完善的配置选项,比如连接超时、重试、重试算法等。 Ribbon是一个负载均衡客户端,可以很好的控制http和tcp的一些行为。Feign默认集成了Ribbon。Ribbon内置可插拔、可定制的负载均衡组件。    一些常用的负载均衡策略:     1. 简单轮询负载均衡     2. 加权响应时间负载均衡     3. 区域感知轮询负载均衡     4. 随机负载均衡     Ribbon中还包括一下功能:     1. 易于与服务发现组件(比如Netflix的Eureka)集成     2. 使用Archaius完成运行时配置     3. 使用JMX暴露运维指标,使用Servo发布     4. 多种可插拔的序列化选择     5. 异步和批处理操作(即将推出)     6. 自动SLA框架(即将推出)     7. 系统管理/指标控制台(即将推出)

Depend on:

    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-eureka</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-ribbon</artifactId>    </dependency>

Annotations:

    • @EnableDiscoveryClient: Register with the service centre;
    • @LoadBalanced: Combined with the @bean in the restremplate, the load balancing function is started;

Configuration:
APPLICATION.YML:

#指定服务的注册中心地址为http://localhost:8761/eureka/eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/#程序端口为8764server:  port: 8764#程序名称为 service-ribbonspring:  application:    name: service-ribbon
Feign:
Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。简而言之:Feign 采用的是基于接口的注解Feign 整合了ribbon

Depend on:

    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-eureka</artifactId>    </dependency>    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-feign</artifactId>    </dependency>

Annotations:

    • @EnableFeignClients: Open feign function;
    • @FeignClient: Add this annotation on the feign interface and specify which service to invoke by @feignclient ("service name"). (A controller-like @requestmapping (value = "Access Path"))

Configuration:
Application.yml

#服务注册地址为http://localhost:8761/eureka/eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/#端口号为8765server:  port: 8765#指定程序名为service-feignspring:  application:    name: service-feign
Hystrix:
Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次)断路器将会被打开。断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。断路器的特点:1.断路器可以防止一个应用程序多次试图执行一个操作,即很可能失败,允许它继续而不等待故障恢复或者浪费 CPU 周期,而它确定该故障是持久的.2.断路器模式也使应用程序能够检测故障是否已经解决。如果问题似乎已经得到纠正??,应用程序可以尝试调用操作。3.断路器增加了稳定性和灵活性,以一个系统,提供稳定性,而系统从故障中恢复,并尽量减少此故障的对性能的影响。它可以帮助快速地拒绝对一个操作,即很可能失败,而不是等待操作超时(或者不返回)的请求,以保持系统的响应时间。如果断路器提高每次改变状态的时间的事件,该信息可以被用来监测由断路器保护系统的部件的健康状况,或以提醒管理员当断路器跳闸,以在打开状态。

Depend on:

<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-hystrix</artifactId></dependency>

Annotations:

    • @EnableHystrix: Open hystrix;
    • @HystrixCommand: The method of service layer is added, the function of fuse is created, and the method of Fallbackmethod fuse is specified and the corresponding name is written.
Circuit breakers in the Ps:feign:
Feign是自带断路器的,在D版本的SpringCloud中,它没有默认打开。需要在配置文件中配置打开它,在配置文件加入配置:feign.hystrix.enabled=true

Annotations:

    • @FeignClient: Add the fallback attribute to the annotations of the Feignclient interface to specify the class, which implements the Feignclient interface and injects into the IOC container (@Component) to implement the Fuse method.

Hystrix Dashboard (Circuit breaker: Hystrix instrument panel):

Depend on:

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId></dependency>

Annotations:

    • @EnableHystrixDashboard: Configure to the main program startup class, Open hystrixdashboard;
Zuul:
在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统。在SpringCloud微服务系统中,一种常见的负载均衡方式是:客户端的请求首先经过负载均衡(zuul、Ngnix),再到达服务网关(zuul集群),然后再到具体的服。,服务统一注册到高可用的服务注册中心集群,服务的所有的配置文件由配置服务管理,配置服务的配置文件放在git仓库,方便开发人员随时改配置。类似nginx,反向代理的功能,不过netflix自己增加了一些配合其他组件的特性。Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。

Depend on:

<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-zuul</artifactId></dependency>

Annotations:

    • @EnableZuulProxy: General application in the entry Applicaton class annotated @enablezuulproxy, open the function of Zuul;

Configuration:

#指定服务注册中心的地址为http://localhost:8761/eureka/eureka:  client:    serviceUrl:      defaultZone: http://localhost:8761/eureka/#服务的端口为8769server:  port: 8769#服务名为service-zuulspring:  application:    name: service-zuul#以/api-a/ 开头的请求都转发给service-ribbon服务#以/api-b/ 开头的请求都转发给service-feign服务zuul:  routes:    api-a:      path: /api-a/**      serviceId: service-ribbon    api-b:      path: /api-b/**      serviceId: service-feign

Zuul Service filtering: Zuul not only routing, but also filtering, doing some security verification. (Implementation: Inherit Zuulfilter)

filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:     pre:路由之前    routing:路由之时    post: 路由之后    error:发送错误调用    filterOrder:过滤的顺序    shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。    run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。    
Springcloudconfig:
1.SpringCloudConfig就是我们通常意义上说的分布式配置中心,将应用原本放在本地文件的配置抽取出来放在中心服务器上,从而能够提供更好的管理,发布能力。2.SpringCloudConfig分为服务端和客户端,服务端负责将git(或者svn)上存储的配置文件发布成REST接口,客户端可以从服务端RESTjie接口获取配置。但客户端并不能主动感知配置文件的变化,从而主动去获取新的配置,它需要每个客户端通过POST方法触发各自的/refresh请求,SpringCloudBus就通过一个轻量级消息代理连接分布式系统节点。3.可以用于广播状态更改,(如配置更改)或者管理指令。4.SpringCloudBus提供了通过POST方法访问的endpoint/bus/refresh,这个接口通常由git的钩子功能调用,用以通知各个SpringCloudConfig的客户端去服务端更新配置。

Depend on:

<!-- 服务端开始 --><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-config-server</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-eureka</artifactId></dependency><!-- 服务端结束 --><!-- 客户端开始 --><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-config</artifactId></dependency><!-- 客户端结束 -->

Annotations:

    • @EnableConfigServer: Applied to the Portal application class of the program, indicating the ability to turn on the configuration server;

Configuration:
Service side: Application.properties

spring.application.name=config-serverserver.port=8888#配置git仓库地址spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/#配置仓库路径spring.cloud.config.server.git.searchPaths=respo#配置仓库的分支spring.cloud.config.label=master#访问git仓库的用户名(如果Git仓库为公开仓库,可以不填写用户名和密码)spring.cloud.config.server.git.username=your username#访问git仓库的用户密码spring.cloud.config.server.git.password=your password

Client: Bootstrap.properties

spring.application.name=config-clientspring.cloud.config.label=masterspring.cloud.config.profile=devspring.cloud.config.uri= http://localhost:8888/server.port=8881
    • Spring.cloud.config.label indicates the branch of the remote repository
    • Spring.cloud.config.profile
      • Dev Development environment configuration file
      • Test environment
      • Pro Formal environment
    • Spring.cloud.config.uri= http://localhost:8888/indicates the URL of the Configuration service center.

        http请求地址和资源文件映射如下:  · /{application}/{profile}[/{label}]  · /{application}-{profile}.yml  · /{label}/{application}-{profile}.yml  · /{application}-{profile}.properties  · /{label}/{application}-{profile}.properties
PS: High-availability Configuration center: When the service instance is many, the file is read from the configuration center, you can consider the configuration center as a micro-service, clustering it to achieve high availability;

Depend on:

<!-- 服务注册中心依赖开始 --><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-eureka-server</artifactId></dependency><!-- 服务注册中心依赖结束 --><!-- 配置服务端依赖开始 --><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-eureka</artifactId></dependency><!-- 配置服务端依赖结束 -->

Configuration:
Registration Center: APPLICATION.YML

#服务端口为8889server:  port: 8889# 作为服务注册中心的基本配置eureka:  instance:    hostname: localhost  client:    registerWithEureka: false    fetchRegistry: false    serviceUrl:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Service side: Bootstrap.properties

spring.application.name=config-clientspring.cloud.config.label=masterspring.cloud.config.profile=dev#spring.cloud.config.uri= http://localhost:8888/#加上服务注册地址为http://localhost:8889/eureka/eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/#是从配置中心读取文件spring.cloud.config.discovery.enabled=true#配置中心的servieId,即服务名spring.cloud.config.discovery.serviceId=config-serverserver.port=8881

Record these for the time being; Thank you for the great god Fang Zhipeng post on the Web:
Https://gitee.com/liudongyang/SpringCloudLearning

Springcloud Distributed Development Understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.