In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to make your service more humanized in eureka". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Let's take a look at the visual interface of eureka as follows.
We know that the service name MICROSERVICE-ORDER on the left is specified in the project through the configuration file. The information on the right is the service information registered with eureka, but we can't get the details through this information. The first thing we need to change is this place.
1. Give the service a more pleasing name.
Let me take an example first, or the example given in the previous section, many startups live in office buildings in economic development zones, so every company must have a name, or a suitable name to the outside world. You can't specify the company with a long list of company codes, which is not easy to remember, but more importantly, difficult to identify.
When there are many micro services registered with Eureka, if any service is broken, we hope to be able to intuitively know which service has gone wrong, so that we can locate the problem more quickly. For example, if we know that this is an order service and the port number is 8001, wouldn't it be more intuitive if we directly display "order Service-8001"? The answer is yes, we can specify it through eureka.instance.instance-id in the configuration file of the order service. As follows:
two。 Assign a correct IP to the service
Above, we give the service a more pleasing name, but when we move the mouse over it, we look at the url message in the lower left corner, as follows:
As you can see, the display is: http://ifly-1741:8001/actuator/info, I don't know what it is.
It's like saying, I'm telling you, if you want to know about our company, you can come to the 8001 floor of xxx Building to learn about it. But I don't even know the address of the xxx building.
Of course, we don't want this to happen. The readability is too low. In theory, it should show the ip of the service itself, so how to make the ip display normal?
Eureka has a configuration eureka.instance.prefer-ip-address, which defaults to false, and we need to set it to true so that the ip address can be displayed properly.
3. Give the service a display details page
After completing the above step, we click on the link "order Service-8001" and visit:
Http://192.168.75.1:8001/actuator/info this address, this is no problem. However, a 404 error is reported, indicating that the details of the service cannot be found.
This is like saying that the name of my company is also given to you, and you follow Amap's navigation to find me, but my information has not yet been imported into Amap. In other words, Amap can not monitor my information, of course you can not find me. What are we going to do?
We see that there is an actuator in this url. Yes, Actuator is used to monitor in Spring Boot, and we need to import this dependency when we use it.
Org.springframework.boot
Spring-boot-starter-actuator
But just import and rely on ah, just like you went to Amap to sign up for an account, but your details have to be entered into Amap's system. What are we going to do?
After importing the actuator dependency, we can initialize the information about these services in the configuration file, and when the user clicks the service link above, it can be displayed in json format. For example:
# used to display the basic information of the project
Info:
Author.name: shengwu ni
App.name: microservice
Server.port: ${server.port}
Application.name: ${spring.application.name}
When you click "order Service-8001" to access http://192.168.75.1:8001/actuator/info, the following json appears, that is, the information configured in our project.
{
"author": {
"name": "shengwu ni"
}
"app": {
"name": "microservice"
}
"server": {
"port": "8001"
}
"application": {
"name": "microservice-order"
}
}
Huh? This is more friendly, I also have the name of the company, the address is also given to you, the information on the map is also entered, and then you can smoothly come to our company to visit, to understand our information.
4. Give others a chance to get to know you
Through some of the above configurations, the relevant information about this service can be said to be more friendly. But there is another problem, ah, my company alias is good, the address is also included in the map, but how can I let the outside world know this information? I need to have a company home page, that is to say, an entrance to let others know this information.
This is called service discovery. We all talk about Eureka service registration and discovery. Up to now, we have been talking about the service registration function of Eureka, so that services can be registered in Eureka. We can see these services in the Eureka interface, but how can they be discovered by the outside world? We need to expose an interface to the outside world to provide details of this service.
How can it be provided? We need to write an interface that is exposed to external calls, as follows:
/ * *
* order service
* @ author shengwu ni
, /
@ RestController
@ RequestMapping ("/ provider/order")
Public class OrderProviderController {
@ Resource
Private EurekaClient client
Private static final Logger LOGGER = LoggerFactory.getLogger (OrderProviderController.class)
@ GetMapping ("/ discovery")
Public Object discovery () {
/ / get all the service nodes in Eureka
List applications = client.getApplications () .getRegisteredApplications ()
If (applications! = null) {
For (Application application: applications) {
/ / the name of the exposed service
String name = application.getName ()
/ / only look at the order service information
If ("MICROSERVICE-ORDER" .equals (name)) {
/ / how many instances of the service, for example, multiple order services may be deployed, and multiple order services may be registered with eureka
List instances = application.getInstances ()
LOGGER.info ("all order Services: {}", instances)
If (instances! = null) {
For (InstanceInfo info: instances) {
LOGGER.info ("Service id: {}", info.getInstanceId ())
LOGGER.info ("Service Host: {}", info.getHostName ())
LOGGER.info ("Service Port: {}", info.getPort ())
}
}
Return instances
}
}
}
Return null
}
}
First of all, we need to introduce the EurekaClient side. Inside the interface, we can get all the Application registered on the Eureka through EurekaClient. In fact, the application here is what we often call the service node, so as to obtain the information of the service node, including the name, status, IP, port, heartbeat and other information of the service node. You can return the information of the service directly.
In this way, by accessing the interface, we can clearly see the details of the service through the returned json.
This is the end of the content of "how to make your services more user-friendly in eureka". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.