22 Şubat 2021 Pazartesi

SpringBoot Actuator @Endpoint Anotasyonu - Custom Endpoint İçindir

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.
  1. Create a new class. This class should be annotated with @Endpoint annotation.
  2. Provide an id attribute to this annotation. This will be the id of the endpoint or the name with which it will be accessed.
  3. Create a method in this class. This method should have @ReadOperation annotation.
  4. 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.
  5. Finally, this class should be a spring bean so it is annotated with @Component annotation.
  6. 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.
@Configuration
public class CustomActuatorConfiguration {

  @Bean
  public HelloWorldEndpoint helloWorldEndpoint() {
    return new HelloWorldEndpoint();
  }

}

@Endpoint(id = "helloworld")
public class HelloWorldEndpoint {

  @ReadOperation
  public String helloWorld() {
    return "Hello World";
  }
}
application.properties dosyasına şöyle yaparız
management.endpoints.web.exposure.include=helloworld
Sonucu 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,getusers
http://localhost:8080/actuator/getusers adresine gidersek çıktı şöyle
[
  {"name":"Mark Twain"},
  {"name":"Robinhood"}
]




















Hiç yorum yok:

Yorum Gönder