-
Notifications
You must be signed in to change notification settings - Fork 55
Configuration basics
Mariusz Kopylec edited this page Jun 24, 2019
·
9 revisions
Charon can be configured in many ways.
This can be done via CharonConfigurer
API.
Charon can be configured globally or per request mapping.
Request mapping configuration always overwrites the global one for a particular mapping.
For example if Charon is configured like below:
import static com.github.mkopylec.charon.configuration.CharonConfigurer.charonConfiguration;
import static com.github.mkopylec.charon.configuration.RequestMappingConfigurer.requestMapping;
import static com.github.mkopylec.charon.forwarding.interceptors.rewrite.RequestServerNameRewriterConfigurer.requestServerNameRewriter;
@Configuration
class CharonConfiguration {
@Bean
CharonConfigurer charonConfigurer() {
return charonConfiguration()
.set(requestServerNameRewriter().outgoingServers("host1:8080"))
.add(requestMapping("mapping 1"))
.add(requestMapping("mapping 2")
.set(requestServerNameRewriter().outgoingServers("host2:8081")));
}
}
then requests handled by mapping 1
mapping will be forwarded to http://host1:8080 but those handled by mapping 2
mapping will be forwarded to http://host2:8081.
To create a different configuration for a different Spring Boot profile a common configuration can be extracted and then adjusted to the profile:
import static com.github.mkopylec.charon.configuration.CharonConfigurer.charonConfiguration;
import static com.github.mkopylec.charon.configuration.RequestMappingConfigurer.requestMapping;
import static com.github.mkopylec.charon.forwarding.interceptors.rewrite.RequestServerNameRewriterConfigurer.requestServerNameRewriter;
@Configuration
class ReverseProxyConfiguration {
@Profile("default")
@Bean("charonConfigurer")
CharonConfigurer defaultCharonConfigurer() {
return charonConfigurer();
}
@Profile("production")
@Bean("charonConfigurer")
CharonConfigurer productionCharonConfigurer() {
return charonConfigurer()
.set(requestServerNameRewriter().outgoingServers("example-prod.com")) // Replaced server name rewriter
.add(requestMapping("mapping prod")) // New mapping
.add(requestMapping("mapping 1") // Replaced mapping
.set(requestServerNameRewriter().outgoingServers("example-prod-1.com")))
.update("mapping 2", configurer -> configurer // Updated mapping
.set(requestServerNameRewriter().outgoingServers("example-prod-2.com")));
}
private CharonConfigurer charonConfigurer() {
return charonConfiguration()
.set(requestServerNameRewriter().outgoingServers("example.com"))
.add(requestMapping("mapping 1"))
.add(requestMapping("mapping 2")
.set(requestServerNameRewriter().outgoingServers("example-2.com")));
}
}