Share
Embed
Transcript
23 slides
-
How to adapt MicroProfile API for generic Web applications JJUG
CCC 2019 Fall #jjug_ccc #ccc_i5 HASUNUMA Kenji @khasunuma -
What’s MicroProfile? Not only for Microservices, but also for generic
Web applications HASUNUMA Kenji @khasunuma -
MicroProfile is ... •Java API for Microservices •Based on Java
EE Technology •Community Driven HASUNUMA Kenji @khasunuma -
Why and How to use MP Config API? As
my experience about systems integration HASUNUMA Kenji @khasunuma -
Application Development Different Configuration • Settings • Databases • OS/JVM
patches • Machine resources • Networks etc. HASUNUMA Kenji @khasunuma -
Default Config Sources • System Properties (Ordinal: 400) • Environment
Variables (Ordinal: 300) • microprofile-config.properties (Ordinal: 100) HASUNUMA Kenji @khasunuma -
e.g. Inject a Config Property HASUNUMA Kenji @khasunuma @Path(“Hello”) @RequestScoped
public class HelloResource { @Inject private Config config; @Inject @ConfigProperty(name = “service.url”) private String url; // snip; continue the next } -
e.g. Using a Config Property HASUNUMA Kenji @khasunuma @Path(“Hello”) @RequestScoped
public class HelloResource { // snip; url is assigned by Config API (prev) @GET public String sayHello() { Client client = ClientBulder.newClient(); String userName = client.target(url) .queryParam(“id”, 63) .request(“text/plain”).get(String.class); return “Hello, ” + username; } } -
Wrap Up: MP Config API • Divides configurations from code
so that applications can run any environments • Provides the same way to obtain the settings • Very versatile API – suitable for most kind of applications; see also JSR 382 (Withdrawn) HASUNUMA Kenji @khasunuma -
Why and How to use MP Rest Client API?
As my experience about systems integration HASUNUMA Kenji @khasunuma -
Using JAX-RS Client HASUNUMA Kenji @khasunuma @Path(“hello”) @RequestScoped public class
HelloResource { // url is assigned by anyway @GET public String sayHello() { Client client = ClientBulder.newClient(); String userName = client.target(url) .queryParam(“id”, 63) .request(“text/plain”).get(String.class); return “Hello, ” + username; } } -
Why needs MP Rest Client? • JAX-RS Client is very
flexible, but... • Not Type-safe • Often complex (especially Async) • Type-safe and simple API is required HASUNUMA Kenji @khasunuma -
Using MP Rest Client (1/3) HASUNUMA Kenji @khasunuma /* REST
Interface */ @RegisterRestClient @ApplicationScoped public interface UserService { // method to call API // (works such as a proxy) @GET @Produces(“text/plain”) String getName(@QueryParam(“id”) int id); } -
Using MP Rest Client (2/3) HASUNUMA Kenji @khasunuma @Path(“hello”) @RequestScoped
public class HelloResource { // Inject the REST Interface @Inject @RestClient private UserService userService; @GET public String sayHello() { return “Hello, ” + userSerivce.getName(63); } } -
Using MP Rest Client (3/3) How to obtain the endpoint
URL: • Using MP Config API; • Key: <ClassName>/mp-rest/url (e.g. UserService/mp-rest/url) • Value: the endpoint URL (e.g. http://cruiser:8080/app/user) HASUNUMA Kenji @khasunuma -
Wrap Up: MP Rest Client API • Rich wrapper of
JAX-RS Client • Not alternative way, but like a proxy • Handles HTTP request/response such as invoking Java methods (Type-safe) • Integration with CDI, MP Config, etc. HASUNUMA Kenji @khasunuma