i implement spring security in apiGateway .The problem that zuul proxy allow all requests from other microservices - spring

for example it allows all request form annoce-service.
this is application.properties API_Gate way (zuul proxy)
server.port=6039
spring.data.mongodb.uri=mongodb://localhost:27017/AccessRefreshToken
spring.data.mongodb.auto-index-creation=true
spring.application.name=API_GATEWAY
eureka.instance.prefer-ip-address=true
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.main.allow-circular-references=true
management.endpoints.routes.enabled=true
management.endpoints.web.exposure.include=*
zuul.routes.middleware.path=/ANNONCE_SERVICE/**
this application.properties of eureka
server.port= 8761
eureka.client.register-with-eureka= false
eureka.client.fetch-registry= false
and this is application.properties of an other to test the zuul
it is annoce service and this is his application.properties
server.port=6040
spring.application.name=ANNONCE_SERVICE
eureka.instance.prefer-ip-address=true
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
what am'i making wrong ??

Related

Not able to run my application : com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

ISSUE WITH THE EUREKA SERVER
NOT ABLE TO RUN MY APPLICATION
Dependency ?
application.properties ?
APPLICATION.PROPERTIES
server.port=9866
eureka.client.service-url.default-zone=http://localhost:8761/eureka
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.instance.prefer-ip-address=true
spring.datasource.url=jdbc:mysql://localhost:3306/ACC_ECOM_SCHEMA?useSSL=false
spring.datasource.username=root
spring.datasource.password=admin123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.root=warn
logging.level.org.springframework=error
logging.level.com.acc.ecom.user=error
logging.file.name=/tmp/logs/userservice.log
logging.file.max-size=10MB
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
Scenario 1
when Eureka Server is Running
see the below Console image
Scenario 2
When Eureka Server is NOT Running
see the below Console image

Spring API Gateway java.net.ConnectException: Connection timed out: no further information

I have a spring boot app with simple GET method "sayHello" i have configured API gateway to route calls to my microservice module when uri pattern matches /ms1/**
Both gateway and microservice registered to the Registry service and dashboard displays both registered
API-GATEAY YAML configuration
server:
port: '8010'
spring:
application:
name: MY-GATEWAY
cloud:
compatibility-verifier:
enabled: false
gateway:
routes:
- id: MICRO-SERVICE1
uri: lb://MICRO-SERVICE1
predicates:
- Path=/ms1/**
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-uri: http://localhost:8761/eureka/
intance:
hostname: localhost
MICRO-SERVICE1 YAML configuration
server:
port: '8030'
spring:
application:
name: MICRO-SERVICE1
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-uri: http://localhost:8761/eureka/
intance:
hostname: localhost
Controller's GET method
#RestController
#RequestMapping("/ms1")
public class UATController {
#GetMapping(value = "/sayHello")
public #ResponseBody ResponseEntity<String> sayHello() {
String message = "Hello World";
return new ResponseEntity<String>(message, HttpStatus.OK);
}
}
I am getting following error when i access via gateway, however when i access my microservice directly it works any idea where i am wrong here?
http://localhost:8010/ms1/sayHello <- Not working via gateway
http://localhost:8030/ms1/sayHello <- Working direct access
Wed Dec 07 23:32:31 EST 2021
[83b324d4-2] There was an unexpected error (type=Internal Server Error, status=500).
Connection timed out: no further information: DESKTOP-1.verizon.net/192.168.1.160:8030
io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection timed out: no further information: DESKTOP-1.verizon.net/192.168.1.160:8030
Suppressed: The stacktrace has been enhanced by Reactor, refer to additional information below:
Error has been observed at the following site(s):
*__checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
*__checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
*__checkpoint ⇢ HTTP GET "/ms1/sayHello" [ExceptionHandlingWebHandler]
Original Stack Trace:
Caused by: java.net.ConnectException: Connection timed out: no further information
at java.base/sun.nio.ch.Net.pollConnect(Native Method)
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:946)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:833)
I have faced the same issue today as well when running the micro services, the gateway and the Eureka discovery all on localhost.
I was able to solve it by adding eureka.instance.preferIpAddress=true to my application.properties of the micro services. That property causes that the services will advertise their IP addresses instead of the host names. More info here under point 2.6: https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-eureka-server.html. My guess is that there was an issue with forwarding the requests to the microservices due to using localhost.
Edit: Here are my settings
There is nothing special in my setup. These are the values I am using in my application.properties
The microservice is using:
spring.cloud.discovery.enabled=true
spring.application.name=[The name of my app]
eureka.client.register.with-eureka=true
eureka.client.service-url.defaultZone=http://localhost:8999/eureka
eureka.instance.prefer-ip-address=true
The properties for the gateway are similar to the service one.
The load balancing part looks like this:
spring.cloud.gateway.routes[0].id=[Same name as my app]
spring.cloud.gateway.routes[0].uri=lb://[name of my app]
spring.cloud.gateway.routes[0].predicates=Path=/test/**
Where test/** is the part of the Rest URL of my application
Adding DESKTOP-1.verizon.net to hosts file worked, following is entry I have added to C:\Windows\System32\drivers\etc\hosts file. Unfortunately eureka.instance.prefer-ip-address=true didn't work for me
127.0.0.1 DESKTOP-1.verizon.net
have added this settings in yml file in all services and its work for me.
eureka:
instance:
prefer-ip-address: true
I just faced the same issue, and in my case, I had a problem with port number. I was using the code below to set up the port for me:
server:
port: ${random.int(8010,8013)}
I noticed when the service starts, even though the log shows "Tomcat started on port(s): 8010", on Eureka's page shows "host.docker.internal:my-service:8011"
This error happens randomly, because sometimes when you re-start the microservice, it is able to register on Eureka correctly.
You can do a simple test using a non-random port number.
I haven't figured out how to solve it for random port number.
UPDATING:
I did this and worked fine for me:
server:
port: 0
spring:
application:
name: user-service
eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://localhost:9000/eureka/

Spring-Cloud on Weblogic 12.1.2

We have developed Spring-boot microservices with NetFlix OSS/Spring-cloud. In the development environment, we are running with embedded tomcat, it works well.
Now I'm trying to deploy on Weblogic 12.1.2, I'm able to deploy service registry and API-Gateway inside two different managed servers. But When I open Eureka server page, API-Gateway is not registered with the Service Registry.
Service-Registry application.yml
spring:
application:
name: service-registry
server:
port: 61001
eureka:
client:
register-with-eureka: false
fetch-registry: false
api-gateway application.yml
spring:
application:
name: api-gateway
cloud:
loadbalancer:
retry:
enabled: true
# Ribbon - Client side load balancer
ribbon:
ReadTimeout : 60000
ConnectTimeout: 60000
server:
port: 61002
context-path: /nasw
eureka:
client:
serviceUrl:
defaultZone: http://<ipAdr>:61001/eureka/
# instance:
# instanceId:
### Zuul - Router/Filter/Reverse Proxy
zuul:
ignoredServices: '*'
host:
connect-timeout-millis: 100000
socket-timeout-millis: 600000
routes:
network-planning: "**/network-planning/**"
network-schemagen: "**/network-schemagen/**"
network-verification: "**/network-verification/**"
nw-netgeoview-nw-tracer: "**/nw-netgeoview-nw-tracer/**"
### Hystrix - Circuit Breaker/Fault&Latency Tolerant
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 600000
timeout:
enabled: false
isolation:
strategy: SEMAPHORE
Service registry home page
Could you please help me with following queries?
Is it possible to deploy Spring-Cloud on Weblogic 12.1.2?
Is it a correct way to deploy each microservice in a separate managed server?
If Yes, Am I missing any configuration so that api-gateway can register with service-registry?

Config server and eureka server in same application: tries to connect to localhost:8761

I have a spring-boot application which I use to setup a spring cloud config server and a eureka server in development and testing environments.
Strangely the application always tries to connect to localhost:8761, even though I have eureka.client.registerWithEureka set to false.
How can I deactivate this?
The error:
ERROR 3144 --- [et_localhost-12] c.n.e.cluster.ReplicationTaskProcessor : Network level connection to peer localhost; retrying after delay
com.sun.jersey.api.client.ClientHandlerException: org.apache.http.conn.ConnectTimeoutException: Connect to localhost:8761 timed out
at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
at com.netflix.eureka.cluster.DynamicGZIPContentEncodingFilter.handle(DynamicGZIPContentEncodingFilter.java:48) ~[eureka-core-1.4.10.jar:1.4.10]
at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.4.10.jar:1.4.10]
at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar:1.19.1]
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) ~[jersey-client-1.19.1.jar:1.19.1]
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.19.1.jar:1.19.1]
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570) ~[jersey-client-1.19.1.jar:1.19.1]
at com.netflix.eureka.transport.JerseyReplicationClient.submitBatchUpdates(JerseyReplicationClient.java:116) ~[eureka-core-1.4.10.jar:1.4.10]
at com.netflix.eureka.cluster.ReplicationTaskProcessor.process(ReplicationTaskProcessor.java:71) ~[eureka-core-1.4.10.jar:1.4.10]
at com.netflix.eureka.util.batcher.TaskExecutors$BatchWorkerRunnable.run(TaskExecutors.java:187) [eureka-core-1.4.10.jar:1.4.10]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_92]
Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to localhost:8761 timed out
My only class looks like this:
#EnableEurekaServer
#EnableConfigServer
#SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
The application.yml:
server:
port: 8888
eureka:
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
renewal-percent-threshold: 0.49
security:
basic:
enabled: true
user:
password: mypassword
spring:
jmx:
default-domain: ${spring.application.name}
cloud:
config:
server:
git:
uri: https://example.com/myrepo.git
username: username
password: password
clone-on-start: true
search-paths: '{application},{application}/{profile}'
endpoints:
jmx:
domain: ${spring.application.name}
unique-names: true
In the bootstrap.yml I have only the application name set.
Versions:
spring-cloud-netflix-eureka: 1.1.6,
spring-cloud-config-server: 1.1.3
Could you try to change your configuration like below
eureka:
client:
registerWithEureka: false
fetchRegistry: false
service-url:
defaultZone: http://localhost:8888/eureka
You specify server.port: 8888. So your eureka is running on 8888 port. But you didn't specify any service-url for eureka. So I think that your eureka server is trying to replicate to localhost:8761 because it's default and you didn't specify service-url for eureka.
For me, below properties that worked for me.
eureka.client.registerWithEureka= false
eureka.client.fetchRegistry= false
eureka.server.maxThreadsForPeerReplication=0
Please Use 8761 port for your Eureka server as below.
server:
port: 8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
Here we’re configuring an application port – 8761 is the default one for Eureka servers. We are telling the built-in Eureka Client not to register with ‘itself’, because our application should be acting as a server.
It seems that eureka tries to connect to itself despite the below settings:
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
you have two options to fix that 1-Keep the default port of eureka 2-Add below to your application.properties of your eureka server eureka.server.maxThreadsForPeerReplication=0

Zuul -> Eureka Server, Basic Authentication issue

I am able to hit the service, if the flow doesn't contain Basic Authorization.
If i use Basic Authorization, it throws "message": "Full authentication is required to access this resource"
Below are my observations:
In ZuulFilter, run() method, i get value for
request.getHeader("Authorization") --> Basic c29tOnNvbzz==
but once it reaches the Micro Service, i am getting value as 'null',
request.getHeader("Authorization") --> null
Using Spring Boot version : 1.4.0.RELEASE
This is my flow:
------------------
Zuul -> Service Discovery (Eureka Server) -> Service
Kindly help, not sure where the Authorization header is vanishing.
Eureka Server yml file:
-------------------------
server.port:4001
eureka.instance.hostname=localhost
eureka.client.fetch-registry:false
eureka.client.register-with-eureka:false
eureka.client.serviceUrl.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.client.healthcheck.enabled=true
Zuul yml file:
-----------------
server:
port: 8765
info:
component: Edge Server
eureka:
instance:
leaseRenewalIntervalInSeconds: 3
metadataMap:
instanceId: ${spring.application.name}:${random.value}
client:
# Default values comes from org.springframework.cloud.netflix.eurek.EurekaClientConfigBean
registryFetchIntervalSeconds: 5
instanceInfoReplicationIntervalSeconds: 5
initialInstanceInfoReplicationIntervalSeconds: 5
endpoints:
restart:
enabled: true
shutdown:
enabled: true
health:
sensitive: false
zuul.sensitive-headers: Cookie,Set-Cookie,Authorization
logging:
level:
ROOT: WARN
se.callista: INFO
# Get info regarding connection to the cofig server and retries if required
org.springframework.cloud.config.client.ConfigServicePropertySourceLocator: INFO
org.springframework.retry.support.RetryTemplate: DEBUG
# Set INFO to see the allocated port
org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer: INFO
---
eureka:
instance:
preferIpAddress: true
client:
serviceUrl:
defaultZone: http://localhost:4001/eureka,http://localhost:4002/eureka
Authorization is by default a sensitive header, this means Zuul will not forward them. If you leave it out of the sensitive headers, Zuul will forward the header.
zuul.sensitiveHeaders: Cookie,Set-Cookie
It should also be camelCase instead of hyphenated.
Extra info: https://github.com/spring-cloud/spring-cloud-netflix/blob/master/docs/src/main/asciidoc/spring-cloud-netflix.adoc#cookies-and-sensitive-headers
This solved my problem, but is this the only solution we have ?
ctx.addZuulRequestHeader("Authorization",request.getHeader("‌​Authorization"))
Your property for zuul.sensitiveHeaders is wrong. It is camel case not hyphenated.
https://github.com/spring-cloud/spring-cloud-netflix/blob/master/docs/src/main/asciidoc/spring-cloud-netflix.adoc#cookies-and-sensitive-headers

Resources