|
1 | 1 | package com.pig4cloud.pig.common.excel;
|
2 | 2 |
|
| 3 | +import com.pig4cloud.pig.common.core.constant.SecurityConstants; |
| 4 | +import com.pig4cloud.pig.common.core.constant.ServiceNameConstants; |
| 5 | +import com.pig4cloud.pig.common.core.util.SpringContextHolder; |
| 6 | +import com.pig4cloud.pig.common.excel.provider.RemoteDictApiService; |
3 | 7 | import com.pig4cloud.pig.common.excel.provider.RemoteDictDataProvider;
|
4 | 8 | import com.pig4cloud.plugin.excel.handler.DictDataProvider;
|
5 | 9 | import org.springframework.boot.autoconfigure.AutoConfiguration;
|
6 | 10 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
| 11 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 12 | +import org.springframework.cloud.client.loadbalancer.LoadBalanced; |
7 | 13 | import org.springframework.context.annotation.Bean;
|
8 |
| -import org.springframework.web.client.RestTemplate; |
| 14 | +import org.springframework.web.client.RestClient; |
| 15 | +import org.springframework.web.client.support.RestClientAdapter; |
| 16 | +import org.springframework.web.service.invoker.HttpServiceProxyFactory; |
| 17 | + |
| 18 | +import java.util.Optional; |
9 | 19 |
|
10 | 20 | /**
|
11 | 21 | * excel 自动装配类
|
|
17 | 27 | public class ExcelAutoConfiguration {
|
18 | 28 |
|
19 | 29 | /**
|
20 |
| - * REST 模板 |
21 |
| - * @return {@link RestTemplate } |
| 30 | + * REST 客户端构建器(支持负载均衡) |
| 31 | + * @return {@link RestClient.Builder } |
| 32 | + */ |
| 33 | + @Bean |
| 34 | + @LoadBalanced |
| 35 | + @ConditionalOnProperty(value = "spring.cloud.nacos.discovery.enabled", havingValue = "true", matchIfMissing = true) |
| 36 | + RestClient.Builder restClientBuilder() { |
| 37 | + return RestClient.builder(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * 远程 dict API 服务 |
| 42 | + * @return {@link RemoteDictApiService } |
22 | 43 | */
|
23 | 44 | @Bean
|
24 | 45 | @ConditionalOnMissingBean
|
25 |
| - public RestTemplate restTemplate() { |
26 |
| - return new RestTemplate(); |
| 46 | + public RemoteDictApiService remoteDictApiService(Optional<RestClient.Builder> restClientBuilderOptional) { |
| 47 | + RestClient client = restClientBuilderOptional.orElseGet(RestClient::builder) |
| 48 | + .baseUrl(getBaseUrl()) |
| 49 | + .defaultHeader(SecurityConstants.FROM, SecurityConstants.FROM_IN) |
| 50 | + .build(); |
| 51 | + HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(RestClientAdapter.create(client)).build(); |
| 52 | + return factory.createClient(RemoteDictApiService.class); |
27 | 53 | }
|
28 | 54 |
|
29 | 55 | /**
|
30 | 56 | * dict 数据提供程序
|
31 |
| - * @param restTemplate REST 模板 |
| 57 | + * @param remoteDictApiService 远程 dict API 服务 |
32 | 58 | * @return {@link DictDataProvider }
|
33 | 59 | */
|
34 | 60 | @Bean
|
35 | 61 | @ConditionalOnMissingBean
|
36 |
| - public DictDataProvider dictDataProvider(RestTemplate restTemplate) { |
37 |
| - return new RemoteDictDataProvider(restTemplate); |
| 62 | + public DictDataProvider dictDataProvider(RemoteDictApiService remoteDictApiService) { |
| 63 | + return new RemoteDictDataProvider(remoteDictApiService); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * 获取Base URL |
| 68 | + * @return {@link String } |
| 69 | + */ |
| 70 | + private String getBaseUrl() { |
| 71 | + // 根据当前架构模式,组装URL |
| 72 | + if (SpringContextHolder.isMicro()) { |
| 73 | + return String.format("http://%s", ServiceNameConstants.UPMS_SERVICE); |
| 74 | + } |
| 75 | + else { |
| 76 | + return String.format("http://%s", SpringContextHolder.getEnvironment() |
| 77 | + .resolvePlaceholders("127.0.0.1:${server.port}${server.servlet.context-path}")); |
| 78 | + } |
38 | 79 | }
|
39 | 80 |
|
40 | 81 | }
|
0 commit comments