<?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: ven</title>
    <description>The latest articles on DEV Community by ven (@venkatsgithub1).</description>
    <link>https://dev.to/venkatsgithub1</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%2F397659%2F6c945701-8381-408b-8f68-227e98d94b20.jpeg</url>
      <title>DEV Community: ven</title>
      <link>https://dev.to/venkatsgithub1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/venkatsgithub1"/>
    <language>en</language>
    <item>
      <title>Http2 setup for Spring Boot, Angular with nginx (containerized)</title>
      <dc:creator>ven</dc:creator>
      <pubDate>Tue, 22 Mar 2022 18:51:23 +0000</pubDate>
      <link>https://dev.to/venkatsgithub1/http2-setup-for-spring-boot-angular-with-nginx-containerized-3lh2</link>
      <guid>https://dev.to/venkatsgithub1/http2-setup-for-spring-boot-angular-with-nginx-containerized-3lh2</guid>
      <description>&lt;h6&gt;
  
  
  This is my first post
&lt;/h6&gt;

&lt;p&gt;&lt;strong&gt;This demo is just to scratch the surface with Http2 using Spring.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You can find code at &lt;a href="https://github.com/venkatsgithub1/http2-with-spring-angular"&gt;Github&lt;/a&gt;&lt;br&gt;
Kindly follow the code to understand the API requests in Spring and Angular.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To enable Http2 with Spring, we need SSL enabled on Spring.&lt;br&gt;
With nginx, we need to add which port is needed to be used with Http2.&lt;/p&gt;

&lt;p&gt;I used docker compose to easily startup the entire application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Here is what I tried and worked:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  To enable http2 in Spring, we need to enable https first.
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Create a self signed certificate, since this is a demo&lt;/li&gt;
&lt;li&gt;Use following properties in application.properties for ssl: 

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;server.ssl.key-store-type=PKCS12&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;server.ssl.key-store=/etc/ssl/certs/https2_test.p12&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;server.ssl.key-store-password=https2_test&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;server.ssl.key-alias=https2_test&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;server.ssl.enabled=true&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Enable http2 using the following:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;server.http2.enabled=true&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;I am using 8443 as embedded tomcat server port.&lt;/li&gt;
&lt;li&gt;Application code is self explanatory, has two endpoints.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  For UI, Angular is used and code is being deployed into nginx.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;UI code is simple, and has two API calls in ngOnInit.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Docker compose is used to start up both backend and frontend.
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Backend dockerfile steps:

&lt;ul&gt;
&lt;li&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jti4YZtY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdqto58q4extjm19j4wp.png" alt="Backend Dockerfile" width="880" height="382"&gt;&lt;/li&gt;
&lt;li&gt;Dockerfile on the backend side is straightforward:&lt;/li&gt;
&lt;li&gt;It uses maven java image as builder&lt;/li&gt;
&lt;li&gt;Copies code into workdir&lt;/li&gt;
&lt;li&gt;Runs mvn clean install&lt;/li&gt;
&lt;li&gt;Uses jdk14 image, copies jar, certificates there&lt;/li&gt;
&lt;li&gt;Importing certificate to cacerts&lt;/li&gt;
&lt;li&gt;Finally run jar.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Frontend dockerfile steps:

&lt;ul&gt;
&lt;li&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3tiuLJZw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0fob51j6llghssmstwc.png" alt="Image description" width="586" height="367"&gt;&lt;/li&gt;
&lt;li&gt;Uses node lts image as builder&lt;/li&gt;
&lt;li&gt;Copy all code&lt;/li&gt;
&lt;li&gt;Run npm install and build&lt;/li&gt;
&lt;li&gt;Use nginx alpine image to copy build files&lt;/li&gt;
&lt;li&gt;I created a custom default.conf and copied it to &lt;code&gt;/etc/nginx/conf.d/&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;The custom default.conf has settings related to http2 and ssl certs&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Add certs into nginx locations:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;/etc/ssl/private/&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;/etc/ssl/certs/&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Start nginx with daemon off.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Changes to default.conf of nginx
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Add following below default 80 port localhost:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;listen 443 ssl http2 default_server;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;listen [::]:443 ssl http2 default_server;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;These steps are saying, here are my certs, use 443 for http2 with ssl.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Docker compose
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6KZZr_Q5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1bno2yg10e7efvwvdvsm.png" alt="Docker compose yml" width="397" height="270"&gt;&lt;/li&gt;
&lt;li&gt;Compose is pretty simple, it has steps in yml to build backend and frontend:&lt;/li&gt;
&lt;li&gt;Backend:

&lt;ul&gt;
&lt;li&gt;sbh2 is spring boot http2 service, we just have container name here and build from current path.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Frontend:

&lt;ul&gt;
&lt;li&gt;fengh2 is frontend nginx http2 service, it exposes 80 and 443 on 8080 and 8443 respectively, build location and container name.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;To build and start, we use: &lt;code&gt;docker compose up&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Demonstration
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yRIO_qkq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/venkatsgithub1/http2-with-spring-angular/blob/master/images/requests1.png%3Fraw%3Dtrue" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yRIO_qkq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/venkatsgithub1/http2-with-spring-angular/blob/master/images/requests1.png%3Fraw%3Dtrue" alt="Requests with Network" width="880" height="442"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;In the network tab, we see that the hello and random-quote API calls are showing protocol h2 indicating Http2.&lt;/strong&gt;&lt;br&gt;
  &lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--90MHIsLB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/venkatsgithub1/http2-with-spring-angular/blob/master/images/requests2.png%3Fraw%3Dtrue" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--90MHIsLB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://github.com/venkatsgithub1/http2-with-spring-angular/blob/master/images/requests2.png%3Fraw%3Dtrue" alt="Console" width="880" height="578"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>spring</category>
      <category>http2</category>
      <category>webdev</category>
      <category>github</category>
    </item>
  </channel>
</rss>
