Giriş
Şu satırı dahil ederiz
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.annotation.WriteOperation; import org.springframework.boot.actuate.endpoint.annotation.Selector;
Açıklaması şöyle
Following are the steps to create a new endpoint.
- Create a new class. This class should be annotated with @Endpoint annotation.
- Provide an id attribute to this annotation. This will be the id of the endpoint or the name with which it will be accessed.
- Create a method in this class. This method should have @ReadOperation annotation.
- When the endpoint is accessed, the method with @ReadOperation will be invoked. Thus, this method should return the data that you want to send when the endpoint is accessed.
- Finally, this class should be a spring bean so it is annotated with @Component annotation.
- Register this endpoint id in application.properties to enable it as shown below.management.endpoints.web.exposure.include = info,env,health,getusers
1. @Endpoint olarak işaretli sınıfı bir bean kodlanır. 
2. Bu bean içinde 
- GET isteği için @ReadOperation,
- POST isteği için @WriteOperation, 
- DELETE isteği için @DeleteOperation 
olarak işaretli metodlar olabilir. Metodlar parametre alabilirler
3. Bu bean application.properties dosyasında Spring'e tanıtılır. 
Örnek
Şöyle yaparız. "http://localhost:8080/actuator" adresindeki listede "helloworld" isimli bir endpoint görebiliriz.
application.properties dosyasına şöyle yaparız@Configurationpublic class CustomActuatorConfiguration {@Beanpublic HelloWorldEndpoint helloWorldEndpoint() {return new HelloWorldEndpoint();}}@Endpoint(id = "helloworld")public class HelloWorldEndpoint {@ReadOperationpublic String helloWorld() {return "Hello World";}}
management.endpoints.web.exposure.include=helloworldSonucu görmek için şöyle yaparız
> curl 'http://localhost:8080/actuator/helloworld'
Hello World
Örnek
Şöyle yaparız
@Component
@Endpoint(id="getusers")
public class LoggedInUserFinder {
  List<User> users = new ArrayList<>();
  @ReadOperation
  public List<User> getUsers() {
    User userOne = new User();
    userOne.setName("Mark Twain");
    User userTwo = new User();
    userTwo.setName("Robinhood");
    users.add(userOne);
    users.add(userTwo);
    return users;
  }
  @ReadOperation
  public User getUser(@Selector String userName) {
    // return user matching name
    for (User user : users) {
      if(user.getName().equals(userName)) {
        return user;
      } 
    }
     return new User();
  }
  @DeleteOperation
  public User removeUser(@Selector String userName) {
    // remove user matching name
    for (User user : users) {
      if(user.getName().equals(userName)) {
        users.remove(user);
        return user;
      } 
    }
     return new User();
  }
  @WriteOperation 
  public User removeUser(@Selector String userName) {
     // create user with name
     User user = new User(); 
     // add user
     users.add(user);
  }
  // User model class
  static class User {
    private String name;
    public String getName() {
      return name;
    }
    public void setName(String name) {
      this.name = name;
    }
  }
}application.properties dosyasına şöyle yaparız
management.endpoints.web.exposure.include = info,env,health,getusershttp://localhost:8080/actuator/getusers adresine gidersek çıktı şöyle
[
  {"name":"Mark Twain"},
  {"name":"Robinhood"}
] 
Hiç yorum yok:
Yorum Gönder