Nyob zoo txhua tus, ntawm no yog qhov thib peb tshaj tawm hauv Quarkus series!
Thaum tsim Java microservices, nws feem ntau ntseeg tias
Hauv kev nthuav dav me ntsis, peb yuav xub saib cov peev txheej thiab cov ntsiab lus ntawm yuav ua li cas Quarkus txhawb nqa Spring APIs los qhia Spring developers yuav siv MicroProfile API li cas hauv lawv cov haujlwm niaj hnub. Tom qab ntawd peb yuav npog MicroProfile APIs, uas muaj txiaj ntsig zoo rau Spring developers thaum tsim microservices.
Vim li cas Quarkus? Ua ntej, qhov no yog nyob coding, uas yog, tsis siv neeg reloading ntawm txhua qhov kev hloov pauv hauv MicroProfile API, Spring API thiab lwm yam Java APIs, uas tau ua nrog ib qho lus txib: mvn quarkus: dev. Thib ob, sib tham hauv
Peb yuav tsis nkag mus rau hauv cov ncauj lus kom ntxaws txog MicroProfile, uas tsis yog los pab Spring developers nkag siab tias lawv tuaj yeem siv Spring APIs li cas nrog MicroProfile APIs hauv Quarkus.
Ntim thiab Kubernetes
Txhawm rau ua kom cov kab lus no yooj yim, peb tsuas yog hais txog cov theem siab ntawm kev txhawb nqa ntawm no.
Quarkus thiab
Thaum kawg, los ntawm kev tsom mus rau Kubernetes raws li lub hom phiaj kev xa tawm ib puag ncig, Quarkus tsis siv Java lub hauv paus nyob rau hauv cov xwm txheej uas muaj kev ua haujlwm zoo sib xws nyob rau theem ntawm Kubernetes platform nws tus kheej. Table 1 muab ib daim ntawv qhia txog kev ua haujlwm sib txuas lus ntawm Kubernetes thiab cov qauv Java siv los ntawm Spring developers.
Table 1. Daim duab qhia kev ua haujlwm ntawm Java lub moj khaum thiab Kubernetes.
Kev ua haujlwm
Ib txwm caij nplooj ntoos hlav khau raj
Kubernetes
Kev pab nrhiav pom
Eureka
DNS
configuration
Spring Cloud Config
Config Maps/Secrets
Ntsuas qhov sib npaug
Ribbon (tus neeg siv khoom sab)
Service, Replication Controller (server side)
Sau thiab khiav cov cai los ntawm qhov piv txwv
Hauv kab lus no peb xa mus
Spring Framework APIs
Cawv Raug Txhaj
Quarkus txhawb ntau yam
Π
Table 2. Piv txwv ntawm kev siv Spring DI APIs uas txhawb nqa.
Txhawb Spring DI Nta
piv txwv
Constructor Txhaj
public PersonSpringController(
PersonSpringRepository personRepository, // injected
PersonSpringMPService personService) { // injected
this.personRepository = personRepository;
this.personService = personService;
}
Kev Txhaj Tshuaj
@Autowired
@RestClient
SalutationRestClient salutationRestClient;
@Value("${fallbackSalutation}")
String fallbackSalutation;
@Configuration
@Configuration
public class AppConfiguration {
@Bean(name = "capitalizeFunction")
public StringFunction capitalizer() {
return String::toUpperCase;
}
}
@Component("noopFunction")
public class NoOpSingleStringFunction implements StringFunction {
@Override
public String apply(String s) {
return s;
}
}
@Service
public class MessageProducer {
@Value("${greeting.message}")
String message;
public String getPrefix() {
return message;
}
}
Lub vev xaib
Cov neeg siv MicroProfile yuav nyiam tias Quarkus txhawb nqa JAX-RS, MicroProfile Rest Client, JSON-P, thiab JSON-B ua tus qauv web programming thawj. Cov neeg tsim tawm caij nplooj ntoos hlav yuav txaus siab rau Quarkus 'kev txhawb nqa tsis ntev los no rau Spring Web API, tshwj xeeb REST interfaces. Zoo ib yam li Spring DI, lub hom phiaj tseem ceeb ntawm Spring Web API kev txhawb nqa yog kom cov neeg tsim khoom Spring siv Spring Web APIs nrog rau MicroProfile APIs. Piv txwv ntawm kev siv Spring Web APIs uas txhawb nqa tau muab rau hauv Table 3, thiab cov ntaub ntawv ntau ntxiv thiab cov piv txwv ntawm cov ncauj lus no tuaj yeem pom hauv Quarkus cov lus qhia hu ua
Table 3. Piv txwv ntawm kev siv Spring Web APIs uas txhawb nqa.
Txhawb Spring Web Features
piv txwv
@RestController
@RequestMapping
@RestController
@RequestMapping("/person")
public class PersonSpringController {
...
...
...
}
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
@RequestParam
@RequestHeader
@MatrixVariable
@PathVariable
@CookieValue
@RequestBody
@ResponseStatus
@ExceptionHandler
@RestControllerAdvice (ib nrab)
@GetMapping(path = "/greet/{id}",
produces = "text/plain")
public String greetPerson(
@PathVariable(name = "id") long id) {
...
...
...
}
Spring DataJPA
Cov neeg siv MicroProfile kuj tseem yuav txaus siab tias Quarkus txhawb JPA siv Hibernate ORM. Kuj tseem muaj xov xwm zoo rau cov neeg tsim tawm caij nplooj ntoos hlav: Quarkus txhawb nqa cov ntaub ntawv Caij nplooj ntoos hlav JPA cov lus piav qhia thiab hom. Piv txwv ntawm kev siv Spring Data JPA APIs tau muab rau hauv Table 4.
Π
Table 4. Piv txwv ntawm kev siv Spring Data JPA APIs.
Txhawb Spring Data JPA Nta
piv txwv
CrudRepository
public interface PersonRepository
extends JpaRepository,
PersonFragment {
...
}
Repository
JpaRepository
PagingAndSortingRepository
public class PersonRepository extends
Repository {
Person save(Person entity);
Optional findById(Person entity);
}
Repository Fragments
public interface PersonRepository
extends JpaRepository,
PersonFragment {
...
}
Tau txais cov lus nug
public interface PersonRepository extends CrudRepository {
List findByName(String name);
Person findByNameBySsn(String ssn);
Optional
findByNameBySsnIgnoreCase(String ssn);
Boolean existsBookByYearOfBirthBetween(
Integer start, Integer end);
}
Cov lus nug ntawm tus neeg siv
public interface MovieRepository
extends CrudRepository {
Movie findFirstByOrderByDurationDesc();
@Query("select m from Movie m where m.rating = ?1")
Iterator findByRating(String rating);
@Query("from Movie where title = ?1")
Movie findByTitle(String title);
}
MicroProfile APIs
Ua txhaum cai
Fault kam rau ua tsim yog ib qho tseem ceeb heev rau kev tiv thaiv cascading tsis ua hauj lwm thiab tsim txhim khu kev qha microservice architectures. Cov neeg tsim tawm caij nplooj ntoos hlav tau siv cov hluav taws xob hluav taws xob rau kev ua txhaum cai rau ntau xyoo.
Table 5. Piv txwv ntawm kev siv txhawb MicroProfile Fault Tolerance APIs.
MicroProfile Fault Tolerance Nta
piav qhia
piv txwv
@Asynchronous
Execute logic nyob rau hauv ib tug cais xov
@Asynchronous
@Retry
public Future<String> getSalutation() {
...
return future;
}
@Bulkhead
Txwv tus naj npawb ntawm kev thov ib txhij
@Bulkhead(5)
public void fiveConcurrent() {
makeRemoteCall(); //...
}
@CircuitBreaker
Kev tswj tsis tau ntse thiab rov qab los ntawm kev ua tsis tiav
@CircuitBreaker(delay=500 // milliseconds
failureRatio = .75,
requestVolumeThreshold = 20,
successThreshold = 5)
@Fallback(fallbackMethod = "fallback")
public String getSalutation() {
makeRemoteCall(); //...
}
@Fallback
Hu rau lwm qhov laj thawj yog tias ua tsis tiav
@Timeout(500) // milliseconds
@Fallback(fallbackMethod = "fallback")
public String getSalutation() {
makeRemoteCall(); //...
}
public String fallback() {
return "hello";
}
Rov sim dua thaum thov ua tsis tiav
@Retry(maxRetries=3)
public String getSalutation() {
makeRemoteCall(); //...
}
Kev tswj tsis tau sijhawm
@Timeout(value = 500 ) // milliseconds
@Fallback(fallbackMethod = "fallback")
public String getSalutation() {
makeRemoteCall(); //...
}
Cov Kev Pab Cuam (Service Health)
Kubernetes platforms saib xyuas kev noj qab haus huv ntawm cov thawv uas siv cov kev pabcuam tshwj xeeb. Txhawm rau tso cai rau lub hauv paus platform los saib xyuas cov kev pabcuam, Spring developers feem ntau siv HealthIndicator thiab Spring Boot Actuator. Hauv Quarkus, qhov no tuaj yeem ua tiav siv MicroProfile Health, uas los ntawm lub neej ntawd ua qhov kev kuaj xyuas muaj sia nyob, tab sis tuaj yeem teeb tsa rau tib lub sijhawm tshawb xyuas lub neej thiab kev npaj txhij. Piv txwv ntawm kev siv MicroProfile Health APIs uas txhawb nqa tau muab rau hauv Table 6, thiab cov ntaub ntawv ntxiv tau muab rau hauv Quarkus phau ntawv
Table 6: Siv cov piv txwv ntawm kev txhawb nqa MicroProfile Health APIs.
MicroProfile Health nta
piav qhia
piv txwv
@Kev
Lub platform reboots ua tsis tau tejyam ntim cov ntawv thov
Endpoint:
host:8080/health/live
@Liveness
public class MyHC implements HealthCheck {
public HealthCheckResponse call() {
...
return HealthCheckResponse
.named("myHCProbe")
.status(ready ? true:false)
.withData("mydata", data)
.build();
}
@Readiness
Lub platform yuav tsis xa tsheb mus rau cov ntawv thov ntim khoom yog tias nws tsis npaj txhij
Endpoint:
party:8080/health/npaj
@Readiness
public class MyHC implements HealthCheck {
public HealthCheckResponse call() {
...
return HealthCheckResponse
.named("myHCProbe")
.status(live ? true:false)
.withData("mydata", data)
.build();
}
Kev ntsuas
Cov ntawv thov muab cov ntsuas ntsuas rau lub hom phiaj ua haujlwm (los saib xyuas kev ua haujlwm SLAs) lossis cov hom phiaj tsis ua haujlwm (kev lag luam SLAs). Cov neeg tsim khoom caij nplooj ntoos hlav muab kev ntsuas siv Spring Boot Actuator thiab Micrometer. Nyob rau hauv lem, Quarkus siv MicroProfile Metrics los muab cov hauv paus ntsuas (JVM thiab kev khiav hauj lwm qhov system), tus neeg muag khoom ntsuas (Quarkus), thiab daim ntawv thov metrics. MicroProfile Metrics xav kom qhov kev siv txhawb JSON thiab OpenMetrics (Prometheus) tso zis tawm tswv yim. Piv txwv ntawm kev siv MicroProfile Metrics API tau muab rau hauv Table 7.
Π
Table 7. Piv txwv ntawm kev siv MicroProfile Metrics APIs.
MicroProfile Metrics nta
piav qhia
piv txwv
@ suav
Qhia txog lub txee txee uas suav cov sij hawm ib qho khoom piav qhia tau raug hu
@Counted(name = "fallbackCounter",
displayName = "Fallback Counter",
description = "Fallback Counter")
public String salutationFallback() {
return fallbackSalutation;
}
@ConcurrentGauge
Denotes ib lub ntsuas uas suav tus naj npawb ntawm concurrent hu mus rau annotated khoom
@ConcurrentGuage(
name = "fallbackConcurrentGauge",
displayName="Fallback Concurrent",
description="Fallback Concurrent")
public String salutationFallback() {
return fallbackSalutation;
}
@Gaug
Qhia txog lub ntsuas ntsuas ntsuas uas ntsuas tus nqi ntawm ib qho khoom piav qhia
@Metered(name = "FallbackGauge",
displayName="Fallback Gauge",
description="Fallback frequency")
public String salutationFallback() {
return fallbackSalutation;
}
@Metered
Denotes ib meter sensor uas saib xyuas qhov hu zaus ntawm ib qho khoom annotated
@Metered(name = "MeteredFallback",
displayName="Metered Fallback",
description="Fallback frequency")
public String salutationFallback() {
return fallbackSalutation;
}
Ib qho lus piav qhia uas muaj cov ntaub ntawv hais txog metadata thaum tau txais kev thov nkag los yog tsim cov metric
@Metric
@Metered(name = "MeteredFallback",
displayName="Metered Fallback",
description="Fallback frequency")
public String salutationFallback() {
return fallbackSalutation;
}
Qhia lub timer uas taug qab lub sijhawm ntawm ib qho khoom piav qhia
@Timed(name = "TimedFallback",
displayName="Timed Fallback",
description="Fallback delay")
public String salutationFallback() {
return fallbackSalutation;
}
Metrics Endpoints
Daim ntawv thov ntsuas
Basic metrics
Tus neeg muag khoom metrics
Txhua qhov ntsuas
MicroProfile Rest Client
Microservices feem ntau muab RESTful cov ntsiab lus kawg uas xav tau cov neeg siv khoom APIs ua haujlwm nrog. Txhawm rau siv RESTful endpoints, Spring developers feem ntau siv RestTemplate. Quarkus muaj MicroProfile Rest Client APIs los daws qhov teeb meem no, piv txwv ntawm kev siv uas tau muab rau hauv Table 8.
Π
Table 8. Piv txwv ntawm kev siv MicroProfile Rest Client APIs.
MicroProfile Rest Client Features
piav qhia
piv txwv
@RegisterRestClient
Sau npe tus ntaus Java interface ua tus neeg siv REST
@RegisterRestClient
@Path("/")
public interface MyRestClient {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getSalutation();
}
@RestClient
Kos qhov kev siv ntawm ib qho piv txwv ntawm tus ntaus REST tus neeg siv interface
@Autowired // or @Inject
@RestClient
MyRestClient restClient;
Tsav lus
Hu rau REST qhov kawg
System.out.println(
restClient.getSalutation());
mp-rest/url
Qhia qhov kawg ntawm REST
application.properties:
org.example.MyRestClient/mp-rest/url=
http://localhost:8081/myendpoint
Cov txiaj ntsim tau los
Hauv qhov blog no, tsom feem ntau ntawm Spring developers, peb tau saib sai sai ntawm yuav ua li cas siv Spring APIs nrog MicroProfile APIs hauv Quarkus los tsim Java microservices thiab tom qab ntawd muab tso ua ke rau hauv haiv neeg binary code uas txuag ntau pua megabytes ntawm RAM thiab tso tawm hauv ib qhov teeb meem ntawm milliseconds.
Raws li koj twb tau nkag siab lawm, cov ntaub ntawv ntau ntxiv txog kev txhawb nqa rau Lub Caij Nplooj Ntoos Hlav thiab MicroProfile APIs, nrog rau ntau cov ntaub ntawv tseem ceeb, tuaj yeem pom hauv
Tau qhov twg los: www.hab.com