1. Overview
1.概述
In this quick article, we’ll have a look at how to customize the Spring Boot Actuators’ /info endpoint.
在这篇快速文章中,我们将看看如何定制Spring Boot Actuators的/info端点。
Please refer to this article to learn more about actuators in Boot and how to configure them.
请参考这篇文章以了解更多关于Boot中的执行器以及如何配置它们。
2. Static Properties in /info
2./info中的静态属性
If we have some static information like the name of the application or it’s version that does not change for a long time, then it’s a good idea to add that details in our application.properties file:
如果我们有一些静态信息,如应用程序的名称或它的版本长期不改变,那么在我们的application.properties文件中添加这些细节是个好主意。
## Configuring info endpoint
info.app.name=Spring Sample Application
info.app.description=This is my first spring boot application
info.app.version=1.0.0
That’s all we need to do to make this data available on the /info endpoint. Spring will automatically add all the properties prefixed with info to the /info endpoint:
这就是我们需要做的,使这些数据在/info端点上可用。Spring会自动将所有以info为前缀的属性添加到/info端点。
{
"app": {
"description": "This is my first spring boot application",
"version": "1.0.0",
"name": "Spring Sample Application"
}
}
3. Environment Variables in /info
3./info中的环境变量
Let’s now expose an Environment variable in our /info endpoint:
现在让我们在我们的/info端点中公开一个Environment变量。
info.java-vendor = ${java.specification.vendor}
This will expose the Java vendor to our /info endpoint:
这将把Java供应商暴露给我们的/info端点。
{
"app": {
"description": "This is my first spring boot application",
"version": "1.0.0",
"name": "Spring Sample Application"
},
"java-vendor": "Oracle Corporation",
}
Please note that all the environment variables are already available on the /env endpoint. However, the same can be exposed quickly on the /info endpoint as well.
请注意,所有的环境变量都已经在/env端点上可用。然而,同样的东西也可以在/info端点上快速暴露。
4. Custom Data From the Persistence Layer
4.来自持久层的自定义数据
Now let’s go one step further and expose some useful data from the persistence storage.
现在让我们更进一步,从持久化存储中公开一些有用的数据。
To achieve this, we need to implement InfoContributor interface and override the contribute() method:
为了实现这一点,我们需要实现InfoContributor接口并重写contribute()方法。
@Component
public class TotalUsersInfoContributor implements InfoContributor {
@Autowired
UserRepository userRepository;
@Override
public void contribute(Info.Builder builder) {
Map<String, Integer> userDetails = new HashMap<>();
userDetails.put("active", userRepository.countByStatus(1));
userDetails.put("inactive", userRepository.countByStatus(0));
builder.withDetail("users", userDetails);
}
}
The first thing is that we need to mark the implementing class as a @Component. Then add the required details to the Info.Builder instance provided to the contribute() method.
首先,我们需要将实现类标记为@Component。然后将所需的细节添加到Info.Builder实例中,提供给contribute()方法。
This approach provides us a lot of flexibility regarding what we can expose to our /info endpoint:
这种方法为我们提供了很大的灵活性,我们可以向/info端点暴露什么。
{
...other /info data...,
...
"users": {
"inactive": 2,
"active": 3
}
}
5. Conclusion
5.结论
In this tutorial, we looked at various ways to add custom data to our /info endpoint.
在本教程中,我们研究了向/info端点添加自定义数据的各种方法。
Note that we’re also discussing how to add git information into the /info endpoint.
请注意,我们也在讨论如何将git信息加入/info端点。
As always, the complete source code of this article can be found over on GitHub.
一如既往,本文的完整源代码可以在GitHub上找到。