Last active
July 8, 2020 17:47
-
-
Save lbbedendo/eb8829b67d8db86eecacb7551ab63c2f to your computer and use it in GitHub Desktop.
Example using ProxyHttpClient in Micronaut 2.0.0 with Multi-Tenancy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
micronaut: | |
application: | |
name: gateway | |
server: | |
port: 8080 | |
cors: | |
enabled: true | |
security: | |
authentication: bearer | |
enabled: true | |
endpoints: | |
login: | |
enabled: true | |
oauth: | |
enabled: true | |
token: | |
jwt: | |
enabled: true | |
signatures: | |
secret: | |
generator: | |
secret: ${JWT_GENERATOR_SIGNATURE_SECRET} | |
generator: | |
access-token: | |
expiration: 86400 | |
propagation: | |
enabled: true | |
header: | |
enabled: true | |
headerName: "Authorization" | |
prefix: "Bearer " | |
service-id-regex: "my-service" | |
http: | |
client: | |
max-content-length: 104857600 | |
services: | |
my-service: | |
url: "${LEARN_URL:http://my-service:8085}" | |
.... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.micronaut.context.annotation.ConfigurationProperties; | |
import java.net.URI; | |
@ConfigurationProperties("micronaut.http.services.my-service") | |
public class MyServiceConfiguration { | |
public static final String ID = "my-service"; | |
private URI url; | |
public URI getUrl() { | |
return url; | |
} | |
public void setUrl(URI url) { | |
this.url = url; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import br.com.platosedu.learn.conf.LearnConfiguration; | |
import io.micronaut.core.async.publisher.Publishers; | |
import io.micronaut.http.HttpRequest; | |
import io.micronaut.http.MutableHttpResponse; | |
import io.micronaut.http.annotation.Filter; | |
import io.micronaut.http.client.ProxyHttpClient; | |
import io.micronaut.http.client.annotation.Client; | |
import io.micronaut.http.filter.OncePerRequestHttpServerFilter; | |
import io.micronaut.http.filter.ServerFilterChain; | |
import io.micronaut.multitenancy.tenantresolver.TenantResolver; | |
import org.reactivestreams.Publisher; | |
@Filter("/my-service/**") | |
public class MyServiceRequestFilter extends OncePerRequestHttpServerFilter { | |
private final ProxyHttpClient proxyHttpClient; | |
private final MyServiceConfiguration myServiceConfiguration; | |
private final TenantResolver tenantResolver; | |
public LearnRequestFilter(@Client(id = MyServiceConfiguration.ID) ProxyHttpClient proxyHttpClient, | |
MyServiceConfiguration myServiceConfiguration, | |
TenantResolver tenantResolver) { | |
this.proxyHttpClient = proxyHttpClient; | |
this.myServiceConfiguration = myServiceConfiguration; | |
this.tenantResolver = tenantResolver; | |
} | |
@Override | |
protected Publisher<MutableHttpResponse<?>> doFilterOnce(HttpRequest<?> request, ServerFilterChain chain) { | |
return Publishers.map( | |
proxyHttpClient.proxy( | |
request.mutate() | |
.uri(b -> b | |
.scheme("http") | |
.host(myServiceConfiguration.getUrl().getHost()) | |
.port(myServiceConfiguration.getUrl().getPort()) | |
.replacePath(request.getPath().substring("/my-service".length()))) | |
.body(request.getBody()) | |
.header("tenantId", tenantResolver.resolveTenantIdentifier().toString()) | |
), response -> response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment