Spring Cloud Netflix architecture (Summary), cloudnetflix

Source: Internet
Author: User

Spring Cloud Netflix architecture (Summary), cloudnetflix

Recently, I have been familiar with microservices and have some knowledge about this. I 'd like to share with you.

1. Microservice framework Spring Boot + Spring Cloud

Spring Cloud is a complete microservice implementation framework based on Spring Boot. It can be said that Spring Boot is the framework and Spring Cloud is the microservice, together constitute a new framework that cannot be ignored. It provides configuration management, service discovery, circuit breaker, intelligent routing, microproxy, control bus, global lock, decision-making campaign, distributed session, and cluster status management required for microservice development, easy to use. Spring Cloud contains many sub-frameworks. Among them, Spring Cloud Netflix is a set of frameworks. Its main modules include: service Discovery, circuit breaker and monitoring, intelligent routing, and client load balancing.

Features

  1. Eureka instances can register and discover beans managed by spring.
  2. The embedded Eureka server can be created using declarative Java configuration.
  3. The Hystrix client can be driven with simple annotations.
  4. Java configuration can enable embedded Hystrix Indicator Panel
  5. Client Server Load balancer
2. Spring Cloud Netflix components and deployment

(1) Eureka provides a service registration center, a service discovery client, and a convenient interface for viewing all registered services. All services use the Eureka service discovery client to register themselves to the Eureka server.

(2) Zuul, gateway, and all client requests access the background services through this gateway. It can use certain routing configurations to determine which service is used to process a URL. And obtain the registered service from Eureka to forward the request.

(3) Ribbon: Server Load balancer. When Zuul gateway sends a request to an application of a service, if one service starts multiple instances, it is sent to a service instance through a certain load balancing policy through Ribbon.

(4) If the Feign, service client, and services need to access each other, you can use RestTemplate or Feign client. By default, it uses Ribbon for load balancing.

(5) Hystrix, monitoring and circuit breaker. We only need to add the Hystrix tag to the service interface to implement monitoring and circuit breaker functions for this interface.

(6) Hystrix Dashboard, monitoring panel, which provides an interface to monitor the time consumed by service calls on each service.

(7) Turbine, monitoring aggregation, using Hystrix monitoring, we need to open the monitoring information of each service instance to view. Turbine can help us to aggregate the monitoring information of all service instances to a single place for unified viewing.

3. Spring Cloud Netflix component development

See its Chinese document: https://springcloud.cc/spring-cloud-netflix.html

(1) Service registration and monitoring center:

@SpringBootApplication@EnableEurekaServer@EnableHystrixDashboardpublic class ApplicationRegistry { public static void main(String[] args) {  new SpringApplicationBuilder(Application.class).web(true).run(args); }}

@ SpringBootApplication using the spring boot label indicates that the current application is a spring boot application. In this way, I can directly use the main function to start this application in the IDE, or use the command line to start the application after packaging. Of course, you can also start the packaged war package with servers such as tomcat. With the tag @ EnableEurekaServer, you can start the components of the Eureka Service Registration Center during startup. It listens to a port. The default value is 8761 to receive service registration. And provide a web page. After opening, you can see the registered service. Add @ EnableHystrixDashboard to provide a monitoring page. You can enter the address of the service to be monitored on the page to view the call status of the interface that enables Hystrix monitoring. Of course, to use the above components, we need to add corresponding dependencies to the maven POM file, such as using spring-boot-starter-parent, depends on spring-cloud-starter-eureka-server and spring-cloud-starter-hystrix-dashboard.

(2) inter-service call:

You can call services in either of the following ways: RestTemplate and FeignClient. In any way, it calls the http interface of the service through the REST interface. By default, parameters and results are serialized and deserialized through jackson. Because Spring MVC RestController defines interfaces, the returned data is serialized into json data through jackson.

First: RestTemplate. You only need to define a RestTemplate Bean and set it to LoadBalanced:

@Configurationpublic class SomeCloudConfiguration { @LoadBalanced @Bean RestTemplate restTemplate() {  return new RestTemplate(); }}

In this way, we can inject this bean as needed:

public class SomeServiceClass { @Autowired private RestTemplate restTemplate; public String getUserById(Long userId) {  UserDTO results = restTemplate.getForObject("http://users/getUserDetail/" + userId, UserDTO.class);  return results; }}

Here, users is the service ID, and Ribbon obtains an instance of this service from the service instance list, sends the request, and obtains the result. The UserDTO object requires a serial number, and its reverse serial number is automatically completed.

Type 2: FeignClient

@FeignClient(value = "users", path = "/users")public interface UserCompositeService { @RequestMapping(value = "/getUserDetail/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) UserDTO getUserById(@PathVariable Long id);}

We only need to use @ FeignClient to define an excuse. Spring Cloud Feign will generate an implementation for us to get data from the corresponding users service. Here, the value in @ FeignClient (value = "users", path = "/users/getUserDetail") is the service ID, and path is the path prefix of this group of interfaces. In the following method definition, it is like setting the Spring MVC interface. For this method, its URL is/users/getUserDetail/{id }. Then, when using it, it can be injected just like a General Service:

public class SomeOtherServiceClass { @Autowired private UserCompositeService userService; public void doSomething() {  // .....       UserDTO results = userService.getUserById(userId);  // other operation...      }}

(3) circuit breaker:

// Circuit breaker: To solve the problem of a failed method call, call the backup method to replace the failed method, the error tolerance/prevention of cascade errors has been achieved. // fallbackMethod specifies the backup method @ HystrixCommand (fallbackMethod = "doStudentFallback") @ RequestMapping (value = "dostudent", method = RequestMethod. GET) public String doStudent () {return "your name: secret, your age: secret! ";} Public String doStudentFallback () {return" your name: FEIFEI, your age: 26! ";}

Use @ EnableCircuitBreaker to enable Circuit Breaker support. Spring Cloud provides a console to monitor the running status of circuit breaker and enable it by @ EnableHystrixDashboard annotation.

The above are some simple introductions to Spring Cloud Netflix components. I hope it will be helpful for everyone's learning, and I hope you can support the house of helping customers more.

Related Article

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.