<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: martinezhenry</title>
    <description>The latest articles on DEV Community by martinezhenry (@martinezhenry).</description>
    <link>https://dev.to/martinezhenry</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F528105%2F076d5979-ddd2-434a-916c-aee6991b445a.png</url>
      <title>DEV Community: martinezhenry</title>
      <link>https://dev.to/martinezhenry</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/martinezhenry"/>
    <language>en</language>
    <item>
      <title>Spring Boot Async</title>
      <dc:creator>martinezhenry</dc:creator>
      <pubDate>Tue, 15 Nov 2022 00:11:09 +0000</pubDate>
      <link>https://dev.to/martinezhenry/spring-boot-async-1p4n</link>
      <guid>https://dev.to/martinezhenry/spring-boot-async-1p4n</guid>
      <description>&lt;p&gt;Today we talk about how to use the annotation &lt;code&gt;@Async&lt;/code&gt; of Spring Boot to create method asynchronous in our applications.&lt;/p&gt;

&lt;p&gt;Using asynchronous methods allows you to execute tasks that require much time to finish without waiting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Environment
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Java 11&lt;/li&gt;
&lt;li&gt;Spring Boot 7.0.1&lt;/li&gt;
&lt;li&gt;Maven 3.8.6&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Preparing a Method
&lt;/h2&gt;

&lt;p&gt;In this example, we used a method called &lt;code&gt;processDelay&lt;/code&gt; that executes a random delay time from 0 to 1500 milliseconds. This method looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void processDelay() throws InterruptedException {
      long delay = ThreadLocalRandom.current().nextLong(0, 1500 + 1);
      log.info("processing delay: {}", delay);
      Thread.sleep(delay);
      log.info("delay time processed");

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The sentence &lt;code&gt;Thread.sleep(delay);&lt;/code&gt; is the apply the delay time and the sentence &lt;code&gt;long delay = ThreadLocalRandom.current().nextLong(0, 1500 + 1);&lt;/code&gt; get a random long number from 0 to 1500 to indicate the first one the time to wait.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using &lt;code&gt;@Async&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;With this annotation we can convert a simple method into an asynchronous method, in our example we will convert &lt;code&gt;processDelay&lt;/code&gt; method to an asynchronous one, we only need to add the annotation &lt;code&gt;@Async&lt;/code&gt; at the top of our method, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Async
public void processDelay() throws InterruptedException {
    long delay = ThreadLocalRandom.current().nextLong(0, 1500 + 1);
    log.info("processing delay: {}", delay);
    Thread.sleep(delay);
    log.info("delay time processed");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Enabling Async
&lt;/h3&gt;

&lt;p&gt;To can use &lt;code&gt;@Async&lt;/code&gt; annotation also we need to active the Spring Boot features async, for do that, we need to add the annotation &lt;code&gt;@EnableAsync&lt;/code&gt;, this one should be added to a class, I prefer to include it on the main class.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@SpringBootApplication
@EnableAsync
public class AsyncApplication {
    public static void main(String[] args) {
        SpringApplication.run(AsyncApplication.class, args);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Testing
&lt;/h3&gt;

&lt;p&gt;To validate the async method we will use a &lt;code&gt;@RestController&lt;/code&gt; to call our method and pass it how many executions to apply the method, this is our controller example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@RestController
@Slf4j
public class DelayController implements IDelayController {

    private final IAsyncProcessorService asyncProcessorService;

    public DelayController(IAsyncProcessorService asyncProcessorService){
        this.asyncProcessorService = asyncProcessorService;
    }

    @Override
    @GetMapping(value = "delay/{executions}")
    public void processDelayTime(@PathVariable(value = "executions") int executions) throws InterruptedException {
        log.info("Tries: {}", executions);
        for(int i = 0; i &amp;lt; executions; i++) {
            this.asyncProcessorService.processDelay();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now, if we run the application and request our controller, we can see how the method is executed as a background task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;022-11-07 22:14:08.958  INFO 35748 --- [nio-8080-exec-2] c.h.l.c.implementations.DelayController  : Tries: 2
2022-11-07 22:14:08.968  INFO 35748 --- [         task-2] c.h.l.s.i.AsyncProcessorService          : processing delay: 957
2022-11-07 22:14:08.968  INFO 35748 --- [         task-1] c.h.l.s.i.AsyncProcessorService          : processing delay: 263
2022-11-07 22:14:09.249  INFO 35748 --- [         task-1] c.h.l.s.i.AsyncProcessorService          : delay time processed
2022-11-07 22:14:09.926  INFO 35748 --- [         task-2] c.h.l.s.i.AsyncProcessorService          : delay time processed
2022-11-07 22:14:15.184  INFO 35748 --- [nio-8080-exec-3] c.h.l.c.implementations.DelayController  : Tries: 2
2022-11-07 22:14:15.185  INFO 35748 --- [         task-3] c.h.l.s.i.AsyncProcessorService          : processing delay: 403
2022-11-07 22:14:15.185  INFO 35748 --- [         task-4] c.h.l.s.i.AsyncProcessorService          : processing delay: 736
2022-11-07 22:14:15.591  INFO 35748 --- [         task-3] c.h.l.s.i.AsyncProcessorService          : delay time processed
2022-11-07 22:14:15.924  INFO 35748 --- [         task-4] c.h.l.s.i.AsyncProcessorService          : delay time processed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The response is very fast, indicating that the controller is not waiting for the delay, other evidence that the method is executing asynchronously.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--At9FgVhl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667878692424/q0D7-UszS.png%2520align%3D%2522left%2522" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--At9FgVhl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1667878692424/q0D7-UszS.png%2520align%3D%2522left%2522" alt="image.png" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this post, we can see how to use the Spring Boot &lt;code&gt;@Async&lt;/code&gt; annotation to execute methods in background tasks without that we need to wait until the execution finishes.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.spring.io/spring-framework/docs/current/reference/html/integration.html#scheduling-annotation-support"&gt;Annotation Support for Scheduling and Asynchronous Execution&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://docs.spring.io/spring-framework/docs/current/reference/html/integration.html#scheduling-annotation-support-async"&gt;The @Async annotation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>springboot</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Interacting with a Kubernetes Cluster - Kind</title>
      <dc:creator>martinezhenry</dc:creator>
      <pubDate>Tue, 08 Nov 2022 00:30:01 +0000</pubDate>
      <link>https://dev.to/martinezhenry/interacting-with-a-kubernetes-cluster-kind-3hg7</link>
      <guid>https://dev.to/martinezhenry/interacting-with-a-kubernetes-cluster-kind-3hg7</guid>
      <description>&lt;p&gt;In the previous article, we told how to install kind y how to create a cluster with this tool (you can see it here &lt;a href="https://codeiscomming.hashnode.dev/kubernetes-cluster-with-kind"&gt;Kubernetes Cluster with Kind&lt;/a&gt;), now we will talk about how we can Interact with a Kubernetes Cluster using Kind.&lt;/p&gt;

&lt;h1&gt;
  
  
  Create a Cluster
&lt;/h1&gt;

&lt;p&gt;To create a cluster with Kind we can use the command &lt;code&gt;create cluster&lt;/code&gt;, by default kind create a cluster called kind (If you do not use &lt;code&gt;--name&lt;/code&gt; parameter)&lt;/p&gt;

&lt;p&gt;For example, to create a cluster with a default name and a cluster with a custom name we can use the next commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kind create cluster # Default cluster context name is `kind`.
# creating a custom name cluster
kind create cluster --name kind-2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we will see something like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kind create cluster
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.21.1) 🖼
 ✓ Preparing nodes 📦  
 ✓ Writing configuration 📜 
 ✓ Starting control-plane 🕹️ 
 ✓ Installing CNI 🔌 
 ✓ Installing StorageClass 💾 
Set kubectl context to "kind-kind"
You can now use your cluster with:

kubectl cluster-info --context kind-kind

Have a nice day! 👋
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kind create cluster --name kind-2
Creating cluster "kind-2" ...
 ✓ Ensuring node image (kindest/node:v1.21.1) 🖼
 ✓ Preparing nodes 📦  
 ✓ Writing configuration 📜 
 ✓ Starting control-plane 🕹️ 
 ✓ Installing CNI 🔌 
 ✓ Installing StorageClass 💾 
Set kubectl context to "kind-kind-2"
You can now use your cluster with:

kubectl cluster-info --context kind-kind-2

Not sure what to do next? 😅  Check out https://kind.sigs.k8s.io/docs/user/quick-start/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Listing our Clusters
&lt;/h1&gt;

&lt;p&gt;Using the command &lt;code&gt;get clusters&lt;/code&gt; we can see the name of the clusters we have.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kind get clusters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we obtain something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kind get clusters                  
kind
kind-2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Loading an Image Into a Cluster
&lt;/h1&gt;

&lt;p&gt;With Kind also we can load our docker images into our cluster nodes, to do that we can use the command &lt;code&gt;load docker-image&lt;/code&gt;, previous to use this command we need to have the docker image.&lt;/p&gt;

&lt;p&gt;we can use &lt;code&gt;docker build&lt;/code&gt; command to create our image, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t my-image:my-tag .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can load our image into a cluster&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kind load docker-image my-image:my-tag
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we will see this result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kind load docker-image lab-1:latest
Image: "lab-1:latest" with ID "sha256:019302746a926ab8fd93400a0be0ea4ffc5e9e6a6f6b645258f96391fce7995b" not yet present on node "kind-control-plane", loading...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also kind allows loading docker images with a file using &lt;code&gt;kind load image-archive my-image-file.tar&lt;/code&gt; command, the result is like `docker-image&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: we can use &lt;code&gt;load&lt;/code&gt; command with the param &lt;code&gt;--name&lt;/code&gt; to specify a cluster, by default Kind uses the cluster called kind if we do not specify a name&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Deleting a Cluster
&lt;/h1&gt;

&lt;p&gt;To delete a cluster with Kind we can use the command &lt;code&gt;delete cluster&lt;/code&gt;, this command also uses the parameter &lt;code&gt;--name&lt;/code&gt; to indicate a specific cluster's name (by default Kind uses the name 'kind').&lt;/p&gt;

&lt;p&gt;For example, we can delete the two clusters previously created with these commands:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
kind delete cluster&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
kind delete cluster --name kind-2&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;if everything is right, we will see something like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
$ kind delete cluster       &lt;br&gt;
Deleting cluster "kind" ...&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
$ kind delete cluster --name kind-2&lt;br&gt;
Deleting cluster "kind-2" ...&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conslusion
&lt;/h1&gt;

&lt;p&gt;In this post, we saw some commands to interact with our cluster create with Kind, existing other commands, and more parameters that we can use but we focussed on the basics.&lt;/p&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt; &lt;a href="https://kind.sigs.k8s.io/docs/user/quick-start/#interacting-with-your-cluster"&gt;Interacting With Your Cluster - Kind Docs&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>kubernetes</category>
      <category>kind</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Kubernetes Cluster with Kind</title>
      <dc:creator>martinezhenry</dc:creator>
      <pubDate>Sun, 06 Nov 2022 23:59:06 +0000</pubDate>
      <link>https://dev.to/martinezhenry/kubernetes-cluster-with-kind-4kaa</link>
      <guid>https://dev.to/martinezhenry/kubernetes-cluster-with-kind-4kaa</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article, I  will show you how you can create a Kubernetes (k8s) cluster using the tool Kind. Kind is a tool that uses docker to create the cluster nodes that may be used for local development or CI, to know more about it, you can go to the &lt;a href="https://kind.sigs.k8s.io/"&gt;Kind official page&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Environment
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Linux Mint 20.2&lt;/li&gt;
&lt;li&gt;Docker version 20.10.12, build e91ed57&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Install
&lt;/h2&gt;

&lt;p&gt;To use Kind, we need to have installed Docker,  &lt;a href="https://docs.docker.com/engine/install/"&gt;How to install Docker&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After installing Docker or if already you had installed it, you need to check if the docker daemon is installed and running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker info
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can install Kind using the steps in the &lt;a href="https://kind.sigs.k8s.io/docs/user/quick-start#installation"&gt;Kind official docs&lt;/a&gt; in the install section.&lt;/p&gt;

&lt;p&gt;This my case, I used the steps for Homebrew:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install kind
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, we can check the Kind installation with this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kind version                                           
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see something like this &lt;code&gt;kind v0.11.1 go1.16.4 linux/amd64&lt;/code&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a K8s Cluster
&lt;/h2&gt;

&lt;p&gt;To create a k8s cluster with kind with a basic configuration you can execute this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kind create cluster
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kind create cluster 
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.21.1) 🖼
 ✓ Preparing nodes 📦  
 ✓ Writing configuration 📜 
 ✓ Starting control-plane 🕹️ 
 ✓ Installing CNI 🔌 
 ✓ Installing StorageClass 💾 
Set kubectl context to "kind-kind"
You can now use your cluster with:

kubectl cluster-info --context kind-kind

Have a nice day! 👋
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is all, you created a k8s cluster with basic kind config, to check your cluster you can use&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;kind get clusters&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;to see your cluster name and&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;kind get nodes&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;to list nodes' names.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kind get clusters
kind

$ kind get nodes     
kind-control-plane
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can use &lt;code&gt;kubectl&lt;/code&gt; command to interact with your cluster. To see how to install kubectl please see this documentation &lt;a href="https://kubernetes.io/docs/tasks/tools/#kubectl"&gt;install kubectl&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://kubernetes.io/docs/tasks/tools/#kubectl"&gt;K8s tool kubectl&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://kubernetes.io/docs/tasks/tools/#kind"&gt;K8s tool kind&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.docker.com/engine/install/"&gt;Docker install&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>kubernetes</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
