'Getting null pointer exception after deployment in GCP
I have a .NET application divided in microservices. One of the microservices is the orchestrator and authenticaticator: this microservice communicates to 3 other microservices and the intercommunication is done via REST.
The code implemented is fully functional locally and each microservice was also containarized with docker and tested locally (also bug free).
The problems arise when we try to test our services in GCP: the error is always the same, being it a null pointer exception, as can be seen in the image below:
We have defined environment variables in the code that match with the
.yml
file which is used to make the deployment.
This is the .yml
that we are using:
apiVersion: apps/v1
kind: Deployment
metadata:
name: orchauth
namespace: default
labels:
app: orchauth
spec:
strategy:
type: Recreate
replicas: 2
selector:
matchLabels:
app: orchauth
template:
metadata:
labels:
app: orchauth
spec:
serviceAccountName: default
containers:
- name: orchauth
image: "eu.gcr.io/boomoseries-api/orchmicroservice"
ports:
- containerPort: 80
env:
- name: SEARCH_HOST
value: "http://search.default"
- name: USERS_HOST
value: "http://users.default"
- name: PREFS_HOST
value: "http://prefs.default"
---
apiVersion: v1
kind: Service
metadata:
name: orchauth
spec:
selector:
app: orchauth
ports:
- name: http
protocol: TCP
port: 80
targetPort: 80
The other microservices are implemented similarly in the file. We also defined the microservices environment variables in the code, correctly:
private static readonly string microservicesBaseURL = Environment.GetEnvironmentVariable("SEARCH_HOST");
private static readonly string microservicesBaseURL = Environment.GetEnvironmentVariable("USERS_HOST");
private static readonly string microservicesBaseURL = Environment.GetEnvironmentVariable("PREFS_HOST");
This is the ingress .yml
file:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: boomoseries-ingress
spec:
ingressClassName: nginx
defaultBackend:
service:
name: orchauth
port:
number: 80
The ingress communicates with the microservice OrchAuth, which is the one giving the error.
The error occurs in the request: from my understanding, the request
variable is null and the null pointer exception occurs here. But I can't understand why, since locally and containarized the service works with no issues. The code is presented below (the class SearchRESTComunicationServiceBooks
):
public async Task<List<BookDTO>> ObtainBooksByRating(string type, double minRating)
{
List<BookDTO> booksDtos = new();
//Makes the requests to different microservices
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var request = httpClient.GetAsync(microservicesBaseURL + "?type=" + type + "&minRating=" + minRating);
stopwatch.Stop();
System.Diagnostics.Debug.WriteLine(stopwatch.ElapsedMilliseconds);
//Get the responses
var response = request.Result;
var responseString = await response.Content.ReadAsStringAsync();
List<BookDTO> deserializedBook = JsonConvert.DeserializeObject<List<BookDTO>>(responseString);
foreach (var item in deserializedBook)
{
booksDtos.Add(item);
}
return booksDtos;
Note: I am fully aware that the code has bad implementation practices, but that is not the main concern. Thank you for the time reading.
Solution 1:[1]
So the problem was the fact that in my .yml file I was only configuring my services to use the port 80 (http). In my startup, I was using Hsts, meaning that there would always be a redirection to the https port (which was configured to 443). After adding this port to the .yml file, the problem was solved.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Miguel Ferreira |