How to use 1 external port for 3 applications - wildfly

I Have a 3 applications,
React Front using NextJs
Spring rest API
WildFly server
But so I only have one external port available, I can use some proxy or gateway to access my applications?
I saw Spring cloud Gateway, could run on my external port and redirecting the request for the correctly app? Or is there a better way?
Like this:
Request ip:8433/app1 -> localhost:9000
Request ip:8433/app2 -> localhost:9001
Request ip:8433/app3 -> localhost:9002
Edit One, Spring cloud Gateway code:
#Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("API", r -> r.path("/api/**").uri("http://localhost:8084"))
.route("UI", r -> r.path("/ui/**").uri("http://localhost:3000"))
.route("WILDFLY", r -> r.path("/wildfly/**").uri("https://localhost:8087/wildfly"))
.build();
}

Related

Spring Cloud Discovery Server for kubernetes integration with Feign Client or RestTemplate

I have used spring cloud discovery server for kubernetes based on this release notes.
https://spring.io/blog/2021/10/26/new-features-for-spring-cloud-kubernetes-in-spring-cloud-2021-0-0-m3
A GET request sent to http://localhost/apps is also returning JSON array of available services as expected.
https://docs.spring.io/spring-cloud-kubernetes/docs/2.1.0-M3/reference/html/#apps
My question is how do I configure Feign Client or RestTemplate to consume the endpoint services provided by the Discovery Server.
(Feign Client or RestTemplate) does not seems to resolve the service names.

Deployment Context Based Vip Address in Spring Cloud Load Balancer

We are looking to of move to Spring Cloud Load Balancer to replace Ribbon. We use Eureka for service discovery and registration.
It looks like as of Spring Cloud Netflix 3.x, the EurekaRibbonClientConfiguration in the Eureka client module has been removed.
We were using the deploymentContextBasedVipAddress configuration to map our internal host name to a registered vip address in Eureka.
The configuration was similar to this:
some-sevice-v1.ribbon.NIWSServerListClassName=com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
some-sevice-v1.ribbon.DeploymentContextBasedVipAddresses=some_service-v1
some-sevice-v1.ribbon.NIWSServerListFilterClassName=com.netflix.loadbalancer.ServerListSubsetFilter
We do this because the vip address that is registered with Eureka contains an _ in it and which is technically an invalid character for host names; the Java URI class will fail to parse a host with a _ in it. We are not able to change this at this point.
So my question is; can this similar configuration be done with Spring Cloud Load Balancer and the new Eureka client module in Spring Cloud Netflix 3.x, where we can provide aliasing for the vip address?
We have faced the same issue. Currently, we are using the following workaround for our feign clients:
#FeignClient(
value = "${my.amazing.service.name:amazing-service-name-default}",
contextId = "amazing-service",
configuration = AmazingServiceClientConfiguration.class,
path = "/amazing-service"
)
public interface AmazingServiceClient {
...
}
We can configure my.amazing.service.name the same way as DeploymentContextBasedVipAddresses.
There is also an open ticket for this issue: https://github.com/spring-cloud/spring-cloud-commons/issues/951
Hope, this helps.

Spring Cloud Zuul - Add http proxy details (proxy server, proxy port, credentials etc) to the incoming request before routing

We are planning to build a reverse proxy server for our enterprise to make some external API calls.
Currently, our microservice applications are hosted on the PCF environment. For any external (on the internet) calls, we make use of the PCF proxy server to communicate.
Now my use-case is to build a reverse proxy server(Spring Zuul) to route to external APIs through the PCF proxy. However, the microservice applications wouldn't pass any proxy information on the request to the zuul server. So, this needs to be added by Spring Zuul reverse proxy server.
Problem:
How to add HTTP proxy details to the coming request from zuul server?
Any documentation also would be really helpful.

Spring cloud - Server and client

I'm config an application using spring cloud eureka. I start my discovery app in the 8761 port and reaching the console in "http://localhost:8761".
So, I start my client application and it's appear in the "Application" page of eureka console.
First question:
My client is using "server.port=0" in properties config so the tomcat port is starting in random. How can I reaching my services in client? Example: I have a get request in "/api/stuff", is that possible to access this not using the random port? Suppose I don't know the port!
Second Question:
I can start any clients I want, they will start, assuming a random port and register in the cloud server discovery, I can see the log:
"Registering application FLY-CLIENT with eureka with status UP"
But they don't appear in "Application" page of eureka console, why they don't appear?
Thanks!
If you are using Spring RestTemplate to request the services registered in Eureka you need to make it #LoadBalanced, something like this should do the try:
#LoadBalanced
#Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
// usage
restTemplate.getForObject("http://your-service-name/api/stuff", StuffResponse.class);
As for the 2nd question, I'm a little confused, as you mentioned earlier in the question that your application appears on Eureka's dashboard. Is this behavior only happening for the "fly-client"?

Springboot REST - Expose same service as secured & non-secured

I want to enable basic authentication for my Spring https REST services(currently unsecured) in springboot application. Multiple clients are consuming these services, while some are ok to move to secured one, some still want to consume the unsecured version for few more months. How can I expose same service as both secured & unsecured in same spring boot application?
Though I had done this for Apache cxf REST services by hosting same service in different ports & securing only one port, don't know how to accomplish this in springboot.
Create RequestMapping with two endPoints as below. Clients who want to use Basic Authentication will be served using /secure/** (can't be accessed without Authentication ) and others who are going to migrate to secure after a few months will be served using /unsecure/** (anyone can access). You can use class level RequestMapping to avoid change in every endPoint at method level
#GetMapping(value= {"/secure/users","/unsecure/users"})
public ResponseEntity<List<User>> findAllUsers()
{
...
}
Now Configure the Security as below. for that, you need Client Roles stored in DB
#Override
protected void configure(HttpSecurity http) throws Exception{
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/unsecure/**").permitAll()
.antMatchers("/secure/**").hasRole("CLIENT_SECURE")
.anyRequest().authenticated();
}
Working Git Example
Secure endPoint: GET http://localhost:8088/secure/users Status 403
UnSecure endPoint: GET: http://localhost:8088/unsecure/users Status 200