47

I am very new to the microservices and trying to run the code from link: https://dzone.com/articles/advanced-microservices-security-with-spring-and-oa . When I simply run the code I see the following error comes.

What is the issue ?

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
    at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:111) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:1030) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.java:944) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.DiscoveryClient.refreshRegistry(DiscoveryClient.java:1468) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.DiscoveryClient$CacheRefreshThread.run(DiscoveryClient.java:1435) [eureka-client-1.4.12.jar:1.4.12]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [na:1.8.0_144]
    at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_144]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_144]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_144]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_144]

2017-09-09 18:53:11.909 ERROR 16268 --- [tbeatExecutor-0] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error

I have not installed anything special on to the system. Please let me know what do I need to install?

enter image description here

17 Answers17

108

I find I have to add these two applications to the appllication.properties,it can work.Only one is not enough.

eureka.client.register-with-eureka=false

eureka.client.fetch-registry=false
keith5140
  • 1,294
  • 1
  • 8
  • 11
  • This helped me resolve my problem, thanks for this @xuezhongyu01 – N00b Pr0grammer Jul 10 '19 at 09:21
  • Thanks ... This is the correct answer ... ``eureka.client.fetch-registry=false`` included this line in my eureka microservice. –  Aug 22 '19 at 11:41
  • 9
    Yeah, but this essentially makes this client unable to be discoverable. Is that ideal? Maybe for this case... For me, it was essentially: 1) start the `@EnableEurekaServer` app first; 2) Start any `@EurekaDiscoveryClient` apps afterward. This is mostly just a timing issue (tries to connect, fails, tries again, fails, tries again, succeeds) – Blake Neal Feb 04 '20 at 19:15
  • 6
    To clarify, the two properties above are used inside the eureka server, not the client. This prevents the server from registering itself. – theRiley May 15 '20 at 02:49
  • Thanks eureka.client.register-with-eureka=false resolved my issue. – aatif Jun 11 '21 at 12:34
24

This is happening because it is trying to connect to any known server, so to stop that error, a known server is eureka server on port 8761 which is its default port, you will have to update the application.properties with following

server.port=8761

To avoid eureka from registering itself, you add this to the application.properties

eureka.client.register-with-eureka=false

Ensure that EurekaServer is enabled, for example using spring boot you write the below on the main class.

@EnableEurekaServer

Please pardon me providing solution using .properties file but that is what i work with but .yml configurations shouldn't be too different.

stanlee
  • 386
  • 2
  • 7
14

You need to create Eureka Registry Server which is another microservice. It's main class incase of an SpringBoot application, should have @EnableEurekaServer annotation.

Then in your Eureka Client you will have to mention the Registry server URL in appliation.yml as below :

spring:
  application:
    name: stock-service

server:
  port: 8083


eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8084/eureka/
  instance:
    hostname: localhost

where defaultzone should hold the value of your Eureka Registry.

Once you do all these configurations you need to Get the Eureka Registry microservice up and then you can get the Eureka client up. Once your Registry is up you will not face this exception.

Ashish Kathait
  • 226
  • 6
  • 15
11

This particular message is just a warning. Your application is attempting to register with Eureka but Eureka is not responding. You can either start an instance of Eureka or disable the automatic registration by adding the following to your application.yml.

eureka:
  client:
    register-with-eureka: false
Faron
  • 1,354
  • 10
  • 23
  • oh, I think I dont have Eureka installed on my windows box ? Is that the problem ? Do I need to installed that ? –  Sep 09 '17 at 18:13
  • 1
    You probably do not need it for what you are doing. Just disable it so you do not see a message every 5 seconds. If you would like to learn more about Eureka, start here (https://spring.io/guides/gs/service-registration-and-discovery/) – Faron Sep 09 '17 at 18:25
6

I was facing the same error when my Eureka client i.e microservice trying to register herself with Eureka Server.

Client registration eureka service failure registration failed Cannot execute request on any known server

This error message comes because of following two possibilities.

1) The defaultZone address is incorrect , either its misspelled or the space is missing after :.

2) Spring security implemented on Eureka server require a valid CSRF token be sent with every request. Eureka clients will not generally possess a valid cross site request forgery (CSRF) token. So you will need to disable this requirement for the /eureka/** endpoints.

After disabling the CSRF token with follwoing line of code my Eureka client successfully register them selves with Eureka Server.

@EnableWebSecurity
class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

Also below is the link from spring docs.

https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#_securing_the_eureka_server

Waqas Ahmed
  • 4,801
  • 3
  • 36
  • 45
  • Can you please tell us spring boot and spring cloud versions you have tried with? I have the same issue and the culprit is spring security. I have tried your solution but no difference. I have already looked at docs and https://github.com/spring-cloud/spring-cloud-netflix/issues/2754. no difference. By the way my `spring-boot` version is: 2.5.2 and `spring-cloud` version is: 2020.0.3 – Morteza Jan 14 '22 at 21:56
  • @MortezaBandi my fixed solution was with spring-boot 2.2.6.RELEASE and spring-cloud Hoxton.SR3 – Waqas Ahmed Jan 17 '22 at 05:50
5

Version for dependencies:

  • Spring Boot: 2.5.6
  • Spring Cloud: 2020.0.4

Eureka Server App

application.yaml

server:
  port: 8010
eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
    service-url:
      defaultZone: http://localhost:8010/eureka

MyEurekaServerApplication.java

@EnableEurekaServer
@SpringBootApplication
public class MyEurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyEurekaServerApplication.class, args);
    }
}

Eureka Client App

application.yaml

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8010/eureka

If you use default-zone instead of defaultZone, then the Eureka Client App throws an exception when registering on the Eureka Server.

MyEurekaClientApplication.java

@EnableEurekaClient
@SpringBootApplication
public class MyEurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyEurekaClientApplication.class, args);
    }
}
Dmitry Rakovets
  • 557
  • 1
  • 6
  • 15
4

I got the same error. In my case, in the application.properties file of my client application I misspelled the defaultZone word. I wrote default-zone but it must be defaultZone.

Client App application.properties:

eureka.client.service-url.defaultZone=http://localhost:8030/eureka/

Hostname and port may differ in your case.

Dmitry Rakovets
  • 557
  • 1
  • 6
  • 15
Muhammed Ozdogan
  • 5,341
  • 8
  • 32
  • 53
2

If your eureka server is deployed/running without username and password use below properties.

spring.application.name=Hello-World
eureka.client.serviceUrl.defaultZone=http://eurekaserver:password@localhost:9100/eureka
server.port=9200
eureka.client.fetch-registry=true

If your eureka server is deployed/running with username and password use below properties.

spring.application.name=Hello-World
eureka.client.serviceUrl.defaultZone=http://eurekaserverusername:eurekaserverpassword@localhost:9100/eureka
server.port=9200
eureka.client.fetch-registry=true
Piyush Chaudhari
  • 962
  • 3
  • 19
  • 41
1

Look for the filterType() in ZuulLoggingFiler.java. I had the same problem. Then i saw my filter was returning null. so I changed it to "post" and it worked.

@Override
public String filterType() {
    // TODO Auto-generated method stub
    return "post";
}
1

Check your URL and port number of eureka server provided in "eureka.client.serviceUrl.defaultZone" property.

1

Similar error will be thrown when you have misspelled anything in the value of defaultZone. For ex: someone has misspelled eureka to euraka. Double check it.

defaultZone : http://localhost:8761/eureka
Vaibs
  • 2,018
  • 22
  • 29
1

Adding the following lines in application.properties solved it:

server.port=8761

eureka.client.register-with-eureka=false

eureka.client.fetch-registry=false

The first one is optional.

The other two properties are used to tell Eureka Server that "Hey, you're the only Eureka Server in this context, don't try to find other Eureka Servers"

If we don't mention these, our Eureka Server will try to find and register itself on other Eureka Servers and will eventually throw the Transport Exception.

Prashant K
  • 869
  • 9
  • 9
0

for me its working after connecting to internet because it need to download some dependence first time.

Avinash Khadsan
  • 441
  • 3
  • 6
0

Try to add this property in your microservice which you want to connect with the naming server:

eureka.instance.hostname=localhost
0

Let's set aside the answers given above. Maybe you didn't get the server that has @EnableEurekaServerannotation program up. :)

0

Stater config is needed sometimes, to pom.xml add

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

In application.properties add

spring.config.import=optional:configserver:http://localhost:8888
0

For me, the issue was in my build.gradle file. I just removed this part from my build.gradle file:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Then my gradle file changed to this:

buildscript {
    apply from:"../dependencies.gradle"
    ext {
        set('springBootVersion', "2.1.8.RELEASE")
        set('springCloudVersion',"Greenwich.SR2" )
        set('springCloudServicesVersion', "3.1.0.RELEASE")
        set('springBootAdminVersion','2.1.6')
    }
    repositories {
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.4.RELEASE")
        classpath "io.spring.gradle:dependency-management-plugin:1.0.8.RELEASE"
    }
}
rgettman
  • 176,041
  • 30
  • 275
  • 357
Alireza
  • 25
  • 7