Spring Tutorial – Microservice with Spring Boot framework

Microservice with Spring Boot framework

Spring Tutorial – Microservice with Spring Boot framework

Pangkalahatang-ideya ng:

The concept of Microservices is being widely put into practice across enterprises. Compared to the monolithic IT systems, Microservices offer more benefits and help overcome the limitations of the monolithic systems, lalo na sa konteksto ng mga pang-modernong pangangailangan ng mga negosyo,,en,Ang mga modernong negosyo ay nais ng mas kaunting downtime,,en,maliksi pamamahala,,en,at kakayahang umangkop mula sa kanilang mga system,,en,Ang mahigpit na pagkabit ng serbisyo at mataas na pagkakaugnay sa mga serbisyo sa mga monolithic system ay napatunayan na hadlang sa pagkamit ng nais ng mga modernong negosyo,,en,pahintulutan ang mga serbisyo sa isang mas malaking system na maging sistematikong malaya at maluwag na kaisa upang ang mga negosyo ay may higit na kakayahang umangkop at liksi,,en,Tulad ng mga negosyo tandaan ang mga pakinabang ng mga mikrobyo,,en,mas maraming pagsisikap at mapagkukunan ang ginugugol upang makabuo ng mga bagong sumusuporta sa mga system o mag-upgrade ng mga mayroon nang mga produkto,,en,Ang Spring Boot ay naging isang naturang sistema na makakatulong sa mga negosyo na ipatupad ang mga mikropono,,en. Modern enterprises want lesser downtime, agile manageability, and flexibility from their systems. Tight service coupling and high interdependence across services in monolithic systems prove to be a barrier in achieving what modern enterprises want.

Microservices allow services in a greater system to be systemically independent and loosely coupled so that enterprises have greater flexibility and agility. As enterprises take note of the benefits of Microservices, more effort and resources are being expended to develop new supporting systems or upgrade existing products. The Spring Boot has been one such system which helps enterprises to implement the Microservices. Ginagamit ng Spring Boot ang lahat ng magagandang bagay tungkol sa Spring Framework na kinakailangan para sa pagpapaunlad ng Microservice at pinapasadya ang,,en,para sa pagpapaunlad ng Microservice,,en,Maaari mong sabihin na ang Spring Boot ay isang microframework na nagpapadali sa pagbuo ng mga Microservice sapagkat binabalot nito ang mga kinakailangang module mula sa superset upang mapili ng mga developer ang kinakailangang mga module nang hindi nalulula.,,en,kilala rin bilang ang,,en,Paglilingkod sa Mikroso,,en,s Arkitektura,,en,ay isang pangkat ng maluwag na kaisa na mga serbisyo ng software na bumubuo sa isang software suite,,en,Ang mga serbisyo ay kilala rin bilang mga module at maaari nilang pamahalaan ang isang module ng software nang nakapag-iisa,,en,Ang mga serbisyo ay maaaring,,en,at kadalasan ay,,en,iba sa isa't isa,,en,Ang mga serbisyo ay kailangang makipag-usap sa isa't isa at magagawa nila ito sa iba't ibang paraan,,en Spring framework for Microservice development. You can say that the Spring Boot is a microframework that makes building Microservices easier because it packages necessary modules from the superset so that developers can choose the necessary modules without getting overwhelmed.








Microservices – What is it?

Microservices, also known as the Microservices Architecture, are a group of loosely coupled software services that constitute a software suite. The services are also known as modules and they can manage a software module independently. The services can be, and usually are, different from one another. The services need to communicate with one another and they can do so in various ways. Ang paraan ng pagpili upang makipag-usap ay nakasalalay sa mga kinakailangan ng software,,en,ang ilan sa mga karaniwang protokol na ginamit ng mga developer ay HTTP / REST kasama si JSON o Protobuf,,en,Ang mga propesyonal sa DevOps ay maaaring gumamit ng anumang protokol na sa palagay nila ay angkop,,en,ang Paglipat ng Estado ng Representasyon,,en,Ang protokol ay itinuturing na kapaki-pakinabang dahil sa medyo mas mababang pagiging kumplikado,,en,Marahil ang pinakamahusay na paraan upang maunawaan ang mga mikrobyo at ang mga pakinabang nito upang ihambing sa mga monolithic software system,,en,Tinutugunan ng mga microservice ang ilang mga limitasyon ng mga monolithic system,,en,Ang mga serbisyo sa isang monolithic system ay mahigpit na isinama,,en,Dahil dun,,en,kung ang isa o higit pang mga serbisyo ay nai-update o naayos,,en,ang iba pang mga serbisyo ay naapektuhan din sapagkat ang lahat ng mga serbisyo ay dapat na muling magkumpuni at muling magamit,,en,Ang mga monolithic system ay mayroon ding isang solong run time system na nakakaapekto sa lahat ng mga serbisyo,,en,kinakailangan o hindi,,en. Still, some of the standard protocols used by the developers are HTTP/REST with JSON or Protobuf. DevOps professionals may use any protocol they think is suitable. Generally, the Representational State Transfer (REST) protocol is considered useful because of the comparatively lower complexity.

Probably the best way to understand Microservices and its benefits to compare with monolithic software systems. Microservices address certain limitations of monolithic systems. Services in a monolithic system are tightly coupled. Because of that, if one or more services are updated or fixed, the other services are also impacted because all services must be recompiled and redeployed. Monolithic systems also have a single run time system which impacts all services, required or not. So, Ang mga kawalan ng monolithic system ay hindi kinakailangang mga redeployment,,en,mahigpit na isinama ang mga serbisyo ay nag-aalok ng minimal o walang kakayahang umangkop,,en,kawalan ng husay at mataas na gastos,,en,Paghambingin ang mga sistemang monolitik sa Arkitektura ng mikroskopyo,,en,kung kailangan mong i-update o ayusin ang isang serbisyo,,en,ginagawa mo ito nang hindi nakakaapekto sa iba pang mga serbisyo,,en,Maaari mong,,en,Kailangang suriin ang komunikasyon protocol mag-post ng pag-update o pag-aayos,,en,dahil ang mga serbisyo ay maluwag na isinama,,en,ang kapalit ay madali nang hindi nakakaapekto sa system,,en,Tatlo,,en,palagi kang makakagamit ng mga serbisyo batay sa mga kinakailangan hindi alintana kung paano sila nakikipag-usap o umaasa sa isa't isa,,en,Apat,,en,kapag nag-redeploy ka,,en,ang naapektuhan lamang na serbisyo ang muling na-deploy,,en,Ano ang spring boot,,en,ay isang bagong balangkas na makakatulong sa iyo na bumuo ng mga application ng Spring,,en,Habang wala namang kakaiba sa tunog tungkol sa Spring Boot,,en,kung paano ito tumutulong sa iyo na bumuo ng mga application ng Spring,,en; tightly coupled services offer minimal or no flexibility; inefficiency and high expenses. Compare monolithic systems with the Microservice Architecture. One, if you need to update or fix a service, you do so without impacting the other services. You may, however, need to review the communication protocol post the update or fix. Two, since the services are loosely coupled, replacement is easy without impacting the system. Three, you always can use services based on requirements regardless of how they communicate or depend on one another. Four, when you redeploy, only the impacted service is redeployed.

Must Read- Mga hakbang upang gumana sa Spring Integration,en

What is spring boot?

The Spring Boot is a new framework that helps you build Spring applications. While that sounds nothing unique about Spring Boot, how it helps you build Spring applications is. Nag-aalok ito ng ilang mga natatanging paraan na makakatulong sa iyong makabuo ng mga application nang madali,,en,makakatulong ito sa iyo na maiwasan ang pagsulat ng verbo code,,en,tinukoy din bilang pagpapaunlad ng software ng Boilerplate sa pamamagitan ng pagpapasimple ng pagsulat ng code,,en,Nilalayon nitong palayain ang mga developer ng software mula sa pagtitiwala sa XML,,en,isang bagay na talagang intrinsic sa balangkas ng Spring,,en,Ang balangkas ng Spring,,en,siya nga pala,,en,naharap sa mabibigat na pagpuna dahil sa malaki nitong pagsasaayos ng XML,,en,Ang Spring Boot ay hindi lamang binabawasan ang paggamit ng,,en,habang nagtatayo ng mga application ng software ngunit kahit na gumagawa ng matapang na paghahabol ng pagpapalaya sa mga developer mula sa pagsulat ng mga pahayag sa pag-import,,en,Spring boot - Ano ang kaugnayan?,,en,ginagawang mas madali ang pagbuo ng mga mikrobyo dahil maaaring pumili ng mga developer ng software,,en,at gumamit ng mga kinakailangang sangkap mula sa balangkas nang hindi nangangailangan ng paggastos ng masyadong maraming oras sa mga detalye ng mga bahagi,,en. First, it helps you avoid writing verbose code, also referred to as Boilerplate software development by simplifying code writing. Second, it aims to free software developers from XML dependency, something that was quite intrinsic with the Spring framework. The Spring framework, by the way, faced heavy criticism because of its bulky XML configuration. Spring Boot not only reduces the use of XML while building software applications but even makes the bold claim of freeing developers from writing import statements.

Must Read – Steps to learn Spring Batch Service

Microservices & Spring boot – What is the relation?

Spring Boot makes developing Microservices easier because software developers can choose, and use required components from the framework without needing to spend too much time on details of the components. Ang Spring Boot ay nakasalalay sa tuktok ng Framework ng Spring at binibigyan nito ang mga developer ng kung ano ang kailangan nila kaya't hindi kailangan ng mga developer na balasin ang balangkas at gumugol ng oras sa mga bahagi ng pagbuo para sa aplikasyon ng Spring,,en,Maaari mong sabihin na ito ay isang uri ng mini framework,,en,Pinapadali ng Spring Boot ang higit na pagiging produktibo sa pamamagitan ng paggawa ng mga konsepto tulad ng RESTful HTTP at naka-embed na application ng web na madaling gamitin at maipadala.,,en,Nagbibigay ang Spring Boot ng isang maginhawang mekanismo para sa pagbuo ng mga application,,en,kilala bilang Spring Initializer,,en,Ginagawa nitong madali ang pag-unlad sa pamamagitan ng pagpapahintulot sa mga developer na pumili nang eksakto sa mga silid-aklatan na kailangan nila,,en,pagbibigay ng metadata at pagbubuo lamang ng proyekto,,en,ang pagpapaunlad ng mga mikropono ay maaaring mapabilis gamit ang Spring Boot,,en,Para sa paglikha ng microservices,,en,sa pamamagitan ng paggamit ng balangkas ng Spring,,en,kailangan nating sundin ang ilang mga hakbang,,en. You can say that it is a kind of mini framework. Spring Boot facilitates greater productivity by making concepts such as RESTful HTTP and embedded web application easy to use and deploy. Spring Boot provides a convenient mechanism for building applications, known as the Spring Initializer. It makes development easy by allowing developers to choose exactly the libraries they need, providing metadata and just generating the project. Overall, development of Microservices can be expedited using Spring Boot.








Sample application & Configuration

For creating microservices (by using Spring framework), we need to follow certain steps. Tatalakayin namin ang mga hakbang na iyon isa-isa,,en,Pagrehistro ng serbisyo,,en,Ang mga microservice ay talagang magkakaibang proseso na nagtutulungan,,en,Ito ay isang komunikasyon batay sa serbisyo sa halip na isang bahagi batay sa arkitektura,,en,upang hanapin ang mga Microservices na iyon,,en,ang ilang pagpapatala ay dapat panatilihin,,en,lahat ng mga microservices ay ipaparehistro doon,,en,Mayroong ilang mga server ng pagpaparehistro,,en,magagamit sa merkado,,en,Ang isa ay tinatawag na ‘Eureka,,en,na isinama ng Spring cloud,,en,Sinusuportahan din ng spring cloud ang 'Consul',,en,na kung saan ay isang kahalili sa 'Eureka',,en,Sample na server ng pagpaparehistro,,en,@EnableEurekaServer,,en,pampublikong klase ServiceRegServer,,en,Hahanapin ng Spring Boot ang registration-server.yml,,en,System.setProperty,,en,spring.config.name,,en,registration-server,,en,Paglapat ng Spring.run,,en,ServiceRegServer.class,,en,pom.xml,,bg,Sa seksyong ito susuriin namin ang mga pangunahing bahagi sa pom.xml config file,,en.

Must Read – Spring with Testing frameworks – How does it work?

Registration of service:

Microservices are actually different processes working together. It is a service based communication instead of a component based architecture. Now, to find those Microservices, some registry has to be maintained. And, all the microservices will be registered there. There are couple of registration servers (open source) available in the market. One is called ‘Eureka’ which is incorporated by Spring cloud. Spring cloud also supports ‘Consul’, which is an alternative to ‘Eureka’.

Listing 1: Sample registration server

@SpringBootApplication

@EnableEurekaServer

public class ServiceRegServer {

 

pampublikong static na walang bisa pangunahing(Pisi[] args) {

// Spring Boot will look for registration-server.yml

System.setProperty(“spring.config.name”, “registration-server”);

SpringApplication.run(ServiceRegServer.class, args);

}

}

Configuration (pom.xml):

In this section we will check the main components in pom.xml configuration file.

Listing 2: Sample na file ng pagsasaayos ng pom.xml,,en,org.springframework.cloud,,en,spring-cloud-starter-magulang,,en,_Brixton_.RELEASE,,en,I-setup ang Spring Boot,,en,spring-boot-starter,,en,I-setup ang Spring MVC at REST,,en,gamit ang naka-embed na server ng Tomcat,,en,Starter ng Spring Cloud,,en,spring-cloud-starter,,en,Kunin ang Eureka para sa pagpaparehistro sa serbisyo,,en,spring-cloud-starter-eureka-server,,en,File ng mga pag-aari,,en,Ang application ng Spring boot palaging suriin ang application.properties o application.yml file para sa impormasyong nauugnay sa pagsasaayos,,en,Sa application na ito,,en,magkakaroon kami ng application.yml,,en,reg-srever.yml,,cy,file para sa pagtatago ng impormasyong nauugnay sa pagsasaayos,,en,Discovery pagsasaayos ng server,,en,eureka,,en,hostname,,en,kliyente,,en,registerWithEureka,,en,fetchRegistry,,en,kung buksan natin ang dashboard ng Eureka sa http,,en,ang seksyon ng mga application ay walang laman,,en,kailangan naming irehistro ang microservice sa server ng pagtuklas,,en,kung ire-refresh namin ang dashboard,,en,ipapakita nito ang microservice doon,,en,Pagsasaayos ng klase sa pagsisimula ng spring boot,,en

<parent>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-parent</artifactId>

<version>_Brixton_.RELEASE</version>

</parent>

<dependencies>

<dependency>

<!– Setup Spring Boot –>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<dependency>

<!– Setup Spring MVC and REST, using embedded Tomcat server –>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<!– Spring Cloud starter –>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter</artifactId>

</dependency>

<dependency>

<!– Take Eureka for service registration –>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka-server</artifactId>

</dependency>

</dependencies>

Properties file:

Spring boot application always check application.properties or application.yml file for configuration related information. In this application, we will have application.yml (reg-srever.yml) file for storing configuration related information.

Listing 3: Sample registration server

# Discovery server configuration

eureka:

instance:

hostname: localhost

client:

registerWithEureka: false

fetchRegistry: false

server:

port: 8761 # HTTP (Tomcat) port

Now, if we open the Eureka dashboard at http://localhost:8761, applications section will be empty. So, we need to register the microservice with the discovery server. Now, if we refresh the dashboard, it will show the microservice there.

Spring boot start-up class configuration:

Ito ang klase sa pagsisimula ng boot ng Spring,,en,Ito ang pangunahing punto ng pagpasok sa application,,en,Sample na klase ng starter ng Spring boot,,en,@EnableAutoConfiguration,,en,@EnableDiscoveryClient,,en,@Angkat,,en,SampleWebApplication.class,,en,pampublikong klase ng AccServer,,en,AccRepository accRepository,,en,I-configure gamit ang acc-server.yml,,en,acc-server,,en,AccServer.class,,en,Pagrehistro kasama ang server ng pagtuklas,,en,iparehistro namin ang klase sa server ng pagtuklas,,en,Magrehistro kasama ang server ng pagtuklas,,en,Mga detalye ng pag-aari ng tagsibol,,en,acc-service,,en,Mga Detalye ng Pag-access sa Discovery Server,,en,serviceUrl,,fr,defaultZone,,en,eureka /,,en,Mga detalye ng HTTP Server,,en,i-refresh ang dashboard,,en,at ipapakita nito ang microservice na nakalista sa ilalim ng mga seksyon ng mga application,,en,Tila ang pagpapabuti ng pagiging produktibo ng developer at pag-save ng oras ay dalawa sa pinakamahalagang tampok ng kombinasyon ng Spring Boot at Microservices,,en. This is the main entry point to the application.

Listing 4: Sample Spring boot starter class.

@EnableAutoConfiguration

@EnableDiscoveryClient

@Import(SampleWebApplication.class)

public class AccServer {

@Autowired

AccRepository accRepository;

pampublikong static na walang bisa pangunahing(Pisi[] args) {

// Configure using acc-server.yml

System.setProperty(“spring.config.name”, “acc-server”);

SpringApplication.run(AccServer.class, args);

}

}

Registration with discovery server

Here, we will register the class with the discovery server.

Listing 5: Register with the discovery server

# Spring properties details

spring:

application:

pangalan: acc-service

# Discovery Server Access Details

eureka:

client:

serviceUrl:

defaultZone: http://localhost:8761/eureka/

# HTTP Server details

server:

port: 8080 # HTTP (Tomcat) port

Now, refresh the dashboard, and it will show the microservice listed under the applications sections.








Konklusyon

It seems improving developer productivity and saving time are two of the most important features of the Spring Boot and Microservices combination. However, kailangan ding pansinin na ito ay pa rin isang bagong konsepto at ang lupong tagahatol ay wala sa kanyang unibersal na kakayahang magamit at ang mahihinang mga benepisyo ay makakaipon pa,,en,Magbasa ng higit pang mga artikulo sa Spring,,en.

Explore – Read more articles on Spring

 

============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share