Showing posts with label OpenLiberty. Show all posts
Showing posts with label OpenLiberty. Show all posts

Saturday, May 25, 2024

Java Microprofile GraphQL client using Open Liberty

 HOWTO

 GitHub

 


 

Build client 

[INFO] -------------< io.openliberty.guides:guide-graphql-intro >--------------
[INFO] Building guide-graphql-intro 1.0-SNAPSHOT                          [5/5]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for guide-graphql-intro 1.0-SNAPSHOT:
[INFO] 
[INFO] guide-graphql-client-models ........................ SUCCESS [  0.786 s]
[INFO] guide-graphql-client-query ......................... SUCCESS [  1.080 s]
[INFO] guide-graphql-client-graphql ....................... SUCCESS [  0.252 s]
[INFO] guide-graphql-client-system ........................ SUCCESS [  0.189 s]
[INFO] guide-graphql-intro ................................ SUCCESS [  0.001 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

 

Access client http://localhost:9084/openapi/ui/


 

Run query 


 

Java Microprofile GraphQL server using Open Liberty

 HOWTO

 

GitHub

 


 

Build models 

$ mvn -pl models install
[INFO] Scanning for projects...
[INFO] 
[INFO] ------< io.openliberty.guides:guide-microprofile-graphql-models >-------
[INFO] Building guide-microprofile-graphql-models 1.0-SNAPSHOT

[INFO] Installing /git/guide-microprofile-graphql/finish/models/target/guide-microprofile-graphql-models-1.0-SNAPSHOT.jar to /home/dave/.m2/repository/io/openliberty/guides/guide-microprofile-graphql-models/1.0-SNAPSHOT/guide-microprofile-graphql-models-1.0-SNAPSHOT.jar
[INFO] Installing /git/guide-microprofile-graphql/finish/models/pom.xml to /home/dave/.m2/repository/io/openliberty/guides/guide-microprofile-graphql-models/1.0-SNAPSHOT/guide-microprofile-graphql-models-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

 

Package WAR


$ mvn package


[INFO] Reactor Summary for guide-graphql-intro 1.0-SNAPSHOT:
[INFO] 
[INFO] guide-microprofile-graphql-models .................. SUCCESS [  0.794 s]
[INFO] guide-microprofile-graphql-system .................. SUCCESS [ 10.233 s]
[INFO] guide-microprofile-graphql-graphql ................. SUCCESS [  0.254 s]
[INFO] guide-graphql-intro ................................ SUCCESS [  0.001 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

 

Enable GraphQL UI  in server.xml

<server description="GraphQL service">
    <featureManager>
        <feature>restfulWS-3.1</feature>
        <feature>jsonb-3.0</feature>
        <feature>jsonp-2.1</feature>
        <feature>cdi-4.0</feature>
        <feature>mpConfig-3.1</feature>
        <feature>mpRestClient-3.0</feature>
        <feature>mpGraphQL-2.0</feature>
    </featureManager>

    <variable name="http.port" defaultValue="9082"/>
    <variable name="https.port" defaultValue="9445"/>

    <variable name="io.openliberty.enableGraphQLUI" value="true" />

    <webApplication location="guide-microprofile-graphql-graphql.war" contextRoot="/" />
    <httpEndpoint host="*" httpPort="${http.port}" 
        httpsPort="${https.port}" id="defaultHttpEndpoint"/>
</server>

 

GraphQL WAR

$ jar tvf ./graphql/target/guide-microprofile-graphql-graphql.war
     0 Sat May 25 07:38:34 CEST 2024 META-INF/
    81 Sat May 25 07:38:34 CEST 2024 META-INF/MANIFEST.MF
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/graphql/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/graphql/client/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/lib/
   433 Sat May 25 07:27:24 CEST 2024 WEB-INF/web.xml
  6366 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/graphql/GraphQLService.class
  1415 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/graphql/client/SystemClient.class
   568 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/graphql/client/UnknownUriException.class
  2268 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/graphql/client/UnknownUriExceptionMapper.class
  6693 Sat May 25 07:37:56 CEST 2024 WEB-INF/lib/guide-microprofile-graphql-models-1.0-SNAPSHOT.jar
 15086 Sat May 25 07:27:24 CEST 2024 favicon.ico
  3150 Sat May 25 07:27:24 CEST 2024 index.html
  4503 Sat May 25 07:27:24 CEST 2024 META-INF/maven/io.openliberty.guides/guide-microprofile-graphql-graphql/pom.xml
    97 Sat May 25 07:38:34 CEST 2024 META-INF/maven/io.openliberty.guides/guide-microprofile-graphql-graphql/pom.properties

 

SystemWAR

dave@fedora:/git/guide-microprofile-graphql/finish$ jar tvf ./system/target/guide-microprofile-graphql-system.war
     0 Sat May 25 07:38:34 CEST 2024 META-INF/
    81 Sat May 25 07:38:34 CEST 2024 META-INF/MANIFEST.MF
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/classes/io/openliberty/guides/system/
     0 Sat May 25 07:38:34 CEST 2024 WEB-INF/lib/
   432 Sat May 25 07:27:24 CEST 2024 WEB-INF/web.xml
  1942 Sat May 25 07:38:30 CEST 2024 WEB-INF/classes/io/openliberty/guides/system/SystemPropertiesResource.class
   444 Sat May 25 07:38:30 CEST 2024 WEB-INF/classes/io/openliberty/guides/system/SystemApplication.class
  2398 Sat May 25 07:38:30 CEST 2024 WEB-INF/classes/io/openliberty/guides/system/SystemMetricsResource.class
  6693 Sat May 25 07:37:56 CEST 2024 WEB-INF/lib/guide-microprofile-graphql-models-1.0-SNAPSHOT.jar
  2461 Sat May 25 07:27:24 CEST 2024 index.html
  4007 Sat May 25 07:27:24 CEST 2024 META-INF/maven/io.openliberty.guides/guide-microprofile-graphql-system/pom.xml
    96 Sat May 25 07:38:34 CEST 2024 META-INF/maven/io.openliberty.guides/guide-microprofile-graphql-system/pom.properties

 

Build Docker images

dave@fedora:/git/guide-microprofile-graphql/finish$ docker images
REPOSITORY                    TAG                   IMAGE ID       CREATED          SIZE
graphql                       1.0-SNAPSHOT          727f633aa833   19 seconds ago   753MB
system                        1.0-java17-SNAPSHOT   6812f8c9c7c0   45 seconds ago   765MB
system                        1.0-java11-SNAPSHOT   915f228505ad   2 minutes ago    747MB

 Start containers 

$ ./scripts/startContainers.sh
fec411c8ccc1e46a593b48399907c93284a2b4b2db82b027d876fd53b1bbf10d
53de0a3383d827a43f6bafe185143f92f885a8a99d36e9c60bb167730399b44e
d175e2f72bfd024c4179659893e92dd566b1120948de122fd7d714d668e403cc
1b7b58a43e02e84c750137e416e55b800b36acac4d6f0c481ccde5b184ef6d8f
dave@fedora:/git/guide-microprofile-graphql/finish$ docker ps 
CONTAINER ID   IMAGE                        COMMAND                  CREATED         STATUS         PORTS                                                           NAMES
d175e2f72bfd   graphql:1.0-SNAPSHOT         "/opt/ol/helpers/run…"   7 seconds ago   Up 6 seconds   9080/tcp, 9443/tcp, 0.0.0.0:9082->9082/tcp, :::9082->9082/tcp   graphql
53de0a3383d8   system:1.0-java17-SNAPSHOT   "/opt/ol/helpers/run…"   7 seconds ago   Up 6 seconds   9080/tcp, 9443/tcp                                              system-java17
1b7b58a43e02   system:1.0-java11-SNAPSHOT   "/opt/ol/helpers/run…"   7 seconds ago   Up 6 seconds   9080/tcp, 9443/tcp                                              system-java11

Call GraphQL endpoint http://localhost:9082/graphql/schema.graphql

"Mutation root"
type Mutation {
  "Changes the note set for the system"
  editNote(hostname: String, note: String): Boolean!
}

"Query root"
type Query {
  "Gets information about the system"
  system(hostname: String): system!
  "Gets system load data from the systems"
  systemLoad(hostnames: [String]): [systemLoad]
}

"Information about a Java installation"
type java {
  vendorName: String
  version: String!
}

"System usage data"
type loadData {
  heapUsed: BigInteger!
  loadAverage: Float!
  nonHeapUsed: BigInteger!
}

"Information about a single system"
type system {
  hostname: String!
  java: java!
  note: String
  osArch: String
  osName: String
  osVersion: String
  systemMetrics: systemMetrics!
  username: String!
}

"Information of system usage"
type systemLoad {
  hostname: String!
  loadData: loadData
}

"System metrics"
type systemMetrics {
  heapSize: BigInteger!
  nonHeapSize: BigInteger!
  processors: Int!
}

Access GraphiQL at the http://localhost:9082/graphql-ui 

 

Query structure

# An example GraphQL query might look like:
#
#     {
#       field(arg: "value") {
#         subField
#       }
#     }

 

Query

query {
  system(hostname: "system-java11") {
    hostname
    username
    osArch
    osName
    osVersion
    systemMetrics {
      processors
      heapSize
      nonHeapSize
    }
    java {
      vendorName
      version
    }
  }
}


Response

{
  "data": {
    "system": {
      "hostname": "system-java11",
      "username": "default",
      "osArch": "amd64",
      "osName": "Linux",
      "osVersion": "6.8.10-200.fc39.x86_64",
      "systemMetrics": {
        "processors": 12,
        "heapSize": 4110352384,
        "nonHeapSize": -1
      },
      "java": {
        "vendorName": "IBM Corporation",
        "version": "11.0.22"
      }
    }
  }
}


Friday, October 26, 2018

Using Docker containers to develop microservices



Adam Bien - Thin WARs, Java EE 7, Docker and Productivity
https://www.youtube.com/watch?v=5N4EUDhrkec

Adam Bien - samples
https://github.com/AdamBien/docklands


OpenLiberty MicroService Docker guide
https://openliberty.io/guides/docker.html

GitHub repo https://github.com/OpenLiberty/guide-docker

  mvn clean install


[INFO] CWWKM2102I: Using artifact based assembly archive : io.openliberty:openliberty-runtime:null:RELEASE:zip.
[INFO] CWWKM2102I: Using installDirectory : /git/guide-docker/finish/target/liberty/wlp.
[INFO] CWWKM2102I: Using serverName : defaultServer.
[INFO] CWWKM2102I: Using serverDirectory : /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer.
[INFO] CWWKM2185I: The liberty-maven-plugin configuration parameter "appsDirectory" value defaults to "dropins".
[INFO] Installing assembly...
[INFO] Expanding: /home/centos/.m2/io/openliberty/openliberty-runtime/18.0.0.3/openliberty-runtime-18.0.0.3.zip into /git/guide-docker/finish/target/liberty
[INFO]
[INFO] --- liberty-maven-plugin:2.6.1:create-server (create-server) @ rest ---
[INFO] CWWKM2102I: Using artifact based assembly archive : io.openliberty:openliberty-runtime:null:RELEASE:zip.
[INFO] CWWKM2102I: Using installDirectory : /git/guide-docker/finish/target/liberty/wlp.
[INFO] CWWKM2102I: Using serverName : defaultServer.
[INFO] CWWKM2102I: Using serverDirectory : /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer.
[INFO] CWWKM2185I: The liberty-maven-plugin configuration parameter "appsDirectory" value defaults to "dropins".
[INFO] CWWKM2112I: Re-using previously installed assembly.
[INFO] CWWKM2143I: Server defaultServer does not exist. Now creating...
[INFO] CWWKM2001I: server.config.dir is /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer.
[INFO] CWWKM2001I: server.output.dir is /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer.
[INFO] CWWKM2001I: Invoke command is [/git/guide-docker/finish/target/liberty/wlp/bin/server, create, defaultServer].
[INFO] Server defaultServer created.
[INFO] CWWKM2129I: Server defaultServer has been created at /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer.
[INFO] Copying 1 file to /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer
[INFO] CWWKM2144I: Update server configuration file server.xml from /git/guide-docker/finish/src/main/liberty/config/server.xml.
[INFO] CWWKM2144I: Update server configuration file bootstrap.properties from inlined configuration.
[INFO]
[INFO] --- maven-war-plugin:3.2.2:war (default-war) @ rest ---
[INFO] Packaging webapp
[INFO] Assembling webapp [rest] in [/git/guide-docker/finish/target/rest-1.0-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [/git/guide-docker/finish/src/main/webapp]
[INFO] Webapp assembled in [47 msecs]
[INFO] Building war: /git/guide-docker/finish/target/rest-1.0-SNAPSHOT.war
[INFO]
[INFO] --- liberty-maven-plugin:2.6.1:install-apps (install-app) @ rest ---
[INFO] CWWKM2102I: Using artifact based assembly archive : io.openliberty:openliberty-runtime:null:RELEASE:zip.
[INFO] CWWKM2102I: Using installDirectory : /git/guide-docker/finish/target/liberty/wlp.
[INFO] CWWKM2102I: Using serverName : defaultServer.
[INFO] CWWKM2102I: Using serverDirectory : /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer.
[INFO] Copying 1 file to /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer
[INFO] CWWKM2144I: Update server configuration file server.xml from /git/guide-docker/finish/src/main/liberty/config/server.xml.
[INFO] CWWKM2144I: Update server configuration file bootstrap.properties from inlined configuration.
[INFO] CWWKM2160I: Installing application /git/guide-docker/finish/target/rest-1.0-SNAPSHOT.war.
[INFO] Copying 1 file to /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer/apps
[INFO]
[INFO] --- liberty-maven-plugin:2.6.1:test-start-server (start-server) @ rest ---
[INFO] CWWKM2102I: Using artifact based assembly archive : io.openliberty:openliberty-runtime:null:RELEASE:zip.
[INFO] CWWKM2102I: Using installDirectory : /git/guide-docker/finish/target/liberty/wlp.
[INFO] CWWKM2102I: Using serverName : defaultServer.
[INFO] CWWKM2102I: Using serverDirectory : /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer.
[INFO] CWWKM2112I: Re-using previously installed assembly.
[INFO] Copying 1 file to /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer
[INFO] CWWKM2144I: Update server configuration file server.xml from /git/guide-docker/finish/src/main/liberty/config/server.xml.
[INFO] CWWKM2144I: Update server configuration file bootstrap.properties from inlined configuration.
[INFO] CWWKM2001I: server.config.dir is /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer.
[INFO] CWWKM2001I: server.output.dir is /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer.
[INFO] CWWKM2001I: Invoke command is [/git/guide-docker/finish/target/liberty/wlp/bin/server, start, defaultServer].
[INFO] Starting server defaultServer.
[INFO] Server defaultServer started with process ID 20700.
[INFO] Waiting up to 30 seconds for server confirmation:  CWWKF0011I to be found in /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer/logs/messages.log
[INFO] CWWKM2010I: Searching for CWWKF0011I in /git/guide-docker/finish/target/liberty/wlp/usr/servers/defaultServer/logs/messages.log. This search will timeout after 30 seconds.
[INFO] CWWKM2015I: Match number: 1 is [10/26/18 13:21:13:991 CEST] 00000019 com.ibm.ws.kernel.feature.internal.FeatureManager            A CWWKF0011I: The server defaultServer is ready to run a smarter planet..



 yum install docker

sudo systemctl start docker
 
Configure Docker proxy 
 https://docs.docker.com/config/daemon/systemd/#httphttps-proxy

docker build -t ol-runtime .


docker build -t ol-runtime .
Sending build context to Docker daemon 150.5 MB
Step 1/4 : FROM open-liberty
Trying to pull repository docker.io/library/open-liberty ...
latest: Pulling from docker.io/library/open-liberty
18d680d61657: Pull complete
0addb6fece63: Pull complete
78e58219b215: Pull complete
eb6959a66df2: Pull complete
d28276d78514: Pull complete
f4b7f9cac3ff: Pull complete
cca6875781ba: Pull complete
19927b6c1502: Pull complete
e7b4b52f440a: Pull complete
b756387f6847: Pull complete
Digest: sha256:ed0252357b07d199c18fa67eac13a563cef9d052c856f7050727775bf2dc3e2b
Status: Downloaded newer image for docker.io/open-liberty:latest
 ---> 2408c7bfb52d
Step 2/4 : RUN ln -s /opt/ol/wlp/usr/servers /servers
 ---> Running in ee3238c49602

 ---> b99f07819d6b
Removing intermediate container ee3238c49602
Step 3/4 : ENTRYPOINT /opt/ol/wlp/bin/server run
 ---> Running in eb0103bfe0a0
 ---> 5be83f0c9bcd
Removing intermediate container eb0103bfe0a0
Step 4/4 : CMD defaultServer
 ---> Running in 79167390de6a
 ---> 026f9cdf87b7
Removing intermediate container 79167390de6a
Successfully built 026f9cdf87b7


Run container

$ docker run -d --name rest-app -p 9080:9080 -p 9443:9443 -v /git/guide-maven-intro/finish/target/liberty/wlp/usr/servers:/servers ol-runtime
6177bbf6b6626c4004878910f97bf2581d7f16cc4aca46550685e65b06a1771b



 docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
6177bbf6b662        ol-runtime          "/opt/ol/wlp/bin/s..."   54 seconds ago      Exited (23) 52 seconds ago                       rest-app