皇上,还记得我吗?我就是1999年那个Linux伊甸园啊-----24小时滚动更新开源资讯,全年无休!

Spring Boot 2.0将会增强Actuator端点的特性

作者 Michael Redlich ,译者 张卫滨

即将发布的Spring Boot 2.0.0 M4将会增强actuator端点基础设施的特性。最重要的变更包括:

  • 支持Jersey RESTful Web服务
  • 支持基于反应式理念的WebFlux Web App
  • 新的端点映射
  • 简化用户自定义端点的创建
  • 增强端点的安全性

Spring Boot的actuator端点允许监控Web应用,并且可以与Web应用进行交互。在此之前,这些端点只支持Spring MVC,如果创建自定义端点的话,需要大量额外的编码和配置。

端点映射

内置的端点,比如/beans/health等等,现在都映射到了/application根上下文下。比如,之前Spring Boot版本中的/beans现在需要通过/application/beans进行访问。

创建用户自定义的端点

新的@Enpoint注解简化了创建用户自定义端点的过程。如下的样例创建了名为person的端点。(完整的示例应用可以在GitHub上查看。)

@Endpoint(id = "person")
@Component
public class PersonEndpoint {

    private final Map<String, Person> people = new HashMap<>();

    PersonEndpoint() {
        this.people.put("mike", new Person("Michael Redlich"));
        this.people.put("rowena", new Person("Rowena Redlich"));
        this.people.put("barry", new Person("Barry Burd"));
    }

    @ReadOperation
    public List<Person> getAll() {
        return new ArrayList<>(this.people.values());
    }

    @ReadOperation
    public Person getPerson(@Selector String person) {
        return this.people.get(person);
    }

    @WriteOperation
    public void updatePerson(@Selector String name, String person) {
        this.people.put(name, new Person(person));
    }

    public static class Person {
        private String name;

        Person(String name) {
            this.name = name;
        }

        public String getName() {
            return this.name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

这个端点借助@ReadOperation@WriteOperation注解暴露了三个方法。这个端点的定义不再需要额外的代码,它可以通过/application/person/application/person/{name}进行访问。另外,这个端点同时还会自动部署为JMXMBean,可以通过像JConsole这样的JMX客户端来访问。

增强端点的安全性

Spring Boot 2.0采用一种稍微不同的方式来确保Web端点默认的安全性。Web端点默认是禁用的,management.security.enabled属性已经被移除掉了。单个端点可以通过application.properties文件中的配置来启用。比如:

endpoints.info.enabled=true
endpoints.beans.enabled=true

但是,我们还可以把endpoints.default.web.enabled属性设置为true,从而将actuator和用户自定义的所有端点暴露出去。

Stéphane Nicoll是Pivotal的首席软件工程师,关于actuator的端点事宜,InfoQ与他进行了交流。

InfoQ:升级actuator端点来支持Jersey和WebFlux的过程中,您能描述一下您的体验吗?

Stéphane Nicoll:同时支持基于servlet的传统环境以及基于reactive理念的Web App是一个很大的挑战,尤其是在处理可扩展性特性方面更是如此。

InfoQ:Spring因为其紧凑且定义良好的API而闻名。这次重构又是怎样处理的呢?

Nicoll:在框架团队中,“spring-webmvc”和“spring-webflux”共享了很多来自“spring-web”的特性,其中我们可以看到很多创造性的设计。构建抽象是非常困难的,我非常开心我们在另外一个层级上完成了相同的事情。

InfoQ:驱动架构变化的原则是什么呢?

Nicoll: Spring Boot 2.0主要关注于搭建坚实的基础和良好的共识:我们相信这个新的端点基础设施方向是正确的,它所针对的是生产环境的特性,我们期待来自社区的反馈。

InfoQ:Spring Boot 2.0 GA版本预期会在何时发布?

Nicoll:事情尚不确定(双关语,这里原文使用的是flux,可能同时指不确定性和对WebFlux的支持),但是我们目前的计划是在年底释放Spring Boot 2.0 GA。

参考资料

查看英文原文Spring Boot 2.0 Will Feature Improved Actuator Endpoints

转自 http://www.infoq.com/cn/news/2017/09/spring-boot-2-actuator-endpoints