I am have created an Gateway Server and registered it with Eureka. Below it is the bootstrap class:
#SpringBootApplication
#EnableZuulServer
#EnableDiscoveryClient
public class GatewayServerApplication {
...
}
I have integrated Zuul Gateway Server with Eureka Server. Everything is going OK. Eureka is starting successfully and the also the Gateway Server is starting successfully.
Nevertheless, I am having trouble accessing the routes. Below is the link that i am trying to access:
https://localhost:5555/actuator/routes
Please, help me with this issue.
Thanks in advance.
P.S my configuration information for gateway server in application.properties file:
#Application
server.port=5555
#Eureka
cloud.config.enabled=true
eureka.instance.preferIpAddress=true
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka-server/
#Actuator
management.endpoints.web.exposure.include=*
management.security.enabled=false
Change the #EnableZuulServer to #EnableZuulProxy, like below:
#SpringBootApplication
#EnableZuulProxy
#EnableDiscoveryClient
public class GatewayServerApplication {
...
}
You have two option for creating Zuul Gateway Server:
The first one is to use #EnableZuulServer annotation. This annotation creates a Zuul Server but doesn't offer the Zuul pre-built routing capabilities. You use this annotation when you want to build your own routing service.
The second option is to use the #EnableZuulProxy annotation. This annotation creates Zuul Server, loads the Zuul Reverse Proxy Filters, and automatically uses Eureka Server to lookup services by their service IDs and then use Netflix Ribbon to do client-side load balancing of requests from within Zuul.
I think in your case is more suited to use #EnableZuulProxy annotation because you don't want to build your own custom service.
Related
Im using the the spring cloud gateway for the first time. my service endpoint is http://localhost:8080/student/getlist
Problem is that when im trying to invoke my service using the cloud gateway it's giving me 404. the eureka is showing the service URL correctly
my gateway properties are below
spring.application.name=gateway
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
server.port=8085
management.endpoints.web.exposure.include=*
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.routes.id=student-service
spring.cloud.gateway.routes.uri=lb://student-service
spring.cloud.gateway.routes.predicates.Path=/student/**
below is the actuator routes in gateway
[{"route_id":"CompositeDiscoveryClient_GATEWAY","route_definition":{"id":"CompositeDiscoveryClient_GATEWAY","predicates":[{"name":"Path","args":{"pattern":"/GATEWAY/**"}}],"filters":[{"name":"RewritePath","args":{"regexp":"/GATEWAY/(?<remaining>.*)","replacement":"/${remaining}"}}],"uri":"lb://GATEWAY","order":0},"order":0},{"route_id":"CompositeDiscoveryClient_STUDENT-SERVICE","route_definition":{"id":"CompositeDiscoveryClient_STUDENT-SERVICE","predicates":[{"name":"Path","args":{"pattern":"/STUDENT-SERVICE/**"}}],"filters":[{"name":"RewritePath","args":{"regexp":"/STUDENT-SERVICE/(?<remaining>.*)","replacement":"/${remaining}"}}],"uri":"lb://STUDENT-SERVICE","order":0},"order":0}]
im trying to invoke the service endpoint trough the gateway via
http://localhost:8085/student/getlist
above URL but this not working.
What am i doing wrong. there is no context path to any microservices.
sample code is under
https://github.com/ojith97/sample.git
Try change your properties to
spring.cloud.gateway.routes[0].id=student-service
spring.cloud.gateway.routes[0].uri=lb://student-service
spring.cloud.gateway.routes[0].predicates[0]=Path=/student/**
You must make the gateway aware of the eureka server with this
eureka.client.service-url.defaultZone=http://user:pass#localhost:8761/eureka
And then make prevent the gateway register to eureka
eureka.client.register-with-eureka=false
The second option is important as it causes 404 errors with loadbalancing using lb:servicename structure.
I have a Spring Boot app as follows:
#SpringBootApplication
#EnableResourceServer
#EnableDiscoveryClient
public class App
{
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
And i have configured oauth2 properties in application.properties.
security.oauth2.resource.user-info-uri=http://localhost:9000/user
However, I need to change this URI depending on the request I send to my Spring Boot App.
For example:
If I receive a request to my Spring Boot App to URL htpp://test.localhost:8000 I would want the user-info-url to be http://test.localhost:9000/user.
But if I receive a request to http://localhost:8000 I want the user-info-url to be http://localhost:9000/user.
My intention is to manage different environments depending on the subdomain.
Is there a way to dynamically change this URI in code instead of hardcoding it as a property?
Spring Cloud Config
By leveraging a Spring Cloud Config server, you can externalize your configuration and push updates to application properties. A simple implementation is backed by a Git repository where you'll store your application.properties file. You commit your changes and then make a POST request the /refresh endpoint that will trigger a reload of your configuration.
There are more advanced scenarios as well but hopefully that's enough to get you started.
Here's a simple Getting Started guide.
Consider you have a Spring application that gets its configuration from a config server. If it cannot connect to the config server, the application will continue to start but as all configurations are missing, it will eventually fail with a potentially misleading error.
Is it possible to configure Spring, so it immediately aborts when it cannot connect to its config server during startup?
Set spring.cloud.config.failFast to true in your bootstrap.yml or bootstrap.properties file. Also, you can add -Dspring.cloud.config.failFast=true to the JVM arguments.
From the documentation
Config Client Fail Fast
In some cases, it may be desirable to fail startup of a service if it cannot connect to the Config Server. If this is the desired behavior, set the bootstrap configuration property spring.cloud.config.failFast=true and the client will halt with an Exception.
You can achieve that by using spring cloud Spring Cloud Config Serverand Spring Cloud Config Client components.
1. Spring Cloud Config Server
The Server provides an HTTP, resource-based API for external configuration (name-value pairs, or equivalent YAML content). The server is easily embeddable in a Spring Boot application using the #EnableConfigServer annotation. So this app is a config server:
//ConfigServer.java
#SpringBootApplication
#EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
2. Spring Cloud Config Client
A Spring Boot application can take immediate advantage of the Spring Config Server (or other external property sources provided by the application developer), and it will also pick up some additional useful features related to Environment change events.
Then in the config client side you can configure fail fast by setting the bootstrap configuration property spring.cloud.config.failFast=true
Spring cloud documentation Config Client Fail Fast
I'm trying to make a basic project using spring cloud with the netflix addons such as Hystrix, Eureka and Ribbon to learn how this works. The project I'm trying to make is a simple message server that will keep messages. And a message-client that will just ask the server for a message, and I want to use the auto discovery client for this, or the RestTemplate discovery. But I can't get either to work.
I have the following structure:
message-client (eureka client)
message-server (eureka client)
configuration-service (config server)
discovery-service (eureka server)
What I currently do is I start up the configuration-service, and expose the application.yml details to all of these "apps/clients" when they are connecting by the following structure:
config-service\src\main\resources\config\appname.yml
app\src\main\resources\bootstrap.yml (contains the appname and url to cloud config)
This is working just fine, and my apps start up on the port they receive from the config server, as well as they all connect to my eureka server, and all of them are visible there. As well as the Hystrix failover is also working, not that it is related to this but it tells me that it can't be completely wrong then.
But here comes my confusion...
When using the #Autowired annotation in my service class (#Service annotated) inside my client module, I get a discoveryClient object, but I am unable to find any other services using that one.
Message Client - boot class:
#EnableAutoConfiguration
#EnableHystrix
#EnableEurekaClient
#ComponentScan("cloud.rest.resources, spring.cloud.client")
public class ClientBoot {
public static void main(String[] args) {
SpringApplication.run(ClientBoot.class, args);
}
}
Message Client - REST Resource:
#RestController
public class MessageResource {
#Autowired
private MessageClient messageClient;
#RequestMapping(value = "/message/{client}", method = RequestMethod.GET)
public Message getMessage(#PathVariable String client) {
return messageClient.getMessage(client);
}
}
Message Client - MessageClient:
#Service
public class RestMessageClient implements MessageClient {
#Autowired
private DiscoveryClient discoveryClient;
#Autowired
private RestTemplate restTemplate;
#Override
public Message getMessage(String client) {
return restTemplate.getForObject(String.format("http://message-server/message/%s", client), Message.class);
}
}
My message server boot class that is holding the messages has the same annotations as my client one.
And as I said, my service class are unable to find anything..
Which leads me to all of my questions:
What is required to actually use ribbon load balancer?
Do I have to use ribbon to be able to use the "auto discovery", I thought not but now I'm just confused.
From what I've understood, when using EnableEurekaClient I should not need to use the EnableDiscoveryClient as well?
Can I change the yml files on my config-server for the clients in runtime and just have to reboot the client?
How much configuration is really meant to be shared by the config-server, because currently all of my clients just contain a super basic bootstrap.yml file.
Does anyone have a good link to where I can read more about all the properties that is being set in my yml files? Both a documentation of what the properties that exists actually do as well as some documentation on how I can use them in combination with spring cloud?
Do I need specific properties to enable my apps/clients to find other apps/clients?
Edited information
Thank you for your quick and excellent reply, I've gone through this over and over today and I finally got my application working..
The problem (I can't understand why and was hoping you could help me understand that) is that my discovery service contains yml files for each of my other clients where I specify things like port and eureka information.. What I specified here as well was:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
So, when I set this value it seems to override something that makes my service discovery not working.. Even tho I can see all my applications in the eureka server, they were unable to find each other when I had this value set.
I set this value by having a message-server.yml file in my configuration service that is sent out to my message-server application after bootstrap..
So then I have two new questions.
How do I override this eureka server property?
Why does my discovery client stop working when I set this value, what is it that it actually does?
What is required to actually use ribbon load balancer?
The ribbon-loadbalancer must be on the classpath (e.g. via "spring-cloud-starter-ribbon"). Then you can inject one as a LoadBalancerClient or you can inject a RestTemplate (it will be load-balancer aware if you have a LoadBalancerClient).
Do I have to use ribbon to be able to use the "auto discovery", I thought not but now I'm just confused.
What is "auto discovery"? You don't need to use Ribbon to use the DiscoveryClient (Ribbon is a load balancer, not a service registry).
From what I've understood, when using EnableEurekaClient I should not need to use the EnableDiscoveryClient as well?
Correct. #EnableEurekaClient is annotated with #EnableDiscoveryClient so it is only there to express a preference.
Can I change the yml files on my config-server for the clients in runtime and just have to reboot the client?
Yes. Or you can use the /refresh or /restart endpoints (a full reboot is probably best in production, at least periodically).
How much configuration is really meant to be shared by the config-server, because currently all of my clients just contain a super basic bootstrap.yml file.
As much as you want. How long is a piece of string? If I were you I would try and keep the central config to a minimum (only the things that change between environments, or at runtime).
Does anyone have a good link to where I can read more about all the properties that is being set in my yml files? Both a documentation of what the properties that exists actually do as well as some documentation on how I can use them in combination with spring cloud?
Spring Boot and Spring Cloud have autogenerated metadata for externalized properties. The new generation of IDEs understands them (so get STS 3.6.4 or IDEA 14.1), and they are listed in the user guide (for Spring Boot at least) under http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties
Do I need specific properties to enable my apps/clients to find other apps/clients?
You need to be able to locate your service registry (Eureka in this case). If you are using Eureka and your clients have registered then that is enough.
I have a simple spring boot application that should register with Eureka. I used the new annotation #EnableDiscoveryClient but still no action at startup. The class is declared as follows, and has a single request mapping.
#Configuration
#ComponentScan
#EnableDiscoveryClient
#EnableAutoConfiguration
#RestController
public class SpringBootEchoApplication {
I see no error messages or anything a startup, I just don't get any discovery client logging like I do in my other app which uses the annotation to enable zuul.
After checking the dependencies in the two projects, the one in which I was not getting eureka registration did not include the eureka-client dependency.
I guess this is a drawback to the #Conditional configuration way of doing things in boot. Even though I have the appropriate Enable annotation specified, because I have no DiscoveryClient implementation (like Eureka) I get no registration... but also no error messages.