2 Ekim 2023 Pazartesi

SpringCloud Kubernetes

Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-kubernetes-fabric8-leader</artifactId>
  <version>3.0.2</version>
</dependency>
<dependency>
  <groupId>org.awaitility</groupId>
  <artifactId>awaitility</artifactId>
  <version>3.1.2</version>
  <scope>test</scope>
</dependency>
Örnek - Leader Election
Açıklaması şöyle. Yani aslında altta Spring Integration kullanılıyor. Sonuçlar CongigMap'te saklanıyor
The Spring Cloud Kubernetes leader election mechanism implements the leader election API of Spring Integration using a Kubernetes ConfigMap.

Multiple application instances compete for leadership, but leadership will only be granted to one. When granted leadership, a leader application receives an OnGrantedEvent application event with leadership Context. Applications periodically attempt to gain leadership, with leadership granted to the first caller. A leader will remain a leader until either it is removed from the cluster, or it yields its leadership. When leadership removal occurs, the previous leader receives OnRevokedEvent application event. After removal, any instances in the cluster may become the new leader, including the old leader.
Şöyle yaparız
spring.cloud.kubernetes.leader.role=world
# Configmap to which leader election metadata will be saved
spring.cloud.kubernetes.leader.config-map-name=my-config-map
Elimizde şöyle bir kod olsun
import org.springframework.integration.leader.Context;
import org.springframework.stereotype.Service;

@Service
public class ManagerContext {
  private Context context;


  public Context getContext() {
    return context;
  }

  public void setContext(Context context) {
    this.context = context;
  }
}
Lider kontrolü için şöyle yaparız
@Component
public class ScheduledTasks {

  private ManagerContext managerContext;
  public ScheduledTasks(ManagerContext managerContext){
    this.managerContext = managerContext;
  }
  ...
  private boolean isLeader() {
    // Logic to check if this instance is the leader
    // Modify the logic to check real leadership
    return managerContext.getContext() != null; 
  }
}
Lider mesajlarını işleyen kod şöyledir
}aa@RestController
@RequestMapping("/leader")
public class LeaderController {

 @Value("${spring.cloud.kubernetes.leader.role}")
 private String role;

 private Context context;

 private ManagerContext managerContext;

 public LeaderController(ManagerContext managerContext)  {
    
  this.managerContext =managerContext;
 }

 /**
  * Return a message whether this instance is a leader or not.
  * @return info
  */
 @GetMapping
 public String getInfo() {
  if (this.context == null) {
   return String.format("I am '%s' but I am not a leader of the '%s'", this.host, this.role);
  }
  return String.format("I am '%s' and I am the leader of the '%s'", this.host, this.role);
 }

 /**
  * PUT request to try and revoke a leadership of this instance. If the instance is not
  * a leader, leadership cannot be revoked. Thus "HTTP Bad Request" response. If the
  * instance is a leader, it must have a leadership context instance which can be used
  * to give up the leadership.
  * @return info about leadership
  */
 @PutMapping
 public ResponseEntity<String> revokeLeadership() {
  if (this.context == null) {
   String message = String.format("Cannot revoke leadership because '%s' is not a leader", this.host);
   return ResponseEntity.badRequest().body(message);
  }
  this.context.yield();
  String message = String.format("Leadership revoked for '%s'", this.host);
  return ResponseEntity.ok(message);
 }

 /**
  * Handle a notification that this instance has become a leader.
  * @param event on granted event
  */
 @EventListener
 public void handleEvent(OnGrantedEvent event) {
  System.out.println(String.format("'%s' leadership granted", event.getRole()));
  this.context = event.getContext();
  managerContext.setContext(this.context);
 }

 /**
  * Handle a notification that this instance's leadership has been revoked.
  * @param event on revoked event
  */
 @EventListener
 public void handleEvent(OnRevokedEvent event) {
  System.out.println(String.format("'%s' leadership revoked", event.getRole()));
  this.context = null;
  managerContext.setContext(null);
 }

}
Kubernetes ortamında Role, RoleBinding, ConfigMap yaratılmalıdır. Bunlar için yaml dosyaları burada



Hiç yorum yok:

Yorum Gönder