I have been exposed to microservices recently and have some understanding of this aspect. I will share it with you.
Spring Cloud is a complete set of frameworks for implementing microservices based on Spring Boot. It can be said that Spring Boot as the framework and Spring Cloud as the microservices together form a new framework system that cannot be ignored. It provides the components required for microservice development such as configuration management, service discovery, circuit breakers, intelligent routing, micro-agents, control bus, global locks, decision-making campaigns, distributed sessions and cluster state management, which are convenient and easy to use. Spring Cloud includes a lot of subframes, among which Spring Cloud Netflix is one of the frameworks, which mainly provide modules: service discovery, circuit breakers and monitoring, intelligent routing, client load balancing, etc.
characteristic
(1) Eureka, service registration and discovery, provides a service registration center, service discovery client, and a convenient interface to view all registered services. All services use Eureka's service discovery client to register themselves to Eureka's server.
(2) Zuul, gateway, all clients request to access the backend services through this gateway. It can use certain routing configurations to determine which service handles a certain URL. And get the registered service from Eureka to forward the request.
(3) Ribbon, that is, load balancing. When Zuul gateway sends a request to an application of a certain service, if a service starts multiple instances, it will send it to a certain service instance through Ribbon through a certain load balancing policy.
(4) Feign, service client, if services need to access each other, you can use RestTemplate or Feign client to access. It uses Ribbon by default to achieve load balancing.
(5) Hystrix, monitoring and circuit breaker. We only need to add a Hystrix tag to the service interface to realize the monitoring and circuit breaker functions of this interface.
(6) Hystrix Dashboard, monitoring panel, provides an interface that can 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 aggregate the monitoring information of all service instances into one place for unified viewing.
You can refer to the document in it: 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); }}Here, @SpringBootApplication using the spring boot tag shows that the current application is a spring boot application. In this way, I can directly use the main function to start the application in the IDE, or I can start it with the command line after packaging. Of course, you can also start the packaged war package with a server such as tomcat. Use the tag @EnableEurekaServer to start the components of the Eureka Service Registry during startup. It will listen for a port, which is 8761 by default, to receive service registration. And provide a web page. After opening it, you can see the registered service. Adding @EnableHystrixDashboard will provide a monitoring page. We can enter the address of the service to be monitored on it to view the call status of the interface with Hystrix monitoring enabled. Of course, in order to use the above components, we need to add corresponding dependencies in the maven POM file, such as using spring-boot-starter-parent, relying on spring-cloud-starter-eureka-server and spring-cloud-starter-hystrix-dashboard.
(2) Inter-service call:
There are two ways to make service calls, RestTemplate and FeignClient. No matter what the method is, it calls the service's http interface through the REST interface, and the parameters and results are serialized and deserialized by default through jackson. Because the interface defined by Spring MVC's RestController, the returned data are serialized into json data through jackson.
The first type: 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 where we need it:
public class SomeServiceClass { @Autowired private RestTemplate restTemplate; public String getUserById(Long userId) { UserDTO results = restTemplate.getForObject("http://users/getUserDetail/" + userId, UserDTO.class); return results; }}Where, users is the service ID, Ribbon will obtain an instance of this service from the service instance list, send a request, and obtain the result. The object UserDTO requires a serial number, and its anti-serial number will be automatically completed.
The second type: 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 define an excuse using @FeignClient. Spring Cloud Feign will help us generate an implementation and get data from the corresponding users service. Among them, 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, just like setting up the Spring MVC interface, for this method, its corresponding URL is /users/getUserDetail/{id}. Then, when using it, just like injecting a general service and then use it:
public class SomeOtherServiceClass { @Autowired private UserCompositeService userService; public void doSomething() { // ...... UserDTO results = userService.getUserById(userId); // other operation... }}(3) Circuit breaker:
// Circuit breaker: In order to solve the problem that calls the fallback method to replace the failed method when a method call fails, the function of fault tolerance/blocking cascade errors has been achieved. //fallbackMethod specifies the fallback 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!";}Among them, use @EnableCircuitBreaker to enable circuit breaker support. Spring Cloud provides a console to monitor the operation of the circuit breaker, which is enabled through the @EnableHystrixDashboard annotation.
The above is a brief introduction to Spring Cloud Netflix components. I hope it will be helpful to everyone's learning, and I hope everyone will support Wulin.com more.