Spring Cloud Consul health check configuration - spring-boot

I'm running a Spring Boot application as a Docker container. This works fine so far, but it's giving me some head aches when trying to use Spring Cloud Consul as well. It reads the configuration from the Consul KVS just fine, but the health checks seem to be acting up.
The default health check uses the hostname of the docker container, for example http://users-microservice/health. Obviously this won't resolve when accessed from Consul.
No problem, the documentation mentions that you can use healthCheckPath in your bootstrap.yml file to configure it. This is what I have now:
spring:
application:
name: users-microservice
cloud:
consul:
host: myserver.com
port: 8500
config:
prefix: API-CONFIG
profileSeparator: '__'
discovery:
tags: users-microservice
healthCheckPath: http://myserver.com:${server.port}/status
healthCheckInterval: 30s
Unfortunately, this variable seems to be used in a very different manner from what I expected. This is what Consul is trying to reach:
Get http://users:18090http//myserver.com:18090/status: dial tcp: unknown port tcp/18090http
How can I fix this? Is there some undocumented configuration parameter that I should set?

Use spring.cloud.consul.discovery.healthCheckUrl=http://myserver.com:${server.port}/status
healthCheckPath only changes the path, not host and port.

Related

Consul share network from docker

There is consul server in docker.
vote-consul-server:
image: consul:1.7.2
environment:
CONSUL_BIND_INTERFACE: eth0
ports:
- "${CONSUL_PORT}:8500"
It generates hosts for registred applications.
The main problem is that anothers applications can't communicate between each other if they are not in docker.
For example: I have config-service (docker with consul), user-service(IDE).
When user-service starts, it asks for configurations from config-server, but consul gives not correct link (available only in docker network).
c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://83a6c7ab12d0:8888/
How to publish all links from consul(docker)?
Since every container has own IP and may have own network, you can try to use host networking:
docker run --rm -d --network host --name vote-consul-server
For more details, see host networking tutorial.
Also, you can learn about networking from the container’s point of view
First of all - need to define IP address configuration in bootstrap.yaml
spring:
...
cloud:
consul:
host: ${CONSUL_HOST}
port: ${CONSUL_PORT}
discovery:
ip-address: ${CONSUL_HOST}
prefer-ip-address: true
The main properties are: ip-address and prefer-ip-address.
If all aplications work in one network - problem doesn't exist.
For example: all services work in docker.

Netflix Eureka Server and Client instance registration issue with Docker swarm

We are trying to deploy Netflix Eureka Server on Docker container which is a Spring Boot application. We are deploying it as a docker service with Docker swarm for container orchestration.
Everything works fine when we directly create the container and run the application.
Eureka server registers all the client application with correct IP addresses.
But when we create and deploy it as docker service it registers the application with the wrong IP address.
We have tried below solution as per spring documentation, updated property values in our configuration files.
spring:
cloud:
inetutils:
ignoredInterfaces:
- docker0
- veth.*
eureka:
instance:
preferIpAddress: true
Tried these solutions as well:
https://github.com/spring-cloud/spring-cloud-netflix/issues/1820
https://github.com/spring-cloud/spring-cloud-netflix/issues/2084
While we run the docker service, the container gets assigned one IP address lets say 172.16.1.3 and service started inside docker gets assigned new IP address 172.16.1.4, because of this issue client application registers its self with container's IP into Eureka server. But it is accessible with 172.16.1.4.
So why running docker as service assigns two IP addresses?
Here Eureka server is running on 10.255.53.172 IP address but as we can see in the second image it shows different IP in Instance Info
Eureka instance is registered with containers IP but it's accessible with its service IP in the network
We use this configuration on production using docker swarm mode:
Eureka server configuration
# Defines the Eureka server that is used by the Netflix OSS components to use as the registry
# for server discovery
eureka:
instance:
hostname: ${HOST_NAME:localhost}
appname: eureka-cluster
# enable to register multiple app instances with a random server port
instance-id: ${spring.application.name}:${random.uuid}
leaseRenewalIntervalInSeconds: 10
leaseExpirationDurationInSeconds: 20
Eureka client configuration
eureka:
client:
registerWithEureka: ${REGISTER_WITH_EUREKA:true}
fetchRegistry: ${FETCH_REGISTRY:false}
serviceUrl:
defaultZone: ${EUREKA_DEFAULT_ZONE:http://localhost:8761/eureka/}
instance:
hostname: ${HOST_NAME:${spring.application.name}} # work on swarm
preferIpAddress: ${PREFER_IP_ADDRESS:false}
# enable to register multiple app instances with a random server port
instance-id: ${spring.application.name}:${random.uuid}
leaseRenewalIntervalInSeconds: ${LEASE_RENEWAL_INTERVAl_IN_SECONDS:10}
leaseExpirationDurationInSeconds: ${LEASE_EXPIRATION_DURATIONIN_SECONDS:20}
ON swarm service definition
rd-service:
image: my-eureka-server-microservice-image
ports:
- "8763:8763"
networks:
- backend
client-service:
image: my-eureka-client-microservice-image
networks:
- backend
networks:
environment:
- "EUREKA_DEFAULT_ZONE=http://rd-service:8763/eureka"
- "REGISTER_WITH_EUREKA=true"
- "FETCH_REGISTRY=true"
networks:
backend:
external: true
Important: Services must be in the same docker overlay network used on eureka server docker service.

Read values from consul while bootstrap spring boot

I have question is there any way to retrieve certain values and inject them to bootstrap.yml while application is coming up.
I have configuration file like this:
spring:
application:
name: myApp
cloud:
consul:
enabled: true
host: localhost
port: 8500
config:
enabled: true
datasource:
url: jdbc:oracle:thin:#localhost:1111:XXXX
username: ${nameOfVariable1}
password: ${nameOfVariable1}
driver-class-name: oracle.jdbc.OracleDriver
For example, I need to configure embedded tomcat port, or DB credentials, I don't want to put it hardcoded in .yml properties file, instead I want to put some variable name in .yml so Spring will go and bring value from Consul. Is it possible?
You can use Spring Cloud Consul Config project that helps to load configuration into the Spring Environment during the special "bootstrap" phase.
3 steps:
add pom dependency: spring-cloud-starter-consul-config
enable consul config: spring.cloud.consul.config.enabled=true
add some config in consul kv in specific folder, such as key: config/testConsulApp/server.port, value:8081
and then start the sample web app, it will listen 8081.
more detail at spring cloud consul doc.
and demo code here

Consul service discovery issue with spring boot applications

According to this blog https://spring.io/blog/2015/07/14/microservices-with-spring which is based on eureka service discovery and where the service discovery is working properly.
But when have switched to use Consul instead Eureka the service discovery is not working and getting this error:
java.lang.IllegalStateException: No instances available for ACCOUNTS-SERVICE
at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:79)
at org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:46) ...
UPDATED: After have fixed the previous error by providing the correct health-check endpoint (see the answer below), when deploying the services to Cloud Foundry with properly provided host and port of the Consul server in bootstrap.yml (Consul based PropertySource loaded during the 'bootstrap' phase):
---
spring:
profiles: cloud
cloud:
consul:
host: <consul host or ip>
port: 8500
Consul is registering the service, but with critical state (failing)!
Would appreciate any help or guidance.
Thanks
The issue was related to the Consul health check default path which is set to the /health endpoint.
Thus after have enabled the spring-actuator in all clients applications (web-server and micro-service) this issue was resolved.
Or you may change the default Consul health-check endpoint in the bootstrap.yml file:
cloud:
consul:
discovery:
healthCheckPath: /test
NB. To enable spring-actuator in maven the following dependency was added to the pom.xml file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
For more information see: http://cloud.spring.io/spring-cloud-consul/spring-cloud-consul.html
When deploying (pushing) to CF (CloudFoundry) the URI of the deployed application should be provided to Consul for service discovery process (CF provides application's URIs in vcap.application.uris environment variable), thus the following configuration should be added to the bootsrap.yml file:
---
spring:
profiles: cloud
cloud:
consul:
host: <consul host or ip>
port: 8500
discovery:
instanceId: ${spring.application.name}:${vcap.application.application_name}:${vcap.application.instance_id}
hostname: ${vcap.application.uris[0]}
port: 80
NB. instanceId is used by Consul to register the application
(microservice) instance.

Token support in spring cloud consul

We are using spring-cloud to read the configuration for our application. We have the similar structure like below in application.yaml
spring:
cloud:
consul:
host: consul_host
port: 8500
We want to enable ACL for consul. So we need to pass consul token to read the configuration by spring.
How can I specify consul token in application.yaml
If you use at least Spring Cloud Brixton M2 (current version is RC1), there is the property spring.cloud.consul.config.acl-token where you can specify the token.
The proper answer is to have token placed in following way:
spring:
cloud:
consul:
host: consul_host
port: 8500
token: your_token
I'm using Spring Boot version: "2.0.4.RELEASE" + "spring-cloud-starter-consul-config"

Resources