Today is the 134th day for Liu Xiaoai to learn Java by herself.
Thank you for watching. Thank you.
The learning content is as follows:
- SpringCloud contact.
- Use SpringCloud to build the micro-service architecture. Of course, this is estimated to take 3 days to complete. Today, I will mainly study the registration center Eureka.
A, SpringCloud
Microservices are a system architecture approach that will ultimately require a technical architecture to implement.
We use SpringCloud, which is another Spring project, needless to say.
So for Java development, Spring is a real catch. It’s everywhere.
Its main components include:
- Eureka: Registry
- Zuul: Service gateway
- Ribbon: Load balancing
- Feign: service invocation
- Hystix: Fuse
Study these points in turn over the next few days.
Eureka: Registry
As the name implies, the role of a registry is to manage the services and record the information provided by the services.
1 had the service side
All services must be registered on the Eureka server.
So we need to create a server first. How do we make a SpringBoot application a Eureka server?
① Introduce eureka server dependency
This dependency is very long, remember 2 core points:
- Starter means starter.
- Erueka-server corresponds to the erueka server. If there is a server, there must be a client.
Start Class Start the Eureka service
@enableEurekaserver (@enableEurekaserver)
The SpringBoot application is used as the Euerka server, which is the registry mentioned above.
3 configure the registry address
It means itself. What does that mean?
The Eureka server is designed to manage services. All services need to be registered with it, and it is a service itself, so it needs to register itself.
Of course, we can also set it to not register itself, so we won’t expand here.
④ Name the service
Application translation is an application, in this case a service, named Eureka-server.
2Eureka client: Registration of services
For example, user-service is used to query data related to users. Register this service in the registry.
Once registered, other services can call it directly.
① Introduce eureka client dependency
Same, 2 core points:
- Starter means starter.
- Erueka-client Indicates the client of erueka.
Start class Start the Eureka client
@enableeurekaclient, which is also annotated, start the Eureka client.
However, the @enableDiscoveryClient is much more powerful, including enabling the Eureka client.
3Eureka client: Service discovery
When a service is registered, another service consumer has to use it directly, which is called discovery of the service.
Since it is a service, it must be registered first, the same steps: importing dependencies, adding client annotations to the startup class.
Skip the above steps and invoke the service directly.
-
DiscoverClient: That is, the client that launches the annotation configuration on the class.
-
RestTemplate: This was studied in detail yesterday and is used to implement calls between services.
① Pull service
You can pull a specified service using the getInstances() method of discoverClient.
② Load Balancing
Use load balancing to get one of these services, because we only have one service here, so we don’t need it.
The use of load balancing is described in detail at the end.
③ String placeholders
The getForObject() method of restTemplate makes a GET request to the specified URL.
Concatenation is implemented using the format() method of String, concatenating the following IP, port, and ID into the %s placeholder.
4 Service invocation test
After the configuration is complete, the consumer-demo service can be used to call the user-service directly.
According to the ID to the database to query the corresponding user, we only realized the code writing of the Service layer and dao layer in user-service service.
To use it in the consumer-demo service, simply call the user-service service.
Three, Eureka details
Three central roles in Eureka’s architecture:
① Service registry
Eureka is a server application that provides service registration and discovery functions, also known as eureka-Server.
② Service provider
The application that provides services can be a SpringBoot application or any other technology, as long as it provides Rest style services externally. User-service is described in the preceding section.
③ Serve consumers
The consumer gets the list of services from the registry to know about each server and where to call the server, which is our consumer-demo implementation.
2 Service Renewal
lease-renewal-interval-in-seconds
The default value is 30 seconds. Renewal means renewal.
lease-expiration-duration-in-seconds
Expiration time (default: 90 seconds). Expiration itself is the end.
That is, by default the service sends a heartbeat to the registry every 30 seconds to prove that it is still alive.
If the heartbeat is not sent for more than 90 seconds, the registry will consider the service GG and remove it.
Do not change these two values in the production environment. The default values are fine.
3 Load Balancing Ribbon
We have just started a user-service, which is accessed through DiscoveryClient to obtain service instance information. However, in the actual environment, more than one user service is enabled.
There will be multiple services in the list. Which one should we access?
Eureka already has a load balancing component called the Ribbon, which can be used with simple code modifications.
1 Enable load balancing
This is enabled with the @loadbanlanced annotation.
Once load balancing is enabled, it can be used directly in the code for inter-service calls.
(2) the url to write
The IP and port in the URL use the name of the service.
The simple understanding is that the service consumer calls the service provider, but the service provider opens many.
Load balancing means that this call may call the service provider (1) and the other call may call the service provider (2).
Multiple services are called in turn, but it’s essentially one service, oops, I don’t know…
The last
Thanks for watching.
If you can, please give it a thumbs up. Thank you.