Eureka server doesn't discover service - spring-boot

I've just started to learn about microservices via spring cloud and to start with I tried to reproduce basic example from this article https://spring.io/blog/2015/07/14/microservices-with-spring. Here is my code:
Eureka server
#SpringBootApplication
#EnableEurekaServer
public class ServiceRegistryApplication {
public static void main(String[] args) {
System.setProperty("spring.config.name", "registration-server");
SpringApplication.run(ServiceRegistryApplication.class, args);
}
}
resources/registration-server.yml:
# Configure this Discovery Server
eureka:
instance:
hostname: localhost
client: # Not a client, don't register with yourself (unless running
# multiple discovery servers for redundancy)
registerWithEureka: false
fetchRegistry: false
server:
port: 1111 # HTTP (Tomcat) port
Sample service:
#SpringBootApplication
#EnableDiscoveryClient
public class AccountsServiceApplication {
public static void main(String[] args) {
System.setProperty("spring.config.name", "accounts-server");
SpringApplication.run(AccountsServiceApplication.class, args);
}
}
accounts-service.yml:
# Spring properties
spring:
application:
name: accounts-service
# Discovery Server Access
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1111/eureka/
# HTTP Server
server:
port: 2222 # HTTP (Tomcat) port
But when I run both apps and go to the localhost:1111 I cannot see my service in the application list:
Could you tel me please what am I doing wrong?
EDIT
After I applied changes the following line appeared:

i have a great solution for you and it's simple
follow those steps:
1- Eureka Server
#SpringBootApplication
#EnableEurekaServer
public class ServiceRegistryApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRegistryApplication.class, args);
}
}
in application.properties specify those params
spring.application.name=eureka-server
server.port=1111
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
2- in Sample Service
#SpringBootApplication
#EnableDiscoveryClient
public class AccountsServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AccountsServiceApplication.class, args);
}
}
in application.properties specify those params
spring.application.name=accounts-service
server.port=2222
eureka.client.service-url.default-zone=http://localhost:1111/eureka
and don't forget to remove all .yml properties files.

Related

FeignException ServiceUnavailable: Load balancer does not contain an instance for the service match-result-service

I fail to understand, nor find any explanation about how can I fix the exception in subject.
This is the main code I use:
Eureka Server
application:
...
#SpringBootApplication
#EnableEurekaServer
public class BbSimApplication {
public static void main(String[] args) {
SpringApplication.run(BbSimApplication.class, args);
}
}
application.yml:
server.port : 8088
spring:
application:
name : bbsim-discovery-server
eureka:
server:
evictionIntervalTimerInMs: 3000
response-cache-update-interval-ms: 3000
wait-time-in-ms-when-sync-empty: 0
peer-node-read-timeout-ms : 10000
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://localhost:${server.port}/eureka
Controller class:
#RestController
public class WebController {
#Autowired private WebService webService;
#GetMapping("/matchresultbyids")
public MatchStats matchResult(#RequestParam Long homeId, #RequestParam Long awayId){
return webService.matchResult(homeId, awayId);
}
}
Manager Service
application:
#SpringBootApplication
#EnableFeignClients
#EnableDiscoveryClient
public class BBSimManagerServiceApplication {
public static void main(String[] args) {
SpringApplication.run(BBSimManagerServiceApplication.class, args);
}
}
application.yaml:
server.port: 9903
spring:
application.name: bbsim-manager-service
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8088/eureka}
registryFetchIntervalSeconds: 1
# register-with-eureka: true
# fetch-registry: true
instance:
leaseRenewalIntervalInSeconds: 1
Client interface:
#FeignClient("match-result-service")
public interface MatchResultClient {
#GetMapping("/matchresultbyids")
MatchStats getMatchResult();
}
Controller class:
#RestController
public class BbsimManagerController {
#Autowired
MatchResultClient matchStatsClient;
#GetMapping("/matchresultbyids")
public MatchStats matchResult(){
return matchStatsClient.getMatchResult();
}
}
MatchResult Service
application:
#SpringBootApplication
#EnableDiscoveryClient
public class MatchResultServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MatchResultServiceApplication.class, args);
}
}
application.yml:
server.port: 9901
spring:
application:
name: match-result-service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8088/eureka/
registryFetchIntervalSeconds: 1
instance:
leaseRenewalIntervalInSeconds: 1
Controller:
#RestController
public class WebController {
#Autowired private WebService webService;
#GetMapping("/matchresultbyids")
public MatchStats matchResult(){
return webService.matchResult();
}
}
When I try to execute:
http://localhost:9903/matchresultbyids
I get the exception:
o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.FeignException$ServiceUnavailable: [503] during [GET] to [http://match-result-service/matchresultbyids?homeId=0&awayId=1] [MatchResultClient#getMatchResult()]: [Load balancer does not contain an instance for the service match-result-service]] with root cause
feign.FeignException$ServiceUnavailable: [503] during [GET] to [http://match-result-service/matchresultbyids?homeId=0&awayId=1] [MatchResultClient#getMatchResult()]: [Load balancer does not contain an instance for the service match-result-service]
Can you advise me what is wrong and how to fix it?
Thank you all.
It may be related to OpenFeign bug. You can try to upgrade it to the version >= v3.0.6.
If you use spring-cloud-dependencies the version >= v2020.0.5 should be also ok.

Feign Client cannot communicate with a method inside of eureka server

I have a eureka server configured and inside that eureka server I have written a rest api. Now I have a eureka client service and I am trying to call one of the method of eureka service using feign from client service. But I am getting an error "Load balancer does not have available server for client: eureka-service".
But If I call api from a client service to another client service using feign then it is giving successful result. Just can't call API from eureka service.
eureka-service is application name of my eureka server.
#EnableEurekaServer
#SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
#RestController
#RequestMapping("test")
public class TestController {
#GetMapping
public String test(){
return "test success";
}
}
bootstrap.yml of eureka service
eureka:
client:
registerWithEureka: false
fetchRegistry: false
eureka-server-read-timeout-seconds: 60
eureka-server-connect-timeout-seconds: 60
serviceUrl:
defaultZone: http://localhost:8763/eureka/
dashboard:
enabled: true
spring:
application:
name: eureka-service
And client service is:
#SpringBootApplication
#EnableFeignClients
#EnableDiscoveryClient
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
#FeignClient("eureka-service")
public interface TestFeign {
#GetMapping("test")
String test();
}
bootstrap.yml of client service
eureka:
client:
registerWithEureka: true
fetchRegistry: true
eureka-server-read-timeout-seconds: 60
eureka-server-connect-timeout-seconds: 60
serviceUrl:
defaultZone: http://localhost:8763/eureka/
spring:
application:
name: client-service
feign:
hystrix:
enabled: true
ERROR log : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.netflix.hystrix.exception.HystrixRuntimeException: TestFeign#test() failed and no fallback available.] with root cause
com.netflix.client.ClientException: Load balancer does not have available server for client: eureka-service.
How can we solve this issue. Thanks for help in advance.
You need to scan your interfaces that declare they are FeignClients with the #EnableFeignClients annotation in your main class and add the feign.hystrix.enabled=true property.

Eureka Server in TCP

When I create service discovery server, it was automatically set to HTTP
#EnableEurekaServer
#SpringBootApplication
public class DiscoveryServerApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryServerApplication.class, args);
}
}
application.properties
spring.application.name=discovery-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
server.port=8761
http://localhost:8761/eureka
is it possible to set to to TCP/IP or UDP etc?
Thank you very much!

Not able to register spring boot service with eureka on external tomcat

I am trying to deploy two spring boot application services on external tomcat.
Eureka service
Client service
When I deployed these two services on external tomcat, I am able to deploy and I am able to access these two services separately but my client service is not able register with Eureka service and I can't see it on eureka console.
My Eureka service properties file:
#EureakaService.properties#
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
spring.application.name=EurekaServiceApplication
logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF
Below is my Client service properties file
#ClientService.properties#
spring.application.name=CBEApplication
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka-server-1.8.0
I am not sure what is wrong with my configuration settings.
EurekaService class as below# #EnableEurekaServer
#SpringBootApplication
public class EurekaServiceApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(EurekaServiceApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class, args);
} #Client Class code as below# #SpringBootApplication
#EnableDiscoveryClient
public class CBEApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(CBEApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(CBEApplication.class, args);
}
}

Share configuration between Spring cloud config clients

I'm trying to share configuration between Spring Cloud clients with a Spring Cloud config server which have a file-based repository:
#Configuration
#EnableAutoConfiguration
#EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
// application.yml
server:
port: 8888
spring:
profiles:
active: native
test:
foo: world
One of my Spring Cloud client use the test.foo configuration, defined in the config server, and it is configured like below:
#SpringBootApplication
#RestController
public class HelloWorldServiceApplication {
#Value("${test.foo}")
private String foo;
#RequestMapping(path = "/", method = RequestMethod.GET)
#ResponseBody
public String helloWorld() {
return "Hello " + this.foo;
}
public static void main(String[] args) {
SpringApplication.run(HelloWorldServiceApplication.class, args);
}
}
// boostrap.yml
spring:
cloud:
config:
uri: ${SPRING_CONFIG_URI:http://localhost:8888}
fail-fast: true
// application.yml
spring:
application:
name: hello-world-service
Despite this configuration, the Environment in the Spring Cloud Client doesn't contains the test.foo entry (cf java.lang.IllegalArgumentException: Could not resolve placeholder 'test.foo')
However it's works perfectly if i put the properties in a hello-world-service.yml file, in my config server file-based repository.
Maven dependencies on Spring Cloud Brixton.M5 and Spring Boot 1.3.3.RELEASE with spring-cloud-starter-config and spring-cloud-config-server
From Spring Cloud documentation
With the "native" profile (local file system backend) it is
recommended that you use an explicit search location that isn’t part
of the server’s own configuration. Otherwise the application*
resources in the default search locations are removed because they are
part of the server.
So i should put the shared configuration in an external directory and add the path in the application.yml file of the config-server.
// application.yml
spring:
profiles:
active: native
cloud:
config:
server:
native:
search-locations: file:/Users/herau/config-repo
// /Users/herau/config-repo/application.yml
test:
foo: world

Resources