Tena koutou katoa, anei te panui tuatoru o te raupapa Quarkus!
I te wa e whakawhanake ana i nga microservices Java, he maha nga wa e whakaponohia ana
I roto i nga korero iti ake, ka titiro tuatahi tatou ki te whanui me nga korero mo te tautoko a Quarkus i nga API Puna hei whakaatu i nga kaiwhakawhanake Puna me pehea te whakamahi i te MicroProfile API i a raatau mahi o ia ra. Na ka hipokina e matou nga API MicroProfile, e whai hua ana mo nga kaiwhakawhanake Puna i te wa e hanga ana i nga ratonga miihini.
He aha a Quarkus? Tuatahi, he whakawaehere ora tenei, ara, te whakahou aunoa i nga huringa i roto i te MicroProfile API, Spring API me etahi atu Java API, ka mahia me te whakahau kotahi: mvn quarkus:dev. Tuarua, i korerohia i roto i
E kore matou e korero mo nga korero mo te MicroProfile, i tua atu i te awhina i nga kaiwhakawhanake Puna ki te mohio me pehea te whakamahi i nga API Puna me nga API MicroProfile i Quarkus.
Nga ipu me nga Kubernetes
Kia ngawari ai tenei tuhinga, ka korero noa matou i nga waahanga taumata teitei o te tautoko i konei.
Quarkus hoki
Ka mutu, ma te aro ki nga Kubernetes hei taiao tukunga whaainga, kaore a Quarkus e whakamahi i nga angamahi Java i roto i nga keehi ka whakatinanahia nga mahi rite ki te taumata o te papaahi Kubernetes ake. Ko te Ripanga 1 he mapi mo nga reta mahi i waenga i nga Kubernetes me nga anga Java angamaheni e whakamahia ana e nga kaiwhakawhanake Puna.
Ripanga 1. Mahere o nga reta mahi i waenga i nga anga Java me nga Kubernetes.
Mahinga
He Putu Puna Tikanga
Kubernetes
Te kitenga ratonga
Eureka
DNS
whirihoranga
Whirihora Kapua Puna
Whirihorahia nga Mahere/Nga Mea ngaro
Te pauna toha
Ripene (taha kiritaki)
Ratonga, Kaiwhakahaere Tukurua (taha tūmau)
Te whakahiato me te whakahaere i te waehere mai i te tauira
I roto i tenei tuhinga e korero ana matou
Ko nga API Anga Koanga
Te Weronga Whakaaetanga
E tautoko ana a Quarkus i te whānuitanga o
В
Ripanga 2. He tauira mo te whakamahi i nga API Spring DI e tautokohia ana.
Tautokona Spring DI āhuatanga
tauira
Werohanga Kaihanga
public PersonSpringController(
PersonSpringRepository personRepository, // injected
PersonSpringMPService personService) { // injected
this.personRepository = personRepository;
this.personService = personService;
}
Werohanga Mara
@Autowired
@RestClient
SalutationRestClient salutationRestClient;
@Value("${fallbackSalutation}")
String fallbackSalutation;
@Whakawhiringa
@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;
}
}
Angamahi Tukutuku
Ka aroha nga kaiwhakamahi MicroProfile ki te tautoko a Quarkus i a JAX-RS, MicroProfile Rest Client, JSON-P, me JSON-B hei tauira kaupapa tukutuku tuatahi. Ka harikoa nga kaihanga o te puna ki te tautoko a Quarkus mo te API Tukutuku Puna, ina koa nga atanga REST. He rite ki te Puna DI, ko te whainga matua o te tautoko API Tukutuku Puna ko te whakaahei i nga kaiwhakawhanake Puna ki te whakamahi i nga API Tukutuku Puna i te taha o nga API MicroProfile. Ko nga tauira mo te whakamahi i nga API Tukutuku Puna e tautokohia ana kei te Ripanga 3, ka kitea etahi atu korero me nga tauira mo tenei kaupapa i roto i te akoranga Quarkus e kiia nei.
Ripanga 3. He tauira mo te whakamahi i nga API Tukutuku Puna e tautokohia ana.
Tautokohia nga waahanga Tukutuku Puna
tauira
@RestController
@RequestMapping
@RestController
@RequestMapping("/person")
public class PersonSpringController {
...
...
...
}
@TikinaMahere
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
@TonoParam
@RequestHeader
@MatrixVariable
@PathVariable
@CookieValue
@TinoTino
@Tauwhakautu
@ExceptionHandler
@RestControllerAdvice (waahanga)
@GetMapping(path = "/greet/{id}",
produces = "text/plain")
public String greetPerson(
@PathVariable(name = "id") long id) {
...
...
...
}
Raraunga Puna JPA
Ka maioha ano nga kaiwhakamahi MicroProfile kei te tautoko a Quarkus i a JPA ma te whakamahi i te ORM Hibernate. He korero pai ano mo nga Kaihanga Puna: Ka tautoko a Quarkus i nga korero me nga momo JPA Raraunga Puna noa. Ko nga tauira mo te whakamahi i nga API JPA Raraunga Puna e tautokohia ana kei te Ripanga 4.
В
Ripanga 4. He tauira mo te whakamahi i nga API JPA Raraunga Puna e tautokohia ana.
Tautokohia nga waahanga JPA Raraunga Puna
tauira
CrudRepository
public interface PersonRepository
extends JpaRepository,
PersonFragment {
...
}
Repository
JpaRepository
Te Wharangi Me te Whakaraupapa Putunga
public class PersonRepository extends
Repository {
Person save(Person entity);
Optional findById(Person entity);
}
Nga Waahanga Putunga
public interface PersonRepository
extends JpaRepository,
PersonFragment {
...
}
Tikanga patai i ahu mai
public interface PersonRepository extends CrudRepository {
List findByName(String name);
Person findByNameBySsn(String ssn);
Optional
findByNameBySsnIgnoreCase(String ssn);
Boolean existsBookByYearOfBirthBetween(
Integer start, Integer end);
}
Uiui tautuhi-kaiwhakamahi
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 API
Whakaaetanga he
He mea nui te hanga i te hee mo te aukati i nga ngoikoretanga me te hanga hoahoanga microservice pono. He maha nga tau e whakamahia ana e nga kaiwhakawhanake o te puna nga ara iahiko mo te aukati i te he.
Ripanga 5. He tauira mo te whakamahi i nga API Whakaaetanga Hapa MicroProfile e tautokohia ana.
Nga Ahuatanga Whakaaetanga Hapa MicroProfile
Whakaahuatanga
tauira
@Tukutahi
Te whakahaere arorau i roto i te miro motuhake
@Asynchronous
@Retry
public Future<String> getSalutation() {
...
return future;
}
@Tuhinga nui
Whakaitihia te maha o nga tono tukutahi
@Bulkhead(5)
public void fiveConcurrent() {
makeRemoteCall(); //...
}
@CircuitBreaker
Te whakahaere rahunga atamai me te whakaora mai i nga rahunga
@CircuitBreaker(delay=500 // milliseconds
failureRatio = .75,
requestVolumeThreshold = 20,
successThreshold = 5)
@Fallback(fallbackMethod = "fallback")
public String getSalutation() {
makeRemoteCall(); //...
}
@Fallback
Te waea atu i nga arorau rereke ina he rahua
@Timeout(500) // milliseconds
@Fallback(fallbackMethod = "fallback")
public String getSalutation() {
makeRemoteCall(); //...
}
public String fallback() {
return "hello";
}
Ngana ano ki te kore tono
@Retry(maxRetries=3)
public String getSalutation() {
makeRemoteCall(); //...
}
Whakamutua te wa whakahaere
@Timeout(value = 500 ) // milliseconds
@Fallback(fallbackMethod = "fallback")
public String getSalutation() {
makeRemoteCall(); //...
}
Nga ratonga arowhai (Ratonga Hauora)
Ka aro turukihia e nga papaahi Kubernetes te hauora o nga ipu ma te whakamahi i nga ratonga motuhake. Hei tuku i te papaapapa o raro ki te aro turuki i nga ratonga, ka whakamahia e nga kaiwhakawhanake Puna he ritenga HealthIndicator me te Spring Boot Actuator. I roto i te Quarkus, ka taea tenei ma te whakamahi i te MicroProfile Health, ma te taunoa ka mahi i te tirotiro ora, engari ka taea te whirihora ki te tirotiro i te ora me te reri. Ko nga tauira mo te whakamahi i nga API Hauora MicroProfile e tautokohia ana kei te Ripanga 6, me etahi atu korero kei roto i te pukapuka Quarkus
Ripanga 6: Nga tauira whakamahi o nga API Hauora MicroProfile tautoko.
Nga waahanga Hauora MicroProfile
Whakaahuatanga
tauira
@Te oranga
I rahua te whakaara ano o te papaaho i nga tono whakauru
Wāhi Whakamutunga:
ope:8080/hauora/ora
@Liveness
public class MyHC implements HealthCheck {
public HealthCheckResponse call() {
...
return HealthCheckResponse
.named("myHCProbe")
.status(ready ? true:false)
.withData("mydata", data)
.build();
}
@Reriri
E kore te papa e tuku waka ki nga tono ipuipui mena kaore i rite
Wāhi Whakamutunga:
kaihautū: 8080 / hauora / rite
@Readiness
public class MyHC implements HealthCheck {
public HealthCheckResponse call() {
...
return HealthCheckResponse
.named("myHCProbe")
.status(live ? true:false)
.withData("mydata", data)
.build();
}
Inenga
Ka whakaratohia e nga tono nga inenga mo nga kaupapa whakahaere (ki te aro turuki i nga SLA mahi) me nga kaupapa kore-mahi (nga SLA pakihi). Ka whakaratohia e nga kaihanga o te puna nga inenga ma te whakamahi i te Spring Boot Actuator me te Micrometer. Ka huri, ka whakamahi a Quarkus i nga MicroProfile Metrics ki te whakarato i nga inenga turanga (JVM me te punaha whakahaere), nga inenga kaihoko (Quarkus), me nga inenga tono. Ko te MicroProfile Metrics me tautoko te whakatinanatanga i nga whakatakotoranga whakaputa JSON me OpenMetrics (Prometheus). Ko nga tauira mo te whakamahi i te MicroProfile Metrics API kei te Ripanga 7.
В
Ripanga 7. He tauira mo te whakamahi i nga API inenga MicroProfile.
Nga waahanga inenga MicroProfile
Whakaahuatanga
tauira
@Kua tatau
He tohu porotiti e tatau ana i te maha o nga wa i karangahia ai tetahi mea kua tohua
@Counted(name = "fallbackCounter",
displayName = "Fallback Counter",
description = "Fallback Counter")
public String salutationFallback() {
return fallbackSalutation;
}
@ConcurrentGauge
He tohu ine e tatau ana i te maha o nga waea tukutahi ki tetahi ahanoa kua tohua
@ConcurrentGuage(
name = "fallbackConcurrentGauge",
displayName="Fallback Concurrent",
description="Fallback Concurrent")
public String salutationFallback() {
return fallbackSalutation;
}
@Mehua
Ka tohu i te pūoko ine e ine ana i te uara o tetahi mea kua tohua
@Metered(name = "FallbackGauge",
displayName="Fallback Gauge",
description="Fallback frequency")
public String salutationFallback() {
return fallbackSalutation;
}
@Metered
He tohu i te pūoko mita e aro turuki ana i te auau waea o te ahanoa kua tohua
@Metered(name = "MeteredFallback",
displayName="Metered Fallback",
description="Fallback frequency")
public String salutationFallback() {
return fallbackSalutation;
}
He korero kei roto nga korero mo te metadata ina tae mai he tono ki te whakauru, ki te whakaputa ine ranei
@Metric
@Metered(name = "MeteredFallback",
displayName="Metered Fallback",
description="Fallback frequency")
public String salutationFallback() {
return fallbackSalutation;
}
He tohu matawā e whai ana i te roanga o te ahanoa kua tohua
@Timed(name = "TimedFallback",
displayName="Timed Fallback",
description="Fallback delay")
public String salutationFallback() {
return fallbackSalutation;
}
Nga Waahi Whakamutunga
Inenga tono
Inenga taketake
Inenga kaihoko
Nga inenga katoa
Kiritaki okiokinga MicroProfile
He maha nga wa ka tukuna e nga Microservices nga tohu mutunga RESTful e hiahia ana ki nga API kiritaki e rite ana ki te mahi. Hei whakamahi i nga tohu mutunga RESTful, ka whakamahia e nga kaiwhakawhanake Puna te RestTemplate. Ka tuku a Quarkus i nga API Kiritaki Whakanoho MicroProfile ki te whakaoti i tenei raru, ko nga tauira o te whakamahi kei te Ripanga 8.
В
Ripanga 8. He tauira mo te whakamahi i nga API Kiritaki Whakanoho Morohiko.
MicroProfile Rest Client Features
Whakaahuatanga
tauira
@RegisterRestClient
Ka rēhita he atanga Java kua patohia hei kiritaki REST
@RegisterRestClient
@Path("/")
public interface MyRestClient {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getSalutation();
}
@RestClient
Ka tohu i te whakatinanatanga o tetahi tauira o te atanga kiritaki REST i patohia
@Autowired // or @Inject
@RestClient
MyRestClient restClient;
Whakataunga
Karangatia he pito mutunga REST
System.out.println(
restClient.getSalutation());
mp- okioki/url
Ka tautuhi i te pito mutunga REST
application.properties:
org.example.MyRestClient/mp-rest/url=
http://localhost:8081/myendpoint
Ngā putanga
I roto i tenei blog, i aro nui ki nga kaiwhakawhanake Puna, i titiro tere matou me pehea te whakamahi i nga API Puna me nga MicroProfile API i Quarkus ki te whakawhanake i nga ratonga moroiti Java katahi ka whakahiatohia ki roto i te waehere takirua taketake e whakaora ana i nga rau megabytes o te RAM ka whakarewahia ki roto. he mea mirihakona.
Kua mohio koe, ka kitea etahi atu korero mo te tautoko mo te Spring me te MicroProfile API, me te maha atu o nga korero whai hua, ka kitea i roto
Source: will.com