<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dapr &#8211; achraf ben alaya</title>
	<atom:link href="https://achrafbenalaya.com/category/dapr/feed/" rel="self" type="application/rss+xml" />
	<link>https://achrafbenalaya.com</link>
	<description>Tech Blog By Achraf Ben Alaya</description>
	<lastBuildDate>Tue, 17 Aug 2021 20:02:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.5</generator>

<image>
	<url>/wp-content/uploads/2022/02/cropped-me-scaled-1-32x32.jpeg</url>
	<title>Dapr &#8211; achraf ben alaya</title>
	<link>https://achrafbenalaya.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">189072172</site>	<item>
		<title>Dapr &#8211; State management (redis)  Part 1/2</title>
		<link>https://achrafbenalaya.com/2021/08/13/dapr-state-management-redis-part-1-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dapr-state-management-redis-part-1-2</link>
					<comments>https://achrafbenalaya.com/2021/08/13/dapr-state-management-redis-part-1-2/#respond</comments>
		
		<dc:creator><![CDATA[achraf]]></dc:creator>
		<pubDate>Fri, 13 Aug 2021 10:13:30 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Dapr]]></category>
		<guid isPermaLink="false">https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/?p=1321</guid>

					<description><![CDATA[This part belong to a serie of articles about dapr : Introduction to dapr Service Invocation Publish-Subscribe pub/sub State management Hosting using AKS (Azure Kubernetes Services) Distributed applications are composed of independent services. While each service should be stateless, some services must track state to complete business operations. Consider a shopping basket service for an [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>This part belong to a serie of articles about dapr :</p>
<ol>
<li><a href="https://bit.ly/3Cw8dDK"><strong>Introduction to dapr</strong></a></li>
<li><a href="https://bit.ly/3yATtkz"><strong>Service Invocation</strong></a></li>
<li><strong>Publish-Subscribe pub/sub</strong></li>
<li><strong>State management</strong></li>
<li><strong>Hosting using AKS (Azure Kubernetes Services)</strong></li>
</ol>
<p>Distributed applications are composed of independent services. While each service should be stateless, some services must track state to complete business operations. Consider a shopping basket service for an e-commerce site. If the service can’t track state, the customer could loose the shopping basket content by leaving the website, resulting in a lost sale and an unhappy customer experience. For these scenarios, the state needs to be persisted to a distributed state store.</p>
<p>For that, we are going to add to our sample an example of a state store first using Redis .<br />
<em><strong>Note</strong>: we are not going through best practices for creating a basket store for our website, we are just going to see how to use the state management.</em></p>
<p>in our last scenario, we have used the Service Invocation so our front app calls our API and so like that we get a list of cookies, now we are going to add an &#8220;Add&#8221; button in front of each cookie, and when the user clique on it, the ID of the cookie will be added to the basket (if you want to work on on the project you are welcome to contribute and add awesome stuff to it ).</p>
<p>First, let&#8217;s see what we are going to have :</p>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/1-1.png"><img fetchpriority="high" decoding="async" class="aligncenter size-full wp-image-1324" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/1-1.png" alt="" width="1190" height="651" srcset="/wp-content/uploads/2021/08/1-1.png 1190w, /wp-content/uploads/2021/08/1-1-300x164.png 300w, /wp-content/uploads/2021/08/1-1-1024x560.png 1024w, /wp-content/uploads/2021/08/1-1-768x420.png 768w, /wp-content/uploads/2021/08/1-1-750x410.png 750w, /wp-content/uploads/2021/08/1-1-1140x624.png 1140w" sizes="(max-width: 1190px) 100vw, 1190px" /></a> <a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/2-1.png"><img decoding="async" class="aligncenter size-full wp-image-1325" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/2-1.png" alt="" width="891" height="239" srcset="/wp-content/uploads/2021/08/2-1.png 891w, /wp-content/uploads/2021/08/2-1-300x80.png 300w, /wp-content/uploads/2021/08/2-1-768x206.png 768w, /wp-content/uploads/2021/08/2-1-750x201.png 750w" sizes="(max-width: 891px) 100vw, 891px" /></a></p>
<p>You can download the source code from this link :</p>
<p>Now, how we work with state management.</p>
<h1>How it works</h1>
<p>First of all, and in order to work with the state we have to add a components folder that will contain few files that we are going to work with from state management to pub/sub and cron jobs.</p>
<p>Now , we need to create a yaml file : <strong>statestore.yaml .</strong></p>
<p>ps: you can find the file when you install daper under  :  user\.dapr\components</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: redisstore
spec:
  type: state.redis
  metadata:
  - name: redisHost
    value: localhost:6379
  - name: redisPassword
    value: ""
   
</pre>
<p>&nbsp;</p>
<p>The type of database is determined by the <code>type</code> field, and things like connection strings and other metadata are put in the <code>.metadata</code> section. Even though metadata values can contain secrets in plain text, it is recommended you use a <a href="https://docs.dapr.io/operations/components/component-secrets/">secret store</a>.<br />
Here our store name is redisstore and we are going to use it later in our code sample.</p>
<h1>Using the Dapr .NET SDK</h1>
<p>Well as you may know it&#8217;s a combination of key/value ,this is how we store data here :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">//saving a key of amount with data 5
await daprClient.SaveStateAsync&lt;int&gt; ("redisstore","amount",5);

//getting the value using the key "amount"
var amounttotal=await daprClient.GetStateAsync&lt;int&gt; ("redisstore","amount");</pre>
<p>As you can see in the above sample, we are saving a combination of amount/5 and we are getting the value using the GetStateAsync method.</p>
<p>In our cookies sample, we want to save a list and its key is going to be CookiesList</p>
<pre class="EnlighterJSRAW" data-enlighter-language="csharp">var orderState = await _daprClient.
                    GetStateEntryAsync&lt;List&lt;Item&gt;&gt;("redisstore", "CookiesList");</pre>
<p>in order to get to the data from the store we should use the: <em>GetStateEntryAsync, </em>passing as parameters the name of the store that we early have configured in the <strong>statestore.yaml , </strong>also we pass the key and in our case is CookiesList.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">public async Task&lt;IActionResult&gt; AdddataAsync(Guid ID)
{
    Item _tem = new Item();
   
 
   //getting the state
    var orderState = await _daprClient.
            GetStateEntryAsync&lt;List&lt;Item&gt;&gt;("redisstore", "CookiesList");

    List&lt;Item&gt; CookiesList = new();
    if (orderState.Value == null)
    {
        _logger.LogInformation("CookiesList   Case not exist ");
        CookiesList.Add(_tem);
        await _daprClient.SaveStateAsync("redisstore", "CookiesList", CookiesList);
    }
    else
    {
        _logger.LogInformation("OrderState  Case exist ");
        CookiesList = orderState.Value;
        CookiesList.Add(_tem);
        await _daprClient.SaveStateAsync("redisstore", "CookiesList", CookiesList);
    }

    ViewData["OrderId"] = ID;
    return View("Thanks");
}</pre>
<p>in our sample here, we are receiving an ID from the page and creating a list of items that will contain the list of ID&#8217;s , in case the CookiesList exists we are adding the item to it else we are going to create it using the first method.</p>
<p>Now like that have made sure everything is alright, how can we explore or see if the store has been created and how to retrieve the value using the key.</p>
<h1>Exploring Redis</h1>
<p>Well, there are many ways to explore the Redis store here that we have created and we are going to see a couple of them.</p>
<h2>Using Docker :</h2>
<p>using this sample docker command we can get into the store :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">Docker run --rm -it --link dapr_redis redis redis-cli -h dapr_redis</pre>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/3-2.png"><img decoding="async" class="aligncenter size-full wp-image-1329" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/3-2.png" alt="" width="1031" height="386" srcset="/wp-content/uploads/2021/08/3-2.png 1031w, /wp-content/uploads/2021/08/3-2-300x112.png 300w, /wp-content/uploads/2021/08/3-2-1024x383.png 1024w, /wp-content/uploads/2021/08/3-2-768x288.png 768w, /wp-content/uploads/2021/08/3-2-750x281.png 750w" sizes="(max-width: 1031px) 100vw, 1031px" /></a></p>
<p>Now as we are inside the store, how can we see our keys/values?</p>
<p>simple using this command :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">keys *</pre>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/4-1.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1330" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/4-1.png" alt="" width="930" height="331" srcset="/wp-content/uploads/2021/08/4-1.png 930w, /wp-content/uploads/2021/08/4-1-300x107.png 300w, /wp-content/uploads/2021/08/4-1-768x273.png 768w, /wp-content/uploads/2021/08/4-1-750x267.png 750w" sizes="(max-width: 930px) 100vw, 930px" /></a></p>
<p>As you can see, we can see our cookies list here,now in order to explore it we need to use the following command :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">HGET "cookiesclient||CookiesList" data</pre>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/5-1.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1331" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/5-1.png" alt="" width="1080" height="400" srcset="/wp-content/uploads/2021/08/5-1.png 1080w, /wp-content/uploads/2021/08/5-1-300x111.png 300w, /wp-content/uploads/2021/08/5-1-1024x379.png 1024w, /wp-content/uploads/2021/08/5-1-768x284.png 768w, /wp-content/uploads/2021/08/5-1-750x278.png 750w" sizes="(max-width: 1080px) 100vw, 1080px" /></a>In order to <strong>delete</strong> key/value we should use :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">del key "cookiesclient||CookiesList"</pre>
<h3>Using Visual studio code  :</h3>
<p>using visual studio code too we can explore the data after installing the Redis explorer plugin:</p>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/6-1.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1332" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/6-1.png" alt="" width="1304" height="458" srcset="/wp-content/uploads/2021/08/6-1.png 1304w, /wp-content/uploads/2021/08/6-1-300x105.png 300w, /wp-content/uploads/2021/08/6-1-1024x360.png 1024w, /wp-content/uploads/2021/08/6-1-768x270.png 768w, /wp-content/uploads/2021/08/6-1-750x263.png 750w, /wp-content/uploads/2021/08/6-1-1140x400.png 1140w" sizes="(max-width: 1304px) 100vw, 1304px" /></a></p>
<p>Also, there is other methods that we can use you can found them right here: https://docs.dapr.io/developing-applications/building-blocks/state-management/howto-get-save-state/</p>
<h1>Retrieving the Data using the Dapr SDK</h1>
<p>Now after we have saved our data to the store ,when we go to basket page we need to see the list of GUID that we have saved inside our list in order to get that list we should use :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">var orderState = await _daprClient.
                                GetStateEntryAsync&lt;List&lt;Item&gt;&gt;("redisstore", "CookiesList");</pre>
<p>now if the order state.Value.Count&gt;0  that means we have data inside it and we can get it  like that and passe it to our view :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">var orderList = orderState.Value;</pre>
<p>Now in this part we have seen how to use the Save and get state using Redis , in the the Part 2 we are going to see how to use the <strong>Azure Cosmos DB as a backend state store .</strong></p>
<p>&nbsp;</p>
<p>Source code : https://bit.ly/3s5pz5C</p>
<h1></h1>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://achrafbenalaya.com/2021/08/13/dapr-state-management-redis-part-1-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1321</post-id>	</item>
		<item>
		<title>Dapr &#8211; Service Invocation Part 2/2</title>
		<link>https://achrafbenalaya.com/2021/08/10/dapr-service-invocation-part-2-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dapr-service-invocation-part-2-2</link>
					<comments>https://achrafbenalaya.com/2021/08/10/dapr-service-invocation-part-2-2/#respond</comments>
		
		<dc:creator><![CDATA[achraf]]></dc:creator>
		<pubDate>Tue, 10 Aug 2021 20:32:46 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Dapr]]></category>
		<guid isPermaLink="false">https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/?p=1311</guid>

					<description><![CDATA[This part belong to a serie of articles about dapr : Introduction to dapr Service Invocation Publish-Subscribe pub/sub State management Hosting using AKS (Azure Kubernetes Services) In the previous part, we have created our API and we have seen how to use dapr to expose it, also we have seen how to use the dapr [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>This part belong to a serie of articles about dapr :</p>
<ol>
<li><a href="https://bit.ly/3Cw8dDK"><strong>Introduction to dapr</strong></a></li>
<li><a href="https://bit.ly/3yATtkz"><strong>Service Invocation</strong></a></li>
<li><strong>Publish-Subscribe pub/sub</strong></li>
<li><strong>State management</strong></li>
<li><strong>Hosting using AKS (Azure Kubernetes Services)</strong></li>
</ol>
<p>In the previous part, we have created our API and we have seen how to use dapr to expose it, also we have seen how to use the dapr dashboard and also to use zipkin as  <em><strong>tracing system .</strong></em></p>
<p>In this part, we are going to create an MVC project as a Front-end for our solution.</p>
<p>For that we are going to see step by step from adding dapr nuget package to consuming the service .</p>
<h1>Creating the MVC Project</h1>
<p>First of all we are going to add a new MVC project to the solution as the follow image below :</p>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/1.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1313" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/1.png" alt="" width="1025" height="660" srcset="/wp-content/uploads/2021/08/1.png 1025w, /wp-content/uploads/2021/08/1-300x193.png 300w, /wp-content/uploads/2021/08/1-768x495.png 768w, /wp-content/uploads/2021/08/1-750x483.png 750w" sizes="(max-width: 1025px) 100vw, 1025px" /></a></p>
<p>next we are going to create the same model that we have in our API inside Models folder .</p>
<p>&nbsp;</p>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/2.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1314" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/2.png" alt="" width="344" height="513" srcset="/wp-content/uploads/2021/08/2.png 344w, /wp-content/uploads/2021/08/2-201x300.png 201w" sizes="(max-width: 344px) 100vw, 344px" /></a>Next thing and before doing anything we need to install our DAPR NUGET PACKAGE :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">Install-Package Dapr.AspNetCore -Version 1.3.0</pre>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/3-1.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1315" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/3-1.png" alt="" width="979" height="422" srcset="/wp-content/uploads/2021/08/3-1.png 979w, /wp-content/uploads/2021/08/3-1-300x129.png 300w, /wp-content/uploads/2021/08/3-1-768x331.png 768w, /wp-content/uploads/2021/08/3-1-750x323.png 750w" sizes="(max-width: 979px) 100vw, 979px" /></a></p>
<p>now we need to configure <strong>ConfigureServices </strong>by adding dapr<strong> :</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">services.AddControllersWithViews().AddDapr();
</pre>
<p>Also inside the <strong>Configure</strong> method we need to add :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">app.UseCloudEvents();</pre>
<p>CloudEvents receive every incoming request with the content type of “application/cloudevents+json” .</p>
<p>Now we can go on and start creating our methods.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">public interface IBroRepository 
   {
       Task&lt;IEnumerable&lt;Cookies&gt;&gt; Getcookies();

   }</pre>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">public class BroRepository : IBroRepository
  {
      private readonly HttpClient _httpClient;

      public BroRepository(HttpClient httpClient)
      {
          _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
      }

      public async Task&lt;IEnumerable&lt;Cookies&gt;&gt; Getcookies()
      {
          return await _httpClient.
                GetFromJsonAsync&lt;IEnumerable&lt;Cookies&gt;&gt;("/api/cookies");
      }
  }</pre>
<p>As you can see inside the BroRepository we are calling the cookies API from the Getcookies() .</p>
<p>Now we need to register our service by adding a singleton and as you can see we are invoking an HTTP client that is calling the API using the app ID that we have created while running the service using dapr .</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">services.AddSingleton&lt;IBroRepository, BroRepository&gt;(_ =&gt; new BroRepository(DaprClient.CreateInvokeHttpClient("cookiesstoreapi")));
</pre>
<p>Now we can create our controller :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">public class HomeController : Controller
  {
      private readonly ILogger&lt;HomeController&gt; _logger;
      private readonly IBroRepository _IBroRepository;

      public HomeController(ILogger&lt;HomeController&gt; logger, IBroRepository IBroRepository)
      {
          _logger = logger;
          _IBroRepository = IBroRepository;
      }

      public async Task&lt;IActionResult&gt; Index()
      {
          //logging info
          _logger.LogInformation("Entered Index method");
          var data = await _IBroRepository.Getcookies();
          //logging info
          _logger.LogInformation($"Returning data {JsonSerializer.Serialize(data)}");
          return View(data);
      }

 

  
  }</pre>
<p>Next we add our view page :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">@model IEnumerable&lt;Cookies&gt;
@{
    ViewData["Title"] = "Home Page";
}

&lt;div class="text-center"&gt;
    &lt;h1 class="display-4"&gt;Welcome&lt;/h1&gt;
    &lt;p&gt;Learn about &lt;a href="https://docs.microsoft.com/aspnet/core"&gt;building Web apps with ASP.NET Core and Dapr&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;

&lt;table class="table"&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;GUID&lt;/th&gt;
            &lt;th&gt;Name&lt;/th&gt;
            &lt;th&gt;Date (C)&lt;/th&gt;
            &lt;th&gt;Picture&lt;/th&gt;
            
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        @foreach (var cookie in @Model)
        {
        &lt;tr&gt;
            &lt;td&gt;@cookie.ID&lt;/td&gt;
            &lt;td&gt;@cookie.Name&lt;/td&gt;
            &lt;td&gt;@cookie.Date&lt;/td&gt;
            &lt;td&gt;
                &lt;div style="margin: 0 auto; width: 90px"&gt;
                    &lt;img src=@cookie.ImageUrl     alt="me" style="width: 90px" /&gt;
                &lt;/div&gt;

            &lt;/td&gt;

        &lt;/tr&gt;
        }
    &lt;/tbody&gt;
&lt;/table&gt;</pre>
<p class="x-hidden-focus">Next, we start the ASP.NET Core API , listening on HTTP port 5001, and dapr on port 50001,also we start the ASP.NET Core Client, listening on HTTP port 5002, and dapr on port 50002:</p>
<p>Ps: <em>make sure Docker is up and dapr containers are running.</em></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">dapr run --app-id cookiesstoreapi --app-port 5001  --dapr-http-port 50001 dotnet run

dapr run --app-id CookiesClient --app-port 5002  --dapr-http-port 50002 dotnet run</pre>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/4.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1316" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/4.png" alt="" width="1366" height="768" srcset="/wp-content/uploads/2021/08/4.png 1366w, /wp-content/uploads/2021/08/4-300x169.png 300w, /wp-content/uploads/2021/08/4-1024x576.png 1024w, /wp-content/uploads/2021/08/4-768x432.png 768w, /wp-content/uploads/2021/08/4-750x422.png 750w, /wp-content/uploads/2021/08/4-1140x641.png 1140w" sizes="(max-width: 1366px) 100vw, 1366px" /></a></p>
<p>As you can see by visiting the http://localhost:5002/ we can see our website up and running!</p>
<p>Dapr also supports the following features through HTTP/gRPC to implement the sidecar pattern:</p>
<ul>
<li>State management.</li>
<li>Publish and subscribe.</li>
<li>Resource bindings &amp; triggers.</li>
<li>Actor runtime.</li>
<li>Distributed tracing.</li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://achrafbenalaya.com/2021/08/10/dapr-service-invocation-part-2-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1311</post-id>	</item>
		<item>
		<title>Dapr &#8211; Service Invocation Part 1/2</title>
		<link>https://achrafbenalaya.com/2021/08/09/dapr-service-invocation-part-1-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dapr-service-invocation-part-1-2</link>
					<comments>https://achrafbenalaya.com/2021/08/09/dapr-service-invocation-part-1-2/#respond</comments>
		
		<dc:creator><![CDATA[achraf]]></dc:creator>
		<pubDate>Mon, 09 Aug 2021 14:15:34 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Dapr]]></category>
		<category><![CDATA[dapr]]></category>
		<guid isPermaLink="false">https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/?p=1297</guid>

					<description><![CDATA[This part belong to a serie of articles about dapr : Introduction to dapr Service Invocation Publish-Subscribe pub/sub State management Hosting using AKS (Azure Kubernetes Services) In the previous part, we have introduced dapr  ,now we are going to start creating our services one by one for that and as a start  what we need [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>This part belong to a serie of articles about dapr :</p>
<ol>
<li><a href="https://bit.ly/3Cw8dDK"><strong>Introduction to dapr</strong></a></li>
<li><strong>Service Invocation</strong></li>
<li><strong>Publish-Subscribe pub/sub</strong></li>
<li><strong>State management</strong></li>
<li><strong>Hosting using AKS (Azure Kubernetes Services)</strong></li>
</ol>
<p>In the previous part, we have <a href="https://bit.ly/3Cw8dDK">introduced dapr  ,</a>now we are going to start creating our services one by one for that and as a start  what we need is :</p>
<ol>
<li>Make sure Docker is running and Dapr services too .</li>
<li>Have .Net  5 Insatlled</li>
<li style="text-align: left;">Have visual studio installed .</li>
</ol>
<h1>Creating the API :</h1>
<p>First of all we are going to create and empty solution and name it as you want to , and than we are need to add a : <strong>ASP .NET WEB API project</strong> to the solution  or you can just type</p>
<pre class="EnlighterJSRAW" data-enlighter-language="csharp">dotnet new webapi</pre>
<p>The full solution can be found at this link :  https://bit.ly/3s5pz5C</p>
<p>Now, you may run the solution by running the following command :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">dotnet run</pre>
<p>after that when we go to visit: http://localhost:3902/api/cookies we will get as return a list of cookies</p>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/3.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1299" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/3.png" alt="" width="917" height="589" srcset="/wp-content/uploads/2021/08/3.png 917w, /wp-content/uploads/2021/08/3-300x193.png 300w, /wp-content/uploads/2021/08/3-768x493.png 768w, /wp-content/uploads/2021/08/3-750x482.png 750w" sizes="(max-width: 917px) 100vw, 917px" /></a></p>
<p>&nbsp;</p>
<h1>Runing the API :</h1>
<p>What we really want to do is to run the solution using Dapr for that and in order to expose the cookies api using the dapr sidecar we need to run the command :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="powershell">dapr run --app-id cookiesstoreapi --app-port 5001  --dapr-http-port 50001 dotnet run</pre>
<p>This may be a bit strange for you so let&#8217;s explain all the parts of this command :</p>
<ul>
<li><strong>&#8211;app-id</strong> : each app/service must have a unique ID .</li>
<li><strong>&#8211;app-port :  </strong>in launch settings i changed the parameters so the app runs on port 5001 when I use dotnet run.</li>
<li><strong>&#8211;dapr-http-port</strong> : the port that dapr in listening to for this service.</li>
<li><strong>dotnet run</strong> : this is how we run dotnet applications</li>
</ul>
<p>&nbsp;</p>
<p>Now when we run this cmd we can still get our cookirs from<em> http://localhost:5001/api/cookies</em> but also now we can get them from the side car using : <em>http://localhost:50001/v1.0/invoke/cookiesstoreapi/method/api/cookies .</em></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">http://localhost:&lt;dapr-http-port&gt;/v1.0/invoke/&lt;app-id&gt;/method/&lt;method-name&gt;
</pre>
<p>&nbsp;</p>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/5.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1300" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/5.png" alt="" width="1352" height="676" srcset="/wp-content/uploads/2021/08/5.png 1352w, /wp-content/uploads/2021/08/5-300x150.png 300w, /wp-content/uploads/2021/08/5-1024x512.png 1024w, /wp-content/uploads/2021/08/5-768x384.png 768w, /wp-content/uploads/2021/08/5-360x180.png 360w, /wp-content/uploads/2021/08/5-750x375.png 750w, /wp-content/uploads/2021/08/5-1140x570.png 1140w" sizes="(max-width: 1352px) 100vw, 1352px" /></a><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/6.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1301" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/6.png" alt="" width="906" height="591" srcset="/wp-content/uploads/2021/08/6.png 906w, /wp-content/uploads/2021/08/6-300x196.png 300w, /wp-content/uploads/2021/08/6-768x501.png 768w, /wp-content/uploads/2021/08/6-750x489.png 750w" sizes="(max-width: 906px) 100vw, 906px" /></a></p>
<h1>Discovering the Services :</h1>
<p>using the follow command we can see the running services by dapr  :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">dapr dashboard</pre>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/7.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1302" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/7.png" alt="" width="1349" height="656" srcset="/wp-content/uploads/2021/08/7.png 1349w, /wp-content/uploads/2021/08/7-300x146.png 300w, /wp-content/uploads/2021/08/7-1024x498.png 1024w, /wp-content/uploads/2021/08/7-768x373.png 768w, /wp-content/uploads/2021/08/7-750x365.png 750w, /wp-content/uploads/2021/08/7-1140x554.png 1140w" sizes="(max-width: 1349px) 100vw, 1349px" /></a></p>
<p>On http://localhost:8080/ u will find dapr dashboard that will present to you the running services also the dapr components :</p>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/8.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1303" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/8.png" alt="" width="1366" height="488" srcset="/wp-content/uploads/2021/08/8.png 1366w, /wp-content/uploads/2021/08/8-300x107.png 300w, /wp-content/uploads/2021/08/8-1024x366.png 1024w, /wp-content/uploads/2021/08/8-768x274.png 768w, /wp-content/uploads/2021/08/8-750x268.png 750w, /wp-content/uploads/2021/08/8-1140x407.png 1140w" sizes="(max-width: 1366px) 100vw, 1366px" /></a></p>
<h1><strong>zipkin</strong></h1>
<p>When the dapr is working and by veryfing the runing containers, one of them is a Zipkin container, so what is zipkin ?</p>
<p><em>Zipkin is a distributed <strong>tracing system</strong>. It helps gather timing data needed to troubleshoot latency problems in service architectures. Features include both the collection and lookup of this data.</em></p>
<p>running: <strong>Docker ps</strong> will show us the following containers  :</p>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/9.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1304" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/9.png" alt="" width="1345" height="223" srcset="/wp-content/uploads/2021/08/9.png 1345w, /wp-content/uploads/2021/08/9-300x50.png 300w, /wp-content/uploads/2021/08/9-1024x170.png 1024w, /wp-content/uploads/2021/08/9-768x127.png 768w, /wp-content/uploads/2021/08/9-750x124.png 750w, /wp-content/uploads/2021/08/9-1140x189.png 1140w" sizes="(max-width: 1345px) 100vw, 1345px" /></a>as you can see zipkin is runing on port : 9411</p>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/10.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1305" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/10.png" alt="" width="1337" height="660" srcset="/wp-content/uploads/2021/08/10.png 1337w, /wp-content/uploads/2021/08/10-300x148.png 300w, /wp-content/uploads/2021/08/10-1024x505.png 1024w, /wp-content/uploads/2021/08/10-768x379.png 768w, /wp-content/uploads/2021/08/10-750x370.png 750w, /wp-content/uploads/2021/08/10-1140x563.png 1140w" sizes="(max-width: 1337px) 100vw, 1337px" /></a></p>
<p>&nbsp;</p>
<p>Zipkin let you trace your services and show you all the dependencies and we will see that in the next part .</p>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://achrafbenalaya.com/2021/08/09/dapr-service-invocation-part-1-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1297</post-id>	</item>
		<item>
		<title>Going Dapr</title>
		<link>https://achrafbenalaya.com/2021/08/06/going-dapr/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=going-dapr</link>
					<comments>https://achrafbenalaya.com/2021/08/06/going-dapr/#respond</comments>
		
		<dc:creator><![CDATA[achraf]]></dc:creator>
		<pubDate>Fri, 06 Aug 2021 14:38:04 +0000</pubDate>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Dapr]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[dapr]]></category>
		<guid isPermaLink="false">https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/?p=1289</guid>

					<description><![CDATA[Welcome to a new series of blog posts where we are going to explore dapr, for that we will see : Introduction to dapr Service Invocation Publish-Subscribe pub/sub State management Hosting using AKS (Azure Kubernetes Services) &#160; Introduction to dapr . Dapr is an open source, portable, event-driven runtime that makes it easy for developers [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Welcome to a new series of blog posts where we are going to explore dapr, for that we will see :</p>
<ol>
<li><strong>Introduction to dapr</strong></li>
<li><a href="https://bit.ly/3yATtkz"><strong>Service Invocation</strong></a></li>
<li><strong>Publish-Subscribe pub/sub</strong></li>
<li><strong>State management</strong></li>
<li><strong>Hosting using AKS (Azure Kubernetes Services)</strong></li>
</ol>
<p>&nbsp;</p>
<h1><strong>Introduction to dapr .<br />
</strong></h1>
<p>Dapr is an open source, portable, event-driven runtime that makes it easy for developers to build resilient, microservice stateless and stateful applications that run on the cloud and edge. Dapr embraces the diversity of all programming languages and developer frameworks and simplifies building applications such as the e-commerce example.</p>
<p>Dapr consists of a set of building blocks accessed by standard HTTP or gRPC APIs that can be called from any programming language. These building blocks empower all developers with proven, industry best practices and each building block is independent; you can use one, some, or all of them in your applications. In addition, through the open source project, we welcome the community to add new building blocks and contribute new components into existing ones. Dapr is completely platform agnostic, meaning you can run your applications locally, on any Kubernetes cluster, and other hosting environments that Dapr integrates with. This enables developers to build microservice applications that can run on both the cloud and edge with no code changes.</p>
<p>With dapr you can  build applications with any language and any framework by calling Dapr building blocks over standard APIs :</p>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/dapr-high-level.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1291" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/dapr-high-level.png" alt="" width="1222" height="577" srcset="/wp-content/uploads/2021/08/dapr-high-level.png 1222w, /wp-content/uploads/2021/08/dapr-high-level-300x142.png 300w, /wp-content/uploads/2021/08/dapr-high-level-1024x484.png 1024w, /wp-content/uploads/2021/08/dapr-high-level-768x363.png 768w, /wp-content/uploads/2021/08/dapr-high-level-750x354.png 750w, /wp-content/uploads/2021/08/dapr-high-level-1140x538.png 1140w" sizes="(max-width: 1222px) 100vw, 1222px" /></a></p>
<p>Portability does also extend beyond the hosting environment – while Dapr is an initiative started by Microsoft, it can also run on Kubernetes on-premise or in the cloud: Microsoft Azure, Amazon AWS, Google GCP, or any other cloud vendor&#8230;</p>
<h1>building blocks</h1>
<p>Dapr provides several building blocks that microservice application developers can adopt selectively, based on their needs:</p>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/building-blocks.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1293" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/building-blocks.png" alt="" width="758" height="781" srcset="/wp-content/uploads/2021/08/building-blocks.png 758w, /wp-content/uploads/2021/08/building-blocks-291x300.png 291w, /wp-content/uploads/2021/08/building-blocks-750x773.png 750w" sizes="(max-width: 758px) 100vw, 758px" /></a></p>
<ul>
<li><strong>Service invocation</strong>: Using service invocation, your application can reliably and securely communicate with other applications using the standard gRPC or HTTP protocols.</li>
<li><strong>State management:</strong> Using state management, your application can store data as key/value pairs in the supported state stores (we will see in a further blog post) .</li>
<li><strong>Publish &amp; subscribe messaging</strong>: The publish/subscribe pattern allows microservices to communicate with each other using messages. The producer or publisher sends messages to a topic without knowledge of what application will receive them. This involves writing them to an input channel. Similarly, a consumer or subscriber subscribes to the topic and receives its messages without any knowledge of what service produced these messages.</li>
<li><strong>Bindings</strong>: Using bindings, you can trigger your app with events coming in from external systems, or interface with external systems.</li>
<li><strong>Actors</strong>: The actor pattern describes actors as the lowest-level “unit of computation”. In other words, you write your code in a self-contained unit (called an actor) that receives messages and processes them one at a time, without any kind of concurrency or threading.</li>
<li><strong>Observability</strong>: Dapr enables the developer and operator to observe the behavior of the system services and applications without having to instrument them.</li>
<li><strong>Secrets management</strong>: It’s common for applications to store sensitive information such as connection strings, keys and tokens that are used to authenticate with databases, services and external systems in secrets by using a dedicated secret store.</li>
</ul>
<p>&nbsp;</p>
<h1>installing dapr :</h1>
<p>Well, before we go any further with dapr , we need first to install it and for so we need a couple fo things to do :</p>
<ul>
<li>Dapr requires <strong>Docker</strong> locally on your development environment, therefore make<br />
sure you have it installed : https://docs.docker.com/install</li>
<li>Installing <strong>Dapr CLI</strong> :The Dapr CLI allows you to setup Dapr on your local dev machine or on a Kubernetes cluster, provides debugging support, launches and manages Dapr instances.</li>
</ul>
<p>To install the Dapr CLI :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="powershell">powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"</pre>
<p>In order to verify that dapr have been installed we need to check the installation with this command:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">dapr</pre>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/dapr.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1294" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/dapr.png" alt="" width="1069" height="602" srcset="/wp-content/uploads/2021/08/dapr.png 1069w, /wp-content/uploads/2021/08/dapr-300x169.png 300w, /wp-content/uploads/2021/08/dapr-1024x577.png 1024w, /wp-content/uploads/2021/08/dapr-768x432.png 768w, /wp-content/uploads/2021/08/dapr-750x422.png 750w" sizes="(max-width: 1069px) 100vw, 1069px" /></a></p>
<p>Now that you have the Dapr CLI installed, it’s time to initialize Dapr on your local machine using the CLI.</p>
<p>Dapr runs as a sidecar alongside your application, and in self-hosted mode this means it is a process on your local machine. Therefore, initializing Dapr includes fetching the Dapr sidecar binaries and installing them locally.</p>
<p>make sure that docker is runing and open the terminal to run the follow cammand :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">dapr init</pre>
<p>Now you can verify the installed version of dapr using :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">dapr --version</pre>
<p>Now one more thing we need to check is the containers are runing well :</p>
<p><a href="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/daprsp.png"><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1295" src="https://achrafbenalaya-ekgvbjdjgta4b4hz.francecentral-01.azurewebsites.net/wp-content/uploads/2021/08/daprsp.png" alt="" width="1096" height="158" srcset="/wp-content/uploads/2021/08/daprsp.png 1096w, /wp-content/uploads/2021/08/daprsp-300x43.png 300w, /wp-content/uploads/2021/08/daprsp-1024x148.png 1024w, /wp-content/uploads/2021/08/daprsp-768x111.png 768w, /wp-content/uploads/2021/08/daprsp-750x108.png 750w" sizes="(max-width: 1096px) 100vw, 1096px" /></a>Now everything seems to be well-installed, the next thing we are going to do is to build a .Net application using the <strong>Service invocation Block.</strong></p>
<p>For that and in order to follow this series , you need to make sure you have .Net installed (we will be working with .Net 5.0 ) and you may need to install visual studio or visual studio code.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://achrafbenalaya.com/2021/08/06/going-dapr/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1289</post-id>	</item>
	</channel>
</rss>
