August 31, 2022 By Isaac Eichelberger 3 min read

How to enable a microservice architecture Code Engine by deploying a Spring Cloud Eureka Registry Server, a Zuul API Gateway and your own microservices.

This blog post details the ease of deploying a Eureka Registry Server to enable Service Discovery and a Netflix Zuul API Gateway, which routes to any additional microservices you want to deploy. This allows the microservices to only accept traffic through the API gateway. An example of this architecture is displayed below:

Before you begin

Deploying the Registry Server

First, select your Code Engine project:

ibmcloud ce project select --name <Name>

Next, create your application. You will likely always want at least one instance of your registry to be available so your other services can register to it when needed. The port option should match the port specified in your Eureka application’s application.yml file. If the port is 8080, the flag is not needed because Code Engine’s default port is 8080:

ibmcloud ce application create --src . --min-scale 1 --port <port> --name <name> --visibility <visibility>

Note that the visibility is public by default, which means the project will be accessible via the internet. If you would like the application to only be accessible from other Code Engine applications or a private network, you can set it to private.

Once the registry is deployed, you need to take note of the URL to which that registry is

assigned. You will need to add this as an environment variable when deploying the gateway and microservice applications. This URL should look something like this: https://(application name).(randomncharacters).(region).codeengine.appdomain.cloud/

You can find this url using ibmcloud ce application get -n <name>.

Similarly, if you would like to deploy from a prebuilt image, you will need to use the following command, which replaces the  --src tag and adds a registry access secret. If you haven’t created a registry access secret before, take a look at “Accessing Container Registries”:

ibmcloud ce app create --name <name> --image us.icr.io/<your namespace>/<your image name>:latest --registry-secret <your registry secret> --min-scale 1 --port <port>

Deploying the API gateway

Next, create your gateway application. In Eureka applications, the defaultZone field in the application.yml tells the application where to look for the Registry. We can either update this value to be the URL we got from our application above, or we can create an environment variable to overwrite the value. If you choose to create an environment variable, it depends on the version of SpringBoot you are using.

  • Springboot Version < 2.4.5: EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=<registry url>/eureka
  • Springboot Version > 2.4.5: SPRING_APPLICATION_JSON={“eureka”:{“client”:{“serviceUrl”:{“defaultZone”:”<registry url>/eureka”}}}}

If you are deploying from source code, the application create command should be as follows:

ibmcloud ce application create --src . --env <your env> --port <port> --name <name>

Conversely, if you are using a prebuilt image, you can use the following command:

ibmcloud ce app create --name <name> --image us.icr.io/<your namespace>/<your image name>:latest --registry-secret <your registry secret> --env <env variable> --port <port>

Deploying your microservice(s)

The process for deploying a microservice is like what was shown above for the gateway and can be repeated for as many microservices as you have. As shown above, the defaultZone needs to be overwritten in the application.yml file. The environment variable to select is shown below:

  • Springboot Version < 2.4.5: EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=<registry url>/eureka
  • Springboot Version > 2.4.5: SPRING_APPLICATION_JSON={“eureka”:{“client”:{“serviceUrl”:{“defaultZone”:”<registry url>/eureka”}}}}

Additionally, to keep the microservice from having its own URL, so it only accepts requests through the API gateway, the application create command needs to have the --cluster-local flag. This flag makes the application only visible to other applications within the same Code Engine project.

If you are deploying from source, the application create command should be as follows:

ibmcloud ce application create --src . --env <your env> --cluster-local --port <port> --name <name>

If you are deploying from an image, use this command:

ibmcloud ce application create --name <name> --image us.icr.io/<your namespace>/<your image name>:latest --registry-secret <your registry secret> --env <your env> --cluster-local --port <port>

Once your microservice has finished deploying, you can launch your gateway application and test your routing.

Conclusion

Following the steps in this post, you learned how to deploy an API gateway and microservice architecture on IBM Cloud Code Engine

Along with the IBM Cloud CLI, you can also use the IBM Cloud Console to achieve what’s shown above.

If you have any questions, feel free to reach out to me on LinkedIn.

Was this article helpful?
YesNo

More from Cloud

Enhance your data security posture with a no-code approach to application-level encryption

4 min read - Data is the lifeblood of every organization. As your organization’s data footprint expands across the clouds and between your own business lines to drive value, it is essential to secure data at all stages of the cloud adoption and throughout the data lifecycle. While there are different mechanisms available to encrypt data throughout its lifecycle (in transit, at rest and in use), application-level encryption (ALE) provides an additional layer of protection by encrypting data at its source. ALE can enhance…

Attention new clients: exciting financial incentives for VMware Cloud Foundation on IBM Cloud

4 min read - New client specials: Get up to 50% off when you commit to a 1- or 3-year term contract on new VCF-as-a-Service offerings, plus an additional value of up to USD 200K in credits through 30 June 2025 when you migrate your VMware workloads to IBM Cloud®.1 Low starting prices: On-demand VCF-as-a-Service deployments begin under USD 200 per month.2 The IBM Cloud benefit: See the potential for a 201%3 return on investment (ROI) over 3 years with reduced downtime, cost and…

The history of the central processing unit (CPU)

10 min read - The central processing unit (CPU) is the computer’s brain. It handles the assignment and processing of tasks, in addition to functions that make a computer run. There’s no way to overstate the importance of the CPU to computing. Virtually all computer systems contain, at the least, some type of basic CPU. Regardless of whether they’re used in personal computers (PCs), laptops, tablets, smartphones or even in supercomputers whose output is so strong it must be measured in floating-point operations per…

IBM Newsletters

Get our newsletters and topic updates that deliver the latest thought leadership and insights on emerging trends.
Subscribe now More newsletters