diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/config/YudaoWebAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/config/YudaoWebAutoConfiguration.java index e27095a46..2b368f598 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/config/YudaoWebAutoConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/config/YudaoWebAutoConfiguration.java @@ -21,6 +21,7 @@ import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; import org.springframework.util.AntPathMatcher; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @@ -146,9 +147,20 @@ public class YudaoWebAutoConfiguration { */ @Bean @ConditionalOnMissingBean - @LoadBalanced + @Primary public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) { return restTemplateBuilder.build(); } + /** + * 创建 RestTemplate 实例(支持负载均衡) + * + * @param restTemplateBuilder {@link RestTemplateAutoConfiguration#restTemplateBuilder} + */ + @Bean + @LoadBalanced + public RestTemplate loadBalancedRestTemplate(RestTemplateBuilder restTemplateBuilder) { + return restTemplateBuilder.build(); + } + } diff --git a/yudao-module-bpm/yudao-module-bpm-server/src/main/java/cn/iocoder/yudao/module/bpm/api/event/CrmContractStatusListener.java b/yudao-module-bpm/yudao-module-bpm-server/src/main/java/cn/iocoder/yudao/module/bpm/api/event/CrmContractStatusListener.java index 12eb9cf17..00dc09500 100644 --- a/yudao-module-bpm/yudao-module-bpm-server/src/main/java/cn/iocoder/yudao/module/bpm/api/event/CrmContractStatusListener.java +++ b/yudao-module-bpm/yudao-module-bpm-server/src/main/java/cn/iocoder/yudao/module/bpm/api/event/CrmContractStatusListener.java @@ -1,8 +1,10 @@ package cn.iocoder.yudao.module.bpm.api.event; import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmHttpRequestUtils; +import jakarta.annotation.Resource; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.client.RestTemplate; /** * 合同审批的结果的监听器实现类 @@ -11,6 +13,9 @@ import org.springframework.web.bind.annotation.RequestBody; */ public class CrmContractStatusListener extends BpmProcessInstanceStatusEventListener { + @Resource + private RestTemplate loadBalancedRestTemplate; + @Override public String getProcessDefinitionKey() { return "crm-contract-audit"; @@ -19,7 +24,8 @@ public class CrmContractStatusListener extends BpmProcessInstanceStatusEventList @Override public void onEvent(@RequestBody @Valid BpmProcessInstanceStatusEvent event) { BpmHttpRequestUtils.executeBpmHttpRequest(event, - "http://crm-server/rpc-api/crm/contract/update-audit-status"); + "http://crm-server/rpc-api/crm/contract/update-audit-status", + loadBalancedRestTemplate); } } diff --git a/yudao-module-bpm/yudao-module-bpm-server/src/main/java/cn/iocoder/yudao/module/bpm/api/event/CrmReceivableStatusListener.java b/yudao-module-bpm/yudao-module-bpm-server/src/main/java/cn/iocoder/yudao/module/bpm/api/event/CrmReceivableStatusListener.java index a780a5be7..336c94cee 100644 --- a/yudao-module-bpm/yudao-module-bpm-server/src/main/java/cn/iocoder/yudao/module/bpm/api/event/CrmReceivableStatusListener.java +++ b/yudao-module-bpm/yudao-module-bpm-server/src/main/java/cn/iocoder/yudao/module/bpm/api/event/CrmReceivableStatusListener.java @@ -1,8 +1,10 @@ package cn.iocoder.yudao.module.bpm.api.event; import cn.iocoder.yudao.module.bpm.framework.flowable.core.util.BpmHttpRequestUtils; +import jakarta.annotation.Resource; import jakarta.validation.Valid; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.client.RestTemplate; /** * 回款审批的结果的监听器实现类 @@ -11,6 +13,9 @@ import org.springframework.web.bind.annotation.RequestBody; */ public class CrmReceivableStatusListener extends BpmProcessInstanceStatusEventListener { + @Resource + private RestTemplate loadBalancedRestTemplate; + @Override public String getProcessDefinitionKey() { return "crm-receivable-audit"; @@ -19,7 +24,8 @@ public class CrmReceivableStatusListener extends BpmProcessInstanceStatusEventLi @Override public void onEvent(@RequestBody @Valid BpmProcessInstanceStatusEvent event) { BpmHttpRequestUtils.executeBpmHttpRequest(event, - "http://crm-server/rpc-api/crm/receivable/update-audit-status"); + "http://crm-server/rpc-api/crm/receivable/update-audit-status", + loadBalancedRestTemplate); } } diff --git a/yudao-module-bpm/yudao-module-bpm-server/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/BpmHttpRequestUtils.java b/yudao-module-bpm/yudao-module-bpm-server/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/BpmHttpRequestUtils.java index 358ee9f4d..6de7f9dfa 100644 --- a/yudao-module-bpm/yudao-module-bpm-server/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/BpmHttpRequestUtils.java +++ b/yudao-module-bpm/yudao-module-bpm-server/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/util/BpmHttpRequestUtils.java @@ -80,6 +80,13 @@ public class BpmHttpRequestUtils { public static void executeBpmHttpRequest(BpmProcessInstanceStatusEvent event, String url) { + RestTemplate restTemplate = SpringUtils.getBean(RestTemplate.class); + executeBpmHttpRequest(event, url, restTemplate); + } + + public static void executeBpmHttpRequest(BpmProcessInstanceStatusEvent event, + String url, + RestTemplate restTemplate) { // 1.1 设置请求头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); @@ -102,7 +109,6 @@ public class BpmHttpRequestUtils { // } // 2. 发起请求 - RestTemplate restTemplate = SpringUtils.getBean(RestTemplate.class); sendHttpRequest(url, headers, event, restTemplate); }