Created
December 31, 2025 07:13
-
-
Save sandeepnegi1996/e240713b9ed0da586539d850896702b5 to your computer and use it in GitHub Desktop.
feign client http proxy configuration
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
| 🔹 application-local.yml | |
| proxy: | |
| enabled: true | |
| host: proxy.local.corp | |
| port: 8080 | |
| auth: | |
| enabled: true | |
| username: ${PROXY_USER} | |
| password: ${PROXY_PASS} | |
| 🔹 application-prod.yml (server) | |
| proxy: | |
| enabled: true | |
| host: proxy.prod.corp | |
| port: 8080 | |
| auth: | |
| enabled: false | |
| @Data | |
| @ConfigurationProperties(prefix = "proxy") | |
| public class ProxyProperties { | |
| private boolean enabled; | |
| private String host; | |
| private int port; | |
| private Auth auth = new Auth(); | |
| @Data | |
| public static class Auth { | |
| private boolean enabled; | |
| private String username; | |
| private String password; | |
| } | |
| } | |
| 4️⃣ Conditional Apache HttpClient Creation | |
| @Configuration | |
| @RequiredArgsConstructor | |
| public class FeignProxyHttpClientConfig { | |
| private final ProxyProperties proxyProperties; | |
| @Bean | |
| @ConditionalOnProperty(name = "proxy.enabled", havingValue = "true") | |
| public Client feignClientWithProxy() { | |
| HttpClientBuilder builder = HttpClients.custom(); | |
| // Configure proxy | |
| HttpHost proxy = new HttpHost( | |
| proxyProperties.getHost(), | |
| proxyProperties.getPort() | |
| ); | |
| builder.setProxy(proxy); | |
| // Conditional authentication | |
| if (proxyProperties.getAuth().isEnabled()) { | |
| CredentialsProvider credentialsProvider = | |
| new BasicCredentialsProvider(); | |
| credentialsProvider.setCredentials( | |
| new AuthScope(proxyProperties.getHost(), | |
| proxyProperties.getPort()), | |
| new UsernamePasswordCredentials( | |
| proxyProperties.getAuth().getUsername(), | |
| proxyProperties.getAuth().getPassword() | |
| ) | |
| ); | |
| builder.setDefaultCredentialsProvider(credentialsProvider); | |
| } | |
| return new ApacheHttpClient(builder.build()); | |
| } | |
| } | |
| @FeignClient( | |
| name = "hotel-external-client", | |
| url = "${hotel.service.url}", | |
| configuration = FeignProxyHttpClientConfig.class | |
| ) | |
| public interface HotelExternalClient { | |
| @GetMapping("/hotels/{id}") | |
| HotelResponse getHotel(@PathVariable String id); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment