I want spring cloud gateway to redirect a request received at "/fetchRoute" to a service api running at "http://localhost:9090/getRoutes".
My application.yml file looks like this:
server:
port: 8080
spring:
cloud:
gateway:
routes:
- id : Routes
uri: http://localhost:9090/getRoutes
predicates:
- Path=/fetchRoute
When I hit "http://localhost:8080/fetchRoute" (Gateway service is running on localhost:8080) from postman I am getting the following error
{
"timestamp": "2022-07-20T12:56:34.802+00:00",
"status": 404,
"error": "Not Found",
"path": "/fetchRoute"
}
I tried hitting "http://localhost:9090/getRoutes" directly and it's working properly so it's not an issue with the service
Related
Eureka Sever registers all my services, however when trying to access them through their application.name I cannot, I have tried RestTemplate and OpenFeign but it has not been possible in any way.
When FEATURES-SERVICE requests an image from IMAGES-SERVICE through: http://localhost:9005 work, but when FEATURES-SERVICE requests an image from IMAGES-SERVICE through: http://images-service fail.
This is my config for eureka-server:
server:
port: 8761
eureka:
client:
fetch-registry: false
register-with-eureka: false
and config for features-service and images-service
// IMAGES-SERVICE
server:
port:9005
spring:
application:
name: images-service
eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
default-zone: http://localhost:8761/eureka
instance:
hostname: localhost
// FEATURES-SERVICE
server:
port:9004
spring:
application:
name: features-service
eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
default-zone: http://localhost:8761/eureka
instance:
hostname: localhost
both services has the annotation #EnableEurekaClient in the Application file.
I was facing the same issue while using rest template and calling the other service, I got it running using the annotation #LoadBalanced on RestTemplate bean in my application class.
I was doing the same as what is suggested here in the answer (adding #LoadBalanced) for the restTemplate injector but it wasn't working for me.
I had written the gateway controller in the Eureka Server itself to receive request from front end and redirect it to other micro-services.
When I wrote whole new application to work as gateway service, everything is working fine now.
Check if you have Ribbon added as a dependency in your pom.xml file OR you are getting exception from Ribbon. If this is the case remove the Ribbon from your project.
i have developed the micro service application, in that application there is a service call user service which is running on port 8281. This service handle the authentications. when i test the service in local environment this is worked fine. But if i call this service using zuul api gateway this is not working. following property in the application.yml file is use to get the redirect url in the local environment. This is worked fine.
security:
oauth2:
client:
registration:
google:
redirectUri: "http://localhost:8281/oauth2/callback/google"
But if i change this property as follow for connect with zuul api gateway.
security:
oauth2:
client:
registration:
google:
redirectUri: "http://localhost:8080/api/user/oauth2/callback/google"
This is not working and is throw this error message [authorization_request_not_found]. localhost:8080 is the zuul api gateway. zuul has configure to forward request to user service like this.
zuul:
prefix: /api
routes:
auth-service:
path: /user/**
serviceId: USER-SERVICE
stripPrefix: true
sensitiveHeaders: Cookie,Set-Cookie
so why this error is thrown ?
Iam getting this issue
Access to XMLHttpRequest at 'http://localhost:8082/v1/store/getstore' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
My config in application.yaml is
grails:
cors:
enabled: true
allowedOrigins:
- http://localhost:4200
and in url mapping
"/v1/store/getStore"(controller: 'store', action: 'getStore')
when i directly hit request as "http://localhost:8082/store/getstore" its not throwing error but after placing in urlMapping its throwing cors issue.
create proxy.conf.json file at root folder and edit the below:
{
"/": {
"target": "http://localhost:8082/",
"secure": false
}
}
change the package json command for start:
"start": "ng serve --proxy-config proxy.conf.json --open",
use the URL mapping as same "/v1/store/getStore"(controller: 'store', action: 'getStore')
Basic question so just want to ensure I understand it all correctly.
I have created a discovery server:
#SpringBootApplication
#EnableEurekaServer
public class DisocveryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DisocveryServiceApplication.class, args);
}
and registered microservices with it successfully; If I hit localhost:8761 I can see my discovery service has found the microservices. The microservices run fine if i hit them on their designated port. For example, I have one called creds and if i hit localhost:9000 it returns. However, My understanding is I should now be able to hit localhost:8761/creds and it will show the same output but this isnt working.
Am I misunderstanding? Any suggestions on what I should try?
creds bootstrap.yml:
spring:
application:
name: creds
creds application.yml
server:
port: 9000
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
discover application.yml
server:
port: ${PORT:8761}
eureka:
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
Another #EnableEurekaClient annotated Spring boot webservice can access your creds webservice by using an injected RestTemplate with http://creds/..., where creds is the spring.application.name registered with Eureka.
If you want to access the creds webservice from the outside of your web application, then what you want is a proxy like Zuul http://github.com/Netflix/zuul.
Just registering micro service to Eureka server wont make sure that you can access the microservice under a gateway. Eureka Server is not a gateway server , its just a service registry. You can think Eureka as just one more service that holds information about all the other services in the cluster. It doesnt do anything extra other than getting information to the Clients registered.
You may need a Gateway service for routing your request under the Eureka Server. Zuul Proxy routes a request coming to it to the under lying microservices using its service id or the URL configured.
Add this dependency in your classpath.
dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.0.4.RELEASE</version>
</dependency>
Add this config in your properties filezuul:
routes:
serv1:
path: /serv1/**
serviceId: http://localhost:8080/serv1
serv2:
path: /serv2/**
serviceId: serv2
This will create a dynamic router that routes your request to appropriate service instances. This also provides a server end load balancer for your services
I have an ouath2 module, server running on port 9999, and when I request "localhost:9999/oauth/token" I do get the token.
However, even the auth server and the REST endpoints are in the same project (different modules, but they know about each other), the controllers seem to not care if the request holds a token.
Now, here comes the interesting stuff:
If I add this code to my application.yml, the controllers do not care about the auth at all.
security:
basic:
enabled: false
oauth2:
client:
client-id: acme
client-secret: acmesecret
access-token-uri: http://localhost:9999/oauth/token
user-authorization-uri: http://localhost:9999/oauth/authorize
resource:
user-info-uri: http://localhost:9999/user
token-info-uri: http://localhost:9999/oauth/check_token
auth:
server:
url: http://localhost:9999/oauth/check_token/
clientId: acme
clientSecret: acmesecret
oauth2:
resource:
userInfoUri: http://localhost:9999/user
But, when I remove it and try to request the controller I get this:
{
"timestamp": 1472114046044,
"status": 401,
"error": "Unauthorized",
"message": "Full authentication is required to access this resource",
"path": "/gamification/api/ebubo"
}
Any ideas? Anyone?
#ComponentScan("the_actual_package") was missing