Spring boot test configuration not override normal configuration well - spring-boot

I have a spring boot application with configuation like this:
src/main/resources/application.yml:
eureka:
client:
serviceUrl:
defaultZone: http://10.254.102.12:1111/eureka/
In my unit test cases, I want to set eureka instance name and change the logging level.
src/test/resources/application.yml:
eureka:
instance:
appname: item-client-test
logging:
level: debug
But when I added these additional settings those former settings seems to not work any more. The eureka serviceUrl fallback to default loclahost:8761, so my test failed. When I added my custom eureka serviceUrl to src/test/resources/application.yml, my test goes well.
So I am confused about is this a bug of spring boot or I was not using the right way for my configuration?

What you can try, instead, is having two configuration files like:
src/main/resources/application.yml
eureka:
client:
serviceUrl:
defaultZone: http://10.254.102.12:1111/eureka/
and
src/main/resources/application-test.yml
eureka:
instance:
appname: item-client-test
client:
serviceUrl:
defaultZone: http://test-server/eureka/
logging:
level: debug
and run your tests with SPRING_PROFILES_ACTIVE=test mvn test
Spring Boot will look for application-#{environment} config file (so to say) and use it, inheriting from the default (application.yml) config file

Related

Ignore specific property when running a Spring Boot test

My Spring Boot application contains the following property in src/main/resources/application.yml:
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: ${AUTH_SERVER_METADATA_URL:http://localhost:8080/auth/realms/demo}
When running tests, e.g. #WebMvcTest, I don't have a running authorization server. Thus, I need to use the following configuration which I put into src/test/resources/application-test.yml and activate it by using the #ActiveProfiles("test") annotation.
spring:
security:
oauth2:
resourceserver:
jwt:
public-key-location: classpath:public_rsa.key
The problem is, that the application context loads both of the application property files which leads to a refused connection during the tests. Is it possible to selectively ignore the issuer-uri property during tests?
If you rename src/test/resources/application-test.yml to src/test/resources/application.yml, it should override the file src/main/resources/application.yml

Spring cloud gateway default routing doesn't work

I want enable default routing in my spring cloud gateway (no zuul) by service ids registered in eureka (application names) but I always got 404 error.
In my chat service's bootstrap.yml
I have defined application name
spring:
application:
name: chat-service
and in application properties:
eureka:
instance:
preferIpAddress: true
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://${EUREKA_HOST:localhost}:${EUREKA_PORT:8761}/eureka/
when I go to eureka's dashboard I can see registered my chat service and gateway as well.
Eureka's configuration in gateway application is same as chat service, but I also have this:
spring:
application:
name: gateway
cloud:
gateway:
discovery:
locator:
enabled: true
and next I also tried add explicit routes which din't work as well, but if I have discovery locator enabled set to true this shouldn't be needed right?
routes:
- id: chat-service-route
uri: lb://chat-service
predicates:
- Path=**
I created test endpoint which I tried call directly on chat service and also with gateway. Direct call works fine so issue will be with routing.
#RestController
#RequestMapping
public class TestController {
#GetMapping
public String test() {
return "chat-service ready";
}
}
What I did wrong? I am little desperate. I am using spring boot 2.2.2 and Hoxton.RELEASE cloud dependencies version
Try removing explicit routes and add below property to application yml. This works for me.
spring:
cloud:
gateway:
discovery:
locator:
lower-case-service-id: true

In Spring Boot, How to assign the exact same random port to another property in configure

I am running spring boot with grpc-spring-boot-starter as a grpc-server and also configured as an eureka client.
Since I'm trying to launch more instances later, i want to assign a random port for my grpc service so I made grpc.port=0
The problem is, I also want eureka.instance.nonSecurePort to be the exact same port as grpc.port.Here is my configure.
grpc:
port: 0
eureka:
client:
service-url:
default-zone: http://localhost:8761/eureka
application:
name: grpc-service
instance:
securePortEnabled: false
nonSecurePort: ${grpc.port}
instance_id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
The problem is, I always get eureka.instance.nonSecurePort to be 0 which is not what I want. I know the random port is assigned during runtime. However are there anyway to make the grpc.port and eureka.instance.nonSecurePort always the same after spring boot assigned the random port?
You can put random port following an interval by following way:
grpc:
port: ${random.int[9000,9900]}
eureka:
client:
service-url:
default-zone: http://localhost:8761/eureka
application:
name: grpc-service
instance:
securePortEnabled: false
nonSecurePort: ${grpc.port}

How to set the jolokia context path in springboot admin

I set the application service management.context-path to /mgmt and Registry it to eureka server.
eureka:
instance:
statusPageUrlPath: ${management.context-path}/info
healthCheckUrlPath: ${management.context-path}/health
metadata-map:
management.context-path: ${management.context-path}
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://registry:7000/eureka/
when i view the JMX in admin UI(another different service, and registry in eureka server), i got some security issue. and actually i add the /mgmt/* to the white list
http.authorizeRequests()
.antMatchers("/" ,"/mgmt/*","/jolokia","/jolokia/**").permitAll()
How to set the jolokia context path in springboot admin?
Oh Fk, just a mistake, change the /mgmt/* to /mgmt/, it works.
http.authorizeRequests()
.antMatchers("/" , "/mgmt/**","/jolokia","/jolokia/**").permitAll()

How do I wire up spring oauth using spring eureka?

I have created a Single Page Application using the Spring Tutorial for making one with AngularJS and OAuth and such found here:
https://spring.io/guides/tutorials/spring-security-and-angular-js/#_multiple_ui_applications_and_a_gateway_single_page_application_with_spring_and_angular_js_part_vi
This is the application.yml files for the SPA application:
security:
user:
password: none
oauth2:
client:
accessTokenUri: localhost:7777/uaa/oauth/token
userAuthorizationUri: localhost:7777/uaa/oauth/authorize
clientId: acme
clientSecret: acmesecret
resource:
user-info-uri: localhost:7777/uaa/user
zuul:
routes:
resource:
path: /resource/**
url: localhost:9000/resource
user:
path: /user/**
url: localhost:7777/uaa/user
eureka:
client:
serviceUrl:
defaultZone: ${vcap.services.eureka-service.credentials.uri:127.0.0.1:8761}/eureka/
---
spring:
profiles: cloud
eureka:
instance:
hostname: ${APPLICATION_DOMAIN}
nonSecurePort: 80
I want to know how I would change the zuul routes and the user-info-uri so that I don't have to specify the urls and all this can be done by using the service-id. I looked at the tutorial for using eureka here:
https://spring.io/blog/2015/01/20/microservice-registration-and-discovery-with-spring-cloud-and-netflix-s-eureka
but I don't quite understand how I can achieve my goal without adding all the java to my code, because the basic eureka server already seems to register all my services.
Had issue with using eureka service id, userInfoUri used to throw UnknownHost Exception all the time, #LoadBallanced restTemplate did not solve my issue, solution was to set prefer-token-info to false ( if true - no load ballancing for oauth )
security.oauth2.resource.service-id={Service ID as at eureka server registered}
security.oauth2.resource.userInfoUri= http://${security.oauth2.resource.service-id}/user/me
security.oauth2.resource.loadBalanced=true
security.oauth2.resource.prefer-token-info=false
no port number needed if service ID used , but needed if ip or host used
If I do understand your question correct you can just use the config file in this pattern:
zuul:
routes:
<service_id>:
path: /path/**
For example (if your oauth-service is registered as auth):
zuul:
routes:
auth:
path: /user/**
Zuul will leverage Eureka and find the endpoints for the services. In addition to that it will provide client-side load-balancing.

Resources