IBMStockTrader / portfolio

Microservice implemented with MicroProfile that persists stock portfolios to JDBC (such as DB2)
Apache License 2.0
7 stars 60 forks source link

This microservice manages a stock portfolio. The data is backed by two relational database (such as DB2, PostgreSQL or MS SQL Server) tables, communicated with via JDBC. The following operations are available:

GET / - gets summary data for all portfolios.

POST /{owner} - creates a new portfolio for the specified owner.

GET /{owner} - gets details for the specified owner.

PUT /{owner} - updates the portfolio for the specified owner (by adding a stock).

DELETE /{owner} - removes the portfolio for the specified owner.

GET /{owner}/returns - gets the return on investment for this portfolio.

POST /{owner}/feedback - submits feedback (to the Watson Tone Analyzer)

All operations return JSON. A portfolio object contains fields named owner, total, loyalty, balance, commissions, free, sentiment, and nextCommission, plus an array of stocks. A stock object contains fields named symbol, shares, commission, price, total, and date. The only operation that takes any query params is the PUT operation, which expects params named symbol and shares. Also, the feedback operation takes a JSON object in the http body, with a single field named text.

For example, doing a PUT http://localhost:9080/portfolio/John?symbol=IBM&shares=123 (against a freshly created portfolio for John) would return JSON like {"owner": "John", "total": 19120.35, "loyalty": "Bronze", "balance": 40.01, "commissions": 9.99, "free": 0, "sentiment": "Unknown", "nextCommission": 8.99, "stocks": [{"symbol": "IBM", "shares": 123, "commission": 9.99, "price": 155.45, "total": 19120.35, "date": "2017-06-26"}]}.

The above REST call would also add a row to the Stocks table via a SQL statement like INSERT INTO Stock (owner, symbol, shares, price, total, dateQuoted) VALUES ('John', 'IBM', 123, 155.45, 19120.35, '2017-06-26'), and would update the corresponding row in the Portfolio table via a SQL statement like UPDATE Portfolio SET total = 19120.35, loyalty = 'Bronze' WHERE owner = 'John'.

The code should work with any JDBC provider. It has been tested with DB2, PostgreSQL, MS SQL Server, and with Derby. Changing providers simply means updating the Dockerfile to copy the JDBC jar file into the Docker image, and updating the server.xml to reference it and specify any database-specific settings. No Java code changes are necessary when changing JDBC providers. The database can either be another pod in the same Kubernetes environment, or running on "bare metal" in a traditional on-premises environment, or, preferably, it could be a database-as-a-service in your preferred hyperscaler. Endpoint and credential info is specified in the Kubernetes secret and made available as environment variables to the server.xml of Open Liberty. See the manifests/portfolio-values.yaml for details.

Prerequisites for ICP Deployment

This project requires two secrets: jwt and db2. You can get the DB2 values from inspecting your DB2 secrets.

  kubectl create secret generic jwt -n stock-trader --from-literal=audience=stock-trader --from-literal=issuer=http://stock-trader.ibm.com

  kubectl create secret generic db2 --from-literal=id=<DB2_USERNAME> --from-literal=pwd=<DB2_PASSWORD> --from-literal=host=<DB2_SVC_NAME> --from-literal=port=50000 --from-literal=db=<TRADER_DB_NAME>

  # Example db2:
  kubectl create secret generic db2 --from-literal=id=db2inst1 --from-literal=pwd=db2inst1 --from-literal=host=trader-ibm-db2oltp-dev --from-literal=port=50000 --from-literal=db=trader

You'll also need to enable login to the IBM Cloud Private internal Docker registry by following [these steps] (https://www.ibm.com/support/knowledgecenter/en/SSBS6K_2.1.0/manage_images/configuring_docker_cli.html). Don't forget to restart Docker after adding your cert. On macOS you can restart Docker by running:

  osascript -e 'quit app "Docker"'
  open -a Docker

Build and Deploy to ICP

To build portfolio clone this repo and run:

mvn package
docker build -t portfolio:latest -t <ICP_CLUSTER>.icp:8500/stock-trader/portfolio:latest .
docker tag portfolio:latest <ICP_CLUSTER>.icp:8500/stock-trader/portfolio:latest
docker push <ICP_CLUSTER>.icp:8500/stock-trader/portfolio:latest

Use the Open Liberty helm chart to deploy the Portfolio microservice to ICP:

helm repo add ibm-charts https://raw.githubusercontent.com/IBM/charts/master/repo/stable/
helm install ibm-charts/ibm-websphere-liberty -f <VALUES_YAML> -n <RELEASE_NAME> --tls

In practice this means you'll run something like:

docker build -t portfolio:latest -t mycluster.icp:8500/stock-trader/portfolio:latest .
docker tag portfolio:latest mycluster.icp:8500/stock-trader/portfolio:latest
docker push mycluster.icp:8500/stock-trader/portfolio:latest

helm repo add ibm-charts https://raw.githubusercontent.com/IBM/charts/master/repo/stable/
helm install ibm-charts/ibm-websphere-liberty -f manifests/portfolio-values.yaml -n portfolio --namespace stock-trader --tls

Note that nowadays, we tend to use the operator, in the sibling stocktrader-operator repository, to deploy the entire Stock Trader application as a whole (to AKS, EKS, GCP, IKS, OCP, or TKG), instead of deploying the microservices one by one. See the readme in the operator repo for more details.