Trying to connect Eureka and Zuul and I have error - microservices

I'm trying to study microservices (and docker). Trying to connect Eureka and Zuul and I have error: "com.sun.jersey.api.client.ClientHandlerException: java.net.UnknownHostException: eureka-service"
There's my zuul application.properties
spring.application.name=ZuulService
server.port=8080
eureka.client.service-url.defaultZone=http://eureka-service:9000/eureka
eureka.instance.prefer-ip-address=true
and eureka application.properties
spring.application.name=EurekaService
server.port=9000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.server.wait-time-in-ms-when-sync-empty=0

java.net.UnknownHostException: eureka-service
The eureka-service host cannot be resolved. This host is coming from
eureka.client.service-url.defaultZone=http://eureka-service:9000/eureka
of Zuul-service's application.properties.
Give it the IP address of machine where your eureka-service is running.
If it's on localhost, it would be:
eureka.client.service-url.defaultZone=http://localhost:9000/eureka

Related

HCP Consul with Spring Cloud Consul Discovery - Failing to connect to HCP Consul

I try to connect my spring application to the HCP Consul Discovery Service.
Running consul locally in dev mode works well.
consul agent -dev
application.yml file looks like this:
spring:
cloud:
consul:
host: localhost
port: 8500
However, when I try to connect to HCP Consul I get:
[catalogWatchTaskScheduler-1] org.springframework.cloud.consul.discovery.ConsulCatalogWatch: Error watching Consul CatalogServices
org.apache.http.conn.ConnectTimeoutException: Connect to HOST:8500 [HOST/IP] failed: Connect timed out
This is the configuration I use:
spring:
cloud:
consul:
host: HCP_HOST
port: 8500
scheme: https
HCP Consul is available to public connections.
I manage to see the members using the CLI command
consul members
This is the dependency I use in the pom.xml file
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>3.1.2</version>
</dependency>
What do I miss in my configuration?

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.

Spring cloud Zuul url configuration

I am trying to run an example with zuul and Eureka server.
Below are my configurations:
eureka-service
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF
eureka -client (bootstrap.properties):
spring.application.name=ab
management.server.port=9001
management.server.address: 127.0.0.1
server.port=8081
edge-service(zuul) :
zuul.routes.ab.url=http://localhost:8081
ribbon.eureka.enabled=false
spring.application.name=API-Gateway
eureka.client.eureka-server-port=8761
server.port=8080
While I am hitting the URL http:localhost:8080/clientapi, I get a white label error page, what configuration seems to be missing, as I could see edge-service, as well as eureka-client, registered correctly with the eureka -server.
I am using spring boot: 2.1.4.
Let me show you my example:
eureka in application.yml file:
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: true
instance-info-replication-interval-seconds: 10
server:
eviction-interval-timer-in-ms: 50000
wait-time-in-ms-when-sync-empty: 5
zuul:
zuul.routes.1.url=http://localhost:8081
zuul.routes.2.url=http://localhost:8082
zuul.prefix=/client
zuul.host.max-per-route-connections=10
zuul.host.max-total-connections=10
I have 2 services (1 - localhost:8081 and 2 - localhost:8082)
You need annotate main class Zuul:
#EnableZuulProxy
#EnableDiscoveryClient
Then start application. When you go to
localhost/8070/client/1
you will redirect to first service (localhost:8081)
When you go to
localhost/8070/client/2
I hope it will help you.
you will redirect to second service (localhost:8082)

Spring Cloud Consul health check configuration

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.

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.

Resources