In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces how the REST API and API extension of Eureka is, the content is very detailed, interested friends can refer to it, hope to be helpful to you.
Eureka REST API
Eureka as a registry, its essence is to store the registration information of each client. When forwarding, Ribbon will get the list of services of the registry, and then according to the corresponding routing rules to select a service to Feign to invoke. If we don't choose Spring Cloud technology, we also want to use Eureka, okay? Absolutely.
If it is not Spring Cloud technology stack, the author recommends using Zookeeper, this will be more convenient, of course, it is possible to use Eureka, so it will involve how to register information, how to obtain registration information and other operations. In fact, Eureka also takes this into account and provides a lot of REST interfaces for us to call.
Let's give a useful example to illustrate, such as the dynamic configuration of upstream for Nginx.
Recommend distributed architecture source code
After the architecture becomes a micro-service, the micro-service has no dependency, can be deployed independently, and the port can be randomly assigned. Anyway, it will be registered in the registry, and the caller does not need to care about the IP and Port of the provider, which can be obtained from the registry.
But there is a question: can the deployment of API gateways be like this? Most API gateways use Nginx as the load, so Nginx must know which nodes the API gateway has, so that the gateway service cannot be started casually and needs to be fixed.
Of course, the gateway will not be changed often, and it will not be released frequently, so there is no big problem. The only bad thing is that it cannot be expanded automatically.
In fact, using the API provided by Eureka, we can obtain the instance information of a service, that is, we can dynamically configure the upstream of Nginx according to the data in Eureka.
In this way, the gateway can be deployed and expanded automatically. There are also many solutions on the Internet, which can be done with Lua scripts, or you can write Sheel scripts yourself.
Here is an example of how to get the information registered in Eureka. For more information about the interface, please see the official document "https://github.com/Netflix/eureka/wiki/Eureka-REST-operations"."
To obtain the registration information of a service, you can make a direct GET request: http://localhost 8761 to eureka hand, appsCure to eureka, customer, user, service. Where eureka-client-user-service is the application name, that is, spring.application.name.
In the browser, the data is displayed in XML format by default, as shown in figure 1.
If you want to return the format of the Json data, you can use some interface test tools, such as Postman, to add the following two lines of code to the request header.
Content-Type:application/json Accept:application/json
If Eureka enables authentication, remember to add authentication information. The user name and password must be the Authorization:Basic user name: password encoded by Base64. The rest of the APIs will not be explained too much. You can try it yourself. Postman directly supports Basic authentication. Switch the option from Headers to Authorization, and select Basic Auth as the authentication method to fill in the user information.
After filling in, just initiate the request directly. When we switch to the Headers option, we can see that there is already an extra Authorization header in the request header.
Metadata usage
There are two types of metadata in Eureka, namely, standard metadata defined by the framework and user-defined metadata. Standard metadata refers to information such as hostname, IP address, port number, status page, and health check, which is published in the service registry for calls between services. Custom metadata can be configured using eureka.instance.metadataMap.
The common point of custom metadata is custom configuration. We can define some of our own configurations for each Eureka Client, which will not affect the functionality of Eureka.
Custom metadata can be used to do some extended information, such as grayscale publishing and other functions, you can use metadata to store grayscale published state data, Ribbon forwarding can be based on the service metadata to do some processing. When there is no need for grayscale publishing, you can call REST API provided by Eureka to erase the metadata.
Let's customize a simple metadata, which is configured as follows in the properties file:
Eureka.instance.metadataMap.biancheng=zhangsan
The above code defines a configuration where key is biancheng and value is zhangsan. Restart the service, and then use the REST API provided by Eureka to see if the metadata you just configured already exists in Eureka, as shown in figure 2.
EurekaClient usage
When Eureka is integrated into our project, we can use EurekaClient to get some of the data we want, such as the metadata mentioned above. We can get it directly through EurekaClient (the code is shown below) without having to call the REST API provided by Eureka.
@ Autowiredprivate EurekaClient eurekaClient;@GetMapping ("/ article/infos") public Object serviceUrl () {return eurekaClient.getInstancesByVipAddress ("eureka-client-user-service", false);}
Call the interface through PostMan to see if the data we want has been returned. At this time, we will find that the data obtained through EurekaClient is the same as that obtained by ourselves without API, and the former is more convenient from the point of view of use.
In addition to using EurekaClient, you can also use DiscoveryClient (the code is shown below), which is not native to Feign, but is re-encapsulated by Spring Cloud, and the class path is org.springframework.cloud.client.discovery.DiscoveryClient.
@ Autowiredprivate DiscoveryClient discoveryClient;@GetMapping ("/ article/infos") public Object serviceUrl () {return discoveryClient.getInstances ("eureka-client-user-service");} Health check
By default, the Eureka client uses heartbeat and server communication to determine whether the client is alive. In some scenarios, such as MongoDB, there is an exception, but your application process still exists, which means that the application can continue to report through the heartbeat to keep its own information from being deleted from the Eureka.
Spring Boot Actuator provides / actuator/health endpoint, which can display the health information of the application. When MongoDB is abnormal, the state of the / actuator/health endpoint becomes DOWN. Because the application itself is indeed alive, but the exception of MongoDB will affect some functions, operation failure will occur when the request arrives at the application.
In this case, we want to pass the health information to the Eureka server. In this way, the instance information of the application can be offline in time in Eureka, isolate normal requests and prevent errors. Enable health check by configuring the following:
Eureka.client.healthcheck.enabled=true
We can simulate the exception by extending the endpoint of the health check, define an extended endpoint, and set the state to DOWN, as shown below.
@ Componentpublic class CustomHealthIndicator extends AbstractHealthIndicator {@ Overrideprotected void doHealthCheck (Builder builder) throws Exception {builder.down () .withDetail ("status", false);}
After the extension, we visit / actuator/health and we can see that the current state is DOWN, as shown in figure 3.
The status in Eureka is UP. In this case, the request can still be forwarded to this service. Let's turn on the monitoring check, check the status in Eureka again, and find that the status changes to DOWN (1).
Service online and offline monitoring
Under some specific requirements, we need to monitor the online and offline service, and email notifications are provided for both online and offline services. Event monitoring is provided in Eureka to expand.
The events currently supported are as follows:
EurekaInstanceCanceledEvent service offline event.
EurekaInstanceRegisteredEvent service registration event.
EurekaInstanceRenewedEvent service renewal event.
EurekaRegistryAvailableEvent Eureka registry startup event.
EurekaServerStartedEvent EurekaServer start event.
Based on the event mechanism provided by Eureka, you can monitor the online and offline process of the service, and send e-mail to notify when the process occurs. The following code only demonstrates the monitoring process and does not send an email.
@ Componentpublic class EurekaStateChangeListener {@ EventListenerpublic void listen (EurekaInstanceCanceledEvent event) {System.err.println (event.getServerId () + "\ t" + event.getAppName () + "Service offline");} @ EventListenerpublic void listen (EurekaInstanceRegisteredEvent event) {InstanceInfo instanceInfo = event.getInstanceInfo (); System.err.println (instanceInfo.getAppName () + "register") } @ EventListenerpublic void listen (EurekaInstanceRenewedEvent event) {System.err.println (event.getServerId () + "\ t" + event.getAppName () + "Service Renewal");} @ EventListenerpublic void listen (EurekaRegistryAvailableEvent event) {System.err.println ("registry startup");} @ EventListenerpublic void listen (EurekaServerStartedEvent event) {System.err.println ("EurekaServer startup");}}
Note: in the Eureka cluster environment, each node will trigger an event. At this time, you need to control the behavior of sending notifications. If not, each node will send notifications.
On Eureka's REST API and API extension is how to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.