Lumelang bohle, poso ea boraro ke ena letotong la Quarkus!
Ha ho etsoa li-microservices tsa Java, hangata ho lumeloa hore
Ka lintlha tse ling, re tla qala ka ho sheba boholo le lintlha tsa hore na Quarkus e ts'ehetsa li-API tsa selemo joang ho bonts'a baetsi ba Selemo mokhoa oa ho sebelisa MicroProfile API mosebetsing oa bona oa letsatsi le letsatsi. Ebe re tla koahela MicroProfile APIs, e leng molemo ho baetsi ba Selemo ha ba theha li-microservices.
Ke hobane'ng ha Quarkus? Taba ea pele, ena ke ho kenya likhoutu ka boits'oaro, ke hore, ho kenya bocha liphetoho tse fumanehang ho MicroProfile API, Spring API le li-API tse ling tsa Java, tse etsoang ka taelo e le 'ngoe feela: mvn quarkus:dev. Ea bobeli, ho buisanoa ka eona
Re ke ke ra bua ka botlalo ka MicroProfile, ntle le ho thusa baetsi ba Selemo ho utloisisa hore na ba ka sebelisa li-API tsa Spring joang ka MicroProfile APIs ho Quarkus.
Lijana le Kubernetes
Ho boloka sengoloa sena se le bonolo, re tla akaretsa feela likarolo tsa boemo bo holimo tsa tšehetso mona.
Quarks le eona
Qetellong, ka ho lebisa tlhokomelo ho Kubernetes e le sebaka sa ho tsamaisoa ha sepheo, Quarkus ha e sebelise mekhoa ea Java maemong ao ts'ebetso e ts'oanang e kenngoa boemong ba sethaleng sa Kubernetes ka boeona. Letlapa la 1 le fana ka 'mapa oa mangolo a sebetsang pakeng tsa Kubernetes le mekhoa e tloaelehileng ea Java e sebelisoang ke baetsi ba Spring.
Letlapa la 1. 'Mapa oa mangolo a sebetsang pakeng tsa mekhoa ea Java le Kubernetes.
Sebetsa
Traditional Spring Boot
Kubernetes
Ho sibolloa ha litšebeletso
Eureka
DNS
phetolo
Spring Cloud Config
Lokisa 'mapa/Liphiri
Meroalo e boima
Ribone (lehlakore la moreki)
Tšebeletso, Replication Controller (lehlakore la seva)
Ho bokella le ho tsamaisa khoutu ho tsoa mohlaleng
Sehloohong sena re bua ka
Spring Framework APIs
Ente ea ho itšepa
Quarkus e tšehetsa mefuta e mengata ea
В
Lethathamo la 2. Mehlala ea ho sebelisa li-API tsa Spring DI tse tšehetsoeng.
Likarolo tsa DI tsa selemo tse tšehelitsoeng
mehlala
Ente ea Moetsi
public PersonSpringController(
PersonSpringRepository personRepository, // injected
PersonSpringMPService personService) { // injected
this.personRepository = personRepository;
this.personService = personService;
}
Ente ea Tšimo
@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;
}
}
Moralo oa webo
Basebelisi ba MicroProfile ba tla rata hore Quarkus e ts'ehetsa JAX-RS, MicroProfile Rest Client, JSON-P, le JSON-B joalo ka mohlala oa mantlha oa lenaneo la webo. Bahlahisi ba selemo ba tla thabela tšehetso ea morao-rao ea Quarkus bakeng sa Spring Web API, haholo-holo li-interface tsa REST. E ts'oanang le Spring DI, sepheo se seholo sa tšehetso ea Spring Web API ke ho nolofalletsa baetsi ba Selemo ho sebelisa Spring Web APIs hammoho le MicroProfile APIs. Mehlala ea mokhoa oa ho sebelisa li-API tsa Websaete tse tšehetsoeng li fanoe ho Lethathamo la 3, 'me boitsebiso bo eketsehileng le mehlala ka taba ena e ka fumanoa thutong ea Quarkus e bitsoang.
Lethathamo la 3. Mehlala ea ho sebelisa li-API tsa Spring Web tse tšehetsoeng.
Litšobotsi tsa Websaete tsa Selemo tse tšehelitsoeng
mehlala
@RestController
@RequestMapping
@RestController
@RequestMapping("/person")
public class PersonSpringController {
...
...
...
}
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
@RequestParam
@RequestHeader
@MatrixVariable
@PathVariable
@CookieValue
@RequestBody
@ResponseStatus
@ExceptionHandler
@RestControllerAdvice (karolelano)
@GetMapping(path = "/greet/{id}",
produces = "text/plain")
public String greetPerson(
@PathVariable(name = "id") long id) {
...
...
...
}
Lintlha tsa Selemo JPA
Basebelisi ba MicroProfile le bona ba tla ananela hore Quarkus e tšehetsa JPA e sebelisa Hibernate ORM. Ho boetse ho na le litaba tse monate bakeng sa bahlahisi ba Selemo: Quarkus e ts'ehetsa litlatsetso le mefuta e tloaelehileng ea Spring Data JPA. Mehlala ea ho sebelisa li-API tsa Spring Data JPA API li fanoe ho Lethathamo la 4.
В
Letlapa la 4. Mehlala ea ho sebelisa li-API tsa Spring Data JPA tse tšehetsoeng.
Lintlha tse tšehetsoeng tsa Spring Data JPA Features
mehlala
CrudRepository
public interface PersonRepository
extends JpaRepository,
PersonFragment {
...
}
Repository
JpaRepository
PagingAndSortingRepository
public class PersonRepository extends
Repository {
Person save(Person entity);
Optional findById(Person entity);
}
Likhechana tsa polokelo
public interface PersonRepository
extends JpaRepository,
PersonFragment {
...
}
Mekhoa ea ho botsa lipotso
public interface PersonRepository extends CrudRepository {
List findByName(String name);
Person findByNameBySsn(String ssn);
Optional
findByNameBySsnIgnoreCase(String ssn);
Boolean existsBookByYearOfBirthBetween(
Integer start, Integer end);
}
Lipotso tse hlalosoang ke basebelisi
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
Ho mamella liphoso
Mehaho ea mamello ea liphoso e bohlokoa haholo bakeng sa ho thibela liphoso tse fokolang le ho theha meralo e tšepahalang ea microservice. Bahlahisi ba selemo ba 'nile ba sebelisa li-circuit-breakers bakeng sa mamello ea liphoso ka lilemo tse ngata.
Letlapa la 5. Mehlala ea ho sebelisa li-API tse tšehetsoeng ke MicroProfile Fault Tolerance.
Likarolo tsa Mamello ea Phoso ea MicroProfile
tlhaloso
mehlala
@Asynchronous
Ho etsa logic ka khoele e ka thoko
@Asynchronous
@Retry
public Future<String> getSalutation() {
...
return future;
}
@Bulkhead
Fokotsa palo ea likopo tsa nako e le 'ngoe
@Bulkhead(5)
public void fiveConcurrent() {
makeRemoteCall(); //...
}
@CircuitBreaker
Smart ho hloleha ho sebetsana le ho hlaphoheloa ho tloha ho hloleha
@CircuitBreaker(delay=500 // milliseconds
failureRatio = .75,
requestVolumeThreshold = 20,
successThreshold = 5)
@Fallback(fallbackMethod = "fallback")
public String getSalutation() {
makeRemoteCall(); //...
}
@Boela morao
Ho bitsa logic enngwe ha ho ka hloleha
@Timeout(500) // milliseconds
@Fallback(fallbackMethod = "fallback")
public String getSalutation() {
makeRemoteCall(); //...
}
public String fallback() {
return "hello";
}
Leka hape ha kopo e hlolehile
@Retry(maxRetries=3)
public String getSalutation() {
makeRemoteCall(); //...
}
Ho hloleha ho laola nako
@Timeout(value = 500 ) // milliseconds
@Fallback(fallbackMethod = "fallback")
public String getSalutation() {
makeRemoteCall(); //...
}
Litšebeletso tsa ho hlahloba (Bophelo ba Ts'ebeletso)
Li-platform tsa Kubernetes li lekola bophelo bo botle ba lijana tse sebelisang lits'ebeletso tse khethehileng. Ho lumella sethala sa motheo ho beha lits'ebeletso ho lekola lits'ebeletso, bahlahisi ba Selemo ka tloaelo ba sebelisa HealthIndicator e tloaelehileng le Spring Boot Actuator. Ho Quarkus, sena se ka etsoa ho sebelisoa MicroProfile Health, eo ka nako e sa lekanyetsoang e etsang tlhahlobo ea bophelo, empa e ka hlophisoa hore ka nako e le 'ngoe e hlahlobe bophelo le ho itokisetsa. Mehlala ea mokhoa oa ho sebelisa li-API tsa bophelo bo botle tsa MicroProfile tse tšehetsoeng li fanoe ho Lethathamo la 6, 'me boitsebiso bo eketsehileng bo fanoe bukeng ea Quarkus.
Letlapa la 6: Mehlala ea tšebeliso ea MicroProfile Health APIs e tšehetsoeng.
Likarolo tsa Bophelo bo Botle ba MicroProfile
tlhaloso
mehlala
@Liveness
Sethala se qalisa bocha lits'ebetso tse kentsoeng ka har'a sesebelisoa
Qetello:
moamoheli:8080/health/live
@Liveness
public class MyHC implements HealthCheck {
public HealthCheckResponse call() {
...
return HealthCheckResponse
.named("myHCProbe")
.status(ready ? true:false)
.withData("mydata", data)
.build();
}
@Go ipaakanya
Sethala se ke ke sa romela sephethephethe ho lits'ebetso tse nang le lisebelisoa haeba se sa itokisetsa
Qetello:
moamoheli:8080/health/ready
@Readiness
public class MyHC implements HealthCheck {
public HealthCheckResponse call() {
...
return HealthCheckResponse
.named("myHCProbe")
.status(live ? true:false)
.withData("mydata", data)
.build();
}
Melemo
Likopo li fana ka metrics molemong oa ts'ebetso (ho beha leihlo li-SLA tsa ts'ebetso) kapa merero e sa sebetseng (li-SLA tsa khoebo). Baetsi ba selemo ba fana ka metrics ba sebelisa Spring Boot Actuator le Micrometer. Ka lehlakoreng le leng, Quarkus e sebelisa MicroProfile Metrics ho fana ka metrics ea mantlha (JVM le sistimi e sebetsang), metrics ea barekisi (Quarkus), le metrics ea ts'ebeliso. MicroProfile Metrics e hloka hore ts'ebetsong e tšehetse liforomo tsa tlhahiso ea JSON le OpenMetrics (Prometheus). Mehlala ea ho sebelisa MicroProfile Metrics API e fanoe ho Lethathamo la 7.
В
Letlapa la 7. Mehlala ea ho sebelisa MicroProfile Metrics APIs.
Likarolo tsa Metrics ea MicroProfile
tlhaloso
mehlala
@Ho baloa
E supa khaontara e balang palo ea makhetlo ao ntho e tšoailoeng e bitsitsoeng ka eona
@Counted(name = "fallbackCounter",
displayName = "Fallback Counter",
description = "Fallback Counter")
public String salutationFallback() {
return fallbackSalutation;
}
@ConcurrentGauge
E bolela sekala se balang palo ea mehala ea nako e le 'ngoe ho ea ho ntho e hlalositsoeng
@ConcurrentGuage(
name = "fallbackConcurrentGauge",
displayName="Fallback Concurrent",
description="Fallback Concurrent")
public String salutationFallback() {
return fallbackSalutation;
}
@Gauge
E supa sensara sa gauge se lekanyang boleng ba ntho e annoted
@Metered(name = "FallbackGauge",
displayName="Fallback Gauge",
description="Fallback frequency")
public String salutationFallback() {
return fallbackSalutation;
}
@Metered
E supa sensara sa metha e lekola maqhubu a mohala oa ntho e hlahisoeng
@Metered(name = "MeteredFallback",
displayName="Metered Fallback",
description="Fallback frequency")
public String salutationFallback() {
return fallbackSalutation;
}
Tlhaloso e nang le tlhahisoleseling mabapi le metadata ha kopo e amoheloa ea ho kenya kapa ho hlahisa metric
@Metric
@Metered(name = "MeteredFallback",
displayName="Metered Fallback",
description="Fallback frequency")
public String salutationFallback() {
return fallbackSalutation;
}
E supa sebali sa nako se latedisang nako ya ntho e itseng
@Timed(name = "TimedFallback",
displayName="Timed Fallback",
description="Fallback delay")
public String salutationFallback() {
return fallbackSalutation;
}
Metrics Endpoints
Metrics ea kopo
Metrics ea mantlha
Metrics ea barekisi
Metrics kaofela
Moreki oa Phomolo ea MicroProfile
Hangata li-Microservices li fana ka lintlha tse RESTful tse hlokang hore li-API tsa bareki tse tsamaisanang li sebetse le tsona. Ho sebelisa li-endpoints tse RESTful, baetsi ba Spring hangata ba sebelisa RestTemplate. Quarkus e fana ka MicroProfile Rest Client APIs ho rarolla bothata bona, mehlala ea ts'ebeliso ea eona e fanoeng ho Lethathamo la 8.
В
Letlapa la 8. Mehlala ea ho sebelisa MicroProfile Rest Client APIs.
Likarolo tsa Bareki ba Phomolo ea MicroProfile
tlhaloso
mehlala
@RegisterRestClient
E ngolisa Java interface e tlatsitsoeng joalo ka moreki oa REST
@RegisterRestClient
@Path("/")
public interface MyRestClient {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getSalutation();
}
@RestClient
E tšoaea ts'ebetsong ea mohlala oa sebopeho sa moreki se thaepileng sa REST
@Autowired // or @Inject
@RestClient
MyRestClient restClient;
Pōpo
E letsetsa REST pheletso
System.out.println(
restClient.getSalutation());
mp-rest/url
E totobatsa ntlha ea REST
application.properties:
org.example.MyRestClient/mp-rest/url=
http://localhost:8081/myendpoint
Liphello
Ho blog ena, e shebaneng haholo le bahlahisi ba Selemo, re ile ra shebisisa ka potlako mokhoa oa ho sebelisa li-API tsa Selemo ka MicroProfile APIs ho Quarkus ho nts'etsapele li-microservices tsa Java ebe li li bokella ka khoutu ea tlhaho ea binary e bolokang makholo a megabyte ea RAM le ho qala ho taba ea milliseconds.
Joalo ka ha u se u ntse u utloisisa, tlhaiso-leseling e batsi mabapi le ts'ehetso ea Spring le MicroProfile APIs, hammoho le tlhaiso-leseling e ngata ea bohlokoa, e ka fumanoa ho
Source: www.habr.com