How to register a service using Spring Cloud Consul - spring

I have registered a service using Spring Cloud Consul, but for this I had to run a Consul local agent which establishes a channel communication to Consul server node (running as bootstrap).
For example:
#Server
consul agent -server -bootstrap -bind -data-dir data -ui-dir web_ui
#Desktop
consul agent -data-dir consul -ui-dir consul/dist -join server_ip_address
Is there any way to avoid of having this local agent in my desktop, I mean from my desktop Spring Cloud Consul would register the service to server node?
An example of this is what Netflix Eureka client does with Netflix Eureka server, no external agents running in machines to bind services names.

you have to use property file or yaml file i just give sample yaml file
application.yml
# Configure this Discovery Server
spring:
cloud:
consul:
host: localhost
port: 8500
bootstrap.yml
spring:
application:
name: service-name

Related

Eureka Client: Eureka Registry server address alternative to 'localhost'

Eureka-server(MediaStoreEurekaServer): bootstrap.properties
spring.application.name=MediaStoreEurekaServer
server.port=9091
eureka.client.serviceUrl.defaultZone=http://localhost:9091/eureka}
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Eureka-client(Config-Server): bootstrap.properties
spring.application.name=Config-Server
server.port=9093
user.home=${HOME}
spring.cloud.config.server.native.search-locations=file://${user.home}/IdeaProjects/MediaStore/centralProperties
spring.profiles.active=native
eureka.client.serviceUrl.defaultZone=http://localhost:9091/eureka
With above 2 properties (services running on same machine), Config-Server is able to register with MediaStoreEurekaServer eureka registry server when using http://localhost:9091/eureka.
I don't want to use localhost. When I use http://MediaStoreEurekaServer:9091/eureka , Config-Server is not able to register.
Can we use something other than localhost for eureka client to find the eureka server like service or instance name? when services are running on same host
Edit 1:
This is what eureka client spring logs give when in eureka client properties file i set
eureka.client.serviceUrl.defaultZone=http://MediaStoreEurekaServer:9091/eureka
Error:
2020-05-22 20:01:02.137 ERROR 22846 --- [nfoReplicator-0] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://MediaStoreEurekaServer:9091/eureka/}
com.sun.jersey.api.client.ClientHandlerException: java.net.UnknownHostException: MediaStoreEurekaServer```

How to run spring boot application multiple instance when get resource from config server?

I have Eureka server, Eureka zuul and config server.
So I write a simple microservice. Then, running on 8686 port.
So I want to run that microservice on another port.
I trying that command. But don't work.
java -Dserver.port=8687 -jar -Dlogging.file="bla.log" testMicro.jar --debug > "bla.log"&
I am confusing. Help me!
You have two ways to running your instances on different ports.
user assignment of random port from a specified range:
server:
port: ${random.int(8080,8090)}
Set in property file from config server for testMicro microservice the following configurations:
spring:
cloud:
config:
override-system-properties: false
allow-override: true
override-none: true
and then run again your jar with -Dserver.port=8687 property

Access Docker Containers Host by DNS name

From my Spring Boot application deployed as a Docker container I need to be able to access the parent node (within a Swarm) by some kind of docker-exposed DNS name so that I can add it to a configuration file and was wondering if there was a DNS name exposed automatically by Docker for this purpose?
Another container that is scheduled on the same host running in "network_mode: host" is running Consul and advertising on port 8500 (real IP is 192.168.1.233).
If I run netstat on the Swarm node (ip 192.168.1.233) I can see it's listening on port 8500:
# netstat -anp | grep 8500
tcp6 0 0 :::8500 :::* LISTEN 25010/docker-proxy
I want to be able to define a connection string as configuration in the Spring Boot app on the swarm node to the local instance of Consul sheduled on the same physical host as the Spring Boot app.
If I refer to "localhost" within the Spring Boot container then there's nothing listening on the 8500 port, proven by running this after shelling into the Spring Boot container (using a sample Consul API call)
# wget http://localhost:8500/v1/status/leader
Connecting to localhost:8500 (127.0.0.1:8500)
wget: can't connect to remote host (127.0.0.1): Connection refused
I've also tried using "host.docker.internal" but that doesn't work:
# ping host.docker.internal
ping: bad address 'host.docker.internal'
The hosts are all Centos 7 hosts, and Docker version 18.09.1, build 4c52b90.
The firewalld service is disabled on all hosts.

How to config consul cluster in spring?

If there is only one consul node, it can config consul like following:
spring:
cloud:
consul:
host: localhost
port: 8500
But if there is consul cluster, for example there are three concul nodes.
in this case, how to config the consul?
Do it need DNS to route the host name to multi IP address?
you should run a consul agent in client mode on a node and join consul cluster.
then run your spring boot instance on this node and connect to this consul agent.

Kubernetes networking, How to transfer a variable to container

I have a K8s, currently running in single node (master+kubelet,172.16.100.81). I have an config server image which I will run it in pod. The image is talking to another pod named eureka server. Both two images are spring boot application. And eureka server's http address and port is defined by me. I need to transfer eureka server's http address and port to config pod so that it could talk to eureka server.
I start eureka server: ( pesudo code)
kubectl run eureka-server --image=eureka-server-image --port=8761
kubectl expose deployment eureka-server --type NodePort:31000
Then I use command "docker pull" to download config server image and run it as below:
kubectl run config-server --image=config-server-image --port=8888
kubectl expose deployment config-server --type NodePort:31001
With these steps, I did not find the way to transfer eureka-server http
server (master IP address 172.16.100.81:31000) to config server, are there
methods I could transer variable eureka-server=172.16.100.81:31000 to Pod config server? I know I shall use ingress in K8s networking, but currently I use NodePort.
Generally, you don't need nodePort when you want two pods to communicate with each other. A simpler clusterIP is enough.
Whenever you are exposing a deployment with a service, it will be internally discoverable from the DNS. Both of your exposed services can be accessed using:
http://config-server.default:31001 and http://eureka-server.default:31000. default is the namespace here.
172.16.100.81:31000 will make it accessible from outside the cluster.

Resources