Micro-service (2): Spring Cloud Science literacy

Source: Internet
Author: User

Spring Cloud is a spring boot implementation-based MicroServices architecture development tool for configuration management, service discovery, circuit breakers, intelligent routing, micro-proxies, control buses, global locks, decision-making campaigns, Operations such as distributed sessions and cluster state management provide a one-stop development framework. The microservices architecture mentioned above is to split a complete application from data storage vertically into multiple different services, each of which can be deployed independently, independently maintained, independently scaled, and communicated between services and services through restful APIs.

A typical access to spring cloud-based architecture is as follows

#1 client requests access to internal services through a unified API Smart Routing Gateway Zuul, so that clients do not have to know the detailed address of the server or the relevant information, only through the API gateway can access the backend services, while the Zuul can authenticate or verify the request.

#2 Intelligent Routing Gateway Zuul receives the request, the host list of available services is obtained from the Service Registration Discovery Center Eureka, the host of the service instance is computed after the load balancing algorithm is calculated, and the request is forwarded, so that internally serves the external caller, or the dependency coupling between internal services turns into a dependency on Eureka.

#3 gets the service from the client load Balancer Ribbon to a specific instance of the backend service after obtaining services of the specified type.

#4 the back-end separate service to process business information through feign.
#5 disconnect is handled by Hystrix, which handles the service timeout fuse, thus avoiding the avalanche effect of service consumers that are not available due to service provider unavailability.
#6 cluster monitoring turbine is used to monitor calls and fuse-related indicators between services.

Quickly build a spring app with spring boot
With the increase in the spring integration suite and more configuration files, spring boot is used to simplify redundant configuration files and to automatically configure them based on conventions and package paths Spring-boot-starter-parent means referencing default dependencies and default configurations in the parent project template (for example, Src/main/resources/application.properties is spring Boot project's default profile); Spring-boot-starter-web represents the introduction of spring Web full stack modules, including spring MVC and Tomcat Spring-boot-maven-plugin indicates that the start and stop of spring boot can be controlled directly through the MVC command.
@RestController: Introduced in Sprint 4.0, is a collection of @controller and @responsebody that defines a controller that supports the rest interface.
@EnableAutoConfiguration: Enable automatic configuration based on conventions and package paths
@SpringBootApplication: Spring Boot-based family bucket configuration

Use Spring cloud Eureka for service registration and Discovery Management
With the increase of service in the system, the Maintenance service instance access Host:port becomes a very time-consuming and laborious thing, the host:port of various services may change at any moment, and such changes need to be recorded in real time, otherwise it may lead to service unavailability; Eureka is used to serve A call center between the provider and service client provides service registration and discovery capabilities that allow the provider and client to be fully decoupled and independent from each other.
@EnableEurakaServer: Create and start a service registry.
@EnableDiscoveryClient: Register the current app as a Eureka client app (itself a service provider for Eureka Management) and have the ability to discover other services.
Resttemplate.getforentity ("Http://HELLO-SERVICE/hello", String.class). GetBody (): Generate a GET request (also provides postforentity, put, Delete request), Eureka returns a map containing Hello-service and the address list of the service instance, and then invokes the specified service by using Resttemplate hello-service/hello,spring Cloud transforms the service name into the corresponding service instance address, string.class the data type of the returned result;

Client load Balancing with Spring cloud ribbon
Commonly referred to as load Balancing is the server load balancer (hardware such as F5, software such as Nginx), acting on the server, the request from the client to a certain policy assigned to internal service instances, and client load balancing is acting on the client, Each client can obtain a list of available service instances from the registry, enabling high availability of the system, mitigation of network pressure, and capacity expansion through client-side load balancing.
The Spring Cloud wrapper enables service-oriented rest template requests to be automatically encapsulated as client load-balanced service calls; Since spring Cloud is basically rest-based communication, service providers can basically do cluster deployment. So the ribbon is used as a tool framework for most of spring cloud services.
@LoadBalanced: Client-side load balancing of HTTP requests using Loadbalancerinterceptor, all requests made through Resttemplate are intercepted and dynamically allocated service instances.

Using spring Cloud Hystrix for service-tolerant protection
There may be mutual invocation dependencies between servers in the MicroServices architecture, service a needs to invoke service B, and service A also serves service C, and if the call to service a service B is unreachable because of some network failure, which ultimately results in service a being unavailable, which is known as a service failure avalanche, spring Cloud Hystrix Returns an error response to the service caller, rather than waiting blindly, by introducing fault monitoring of the break-through, when the service is unavailable and satisfies certain conditions.
@EnableCircuitBreaker: The startup class marked on the service caller, which indicates that the open-circuit-breaker function
@HystrixCommand (fallbackmethod= "Failcallbackfunc"): labeled on the service provider's target method, indicating that if the target method call times out or fails, Returns a resource method named Failcallbackfunc.

Use spring cloud feign to integrate ribbon and HYSTRIX functions for declarative service invocation
The Spring cloud ribbon and Spring Cloud Hystrix are both present in general applications, and in order to simplify the configuration process, you can use feign to configure client load balancing and micro-service fault tolerant protection uniformly.
@FeignClient ("Target-service"): The resource class labeled on the target service, binds the service name and specifies the @requestmapping resource,
@EnableFeignClients: The startup class labeled on the service caller, and the corresponding resource can be accessed directly through @controller, with load balancing and service fault tolerance protection.

implementing external API Gateway services with Spring Cloud Zuul
External requests to access internal services often require routing and load balancing via F5 or nginx, eventually sending to the corresponding service instance, operators need to manually maintain routing rules, load balancing rules and service instance address, as the internal complexity increases, need a similar FA? The ADE service uniformly handles external requests. Zuul not only enables routing and load balancing (automatically integrates the Ribbon and hystrix), but also enables user login verification and access authentication by adding filters.
@EnableZuulProxy: The Zuul API Gateway Service feature is turned on and the forwarding function can be implemented by adding the configuration of the routing rules in Application.properties:
zuul.routes.api-a-url.path=/api-a-url/**
zuul.routes.api-a-url.url=http://localhost:8081/
However, it is recommended to use the service ID in conjunction with EUREKA, and all requests from outside will first reach the Zuul implemented API Gateway server cluster, and then find the Serviceid corresponding to the request based on the routing rules. The mapping of the Serviceid and Host:port lists are then obtained according to the Eureka Registry, and the load balancing and disconnection is automatically implemented, eventually sending the request to the MicroServices instance.
zuul.routes.api-b-url.path=/api-b-url/**
Zuul.routes.api-b-url.serviceid=helo-service
The Zuul API Gateway implements the interceptor definition by inheriting Zuulfilter and implementing an abstract method:
Public String FilterType (): Indicates that the filter executes at that life stage of the request, the pre type implements the request authentication and the route map, the routing type implements the request to the specific instance, the post type processes the result returned by the request, The error type handles exception errors in the steamer process.
public int Filterorder (): Need to decide the order of execution when there are multiple filters in a life stage
public boolean Shouldfilter (): Whether the current filter needs to take effect
Public Object Run (): Filter-specific operation logic

Use Spring Cloud Config for centralized external configuration support
With the growth of the spring cloud cluster, the configuration needs to be managed and configured uniformly, and Spring Cloud Config can be used to get the configuration files from the Git repository uniformly through a single instance of the MicroServices.
@EnableConfigServer: Label the startup class of the MicroServices instance, and configure Git's access URL, path branch, user name, password, file version, and so on in Appication.properties.

Micro-service (2): Spring Cloud Science literacy

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.