From 6678ec6b7bc5b20319e5a4bd39d3145cd3f0fc9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Fri, 25 Feb 2022 13:57:50 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E8=B0=83=E6=95=B4dubbo=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E8=B4=9F=E8=BD=BD=E5=9D=87=E8=A1=A1=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=B3=A8=E5=85=A5=20=E4=BD=BF=E7=94=A8spring=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=B3=A8=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomEnvironmentPostProcessor.java | 25 +++++++++++++++++++ .../CustomLoadBalanceAutoConfiguration.java | 2 -- .../main/resources/META-INF/spring.factories | 2 ++ .../src/main/resources/loadbalance.properties | 1 - 4 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 ruoyi-common/ruoyi-common-loadbalancer/src/main/java/com/ruoyi/common/loadbalance/config/CustomEnvironmentPostProcessor.java delete mode 100644 ruoyi-common/ruoyi-common-loadbalancer/src/main/resources/loadbalance.properties diff --git a/ruoyi-common/ruoyi-common-loadbalancer/src/main/java/com/ruoyi/common/loadbalance/config/CustomEnvironmentPostProcessor.java b/ruoyi-common/ruoyi-common-loadbalancer/src/main/java/com/ruoyi/common/loadbalance/config/CustomEnvironmentPostProcessor.java new file mode 100644 index 00000000..37fe7b30 --- /dev/null +++ b/ruoyi-common/ruoyi-common-loadbalancer/src/main/java/com/ruoyi/common/loadbalance/config/CustomEnvironmentPostProcessor.java @@ -0,0 +1,25 @@ +package com.ruoyi.common.loadbalance.config; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.core.Ordered; +import org.springframework.core.env.ConfigurableEnvironment; + +/** + * dubbo自定义负载均衡配置注入 + * + * @author Lion Li + */ +public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { + + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + System.setProperty("dubbo.consumer.loadbalance", "customDubboLoadBalancer"); + } + + @Override + public int getOrder() { + return Ordered.HIGHEST_PRECEDENCE; + } + +} diff --git a/ruoyi-common/ruoyi-common-loadbalancer/src/main/java/com/ruoyi/common/loadbalance/config/CustomLoadBalanceAutoConfiguration.java b/ruoyi-common/ruoyi-common-loadbalancer/src/main/java/com/ruoyi/common/loadbalance/config/CustomLoadBalanceAutoConfiguration.java index 9ad18b04..a134f136 100644 --- a/ruoyi-common/ruoyi-common-loadbalancer/src/main/java/com/ruoyi/common/loadbalance/config/CustomLoadBalanceAutoConfiguration.java +++ b/ruoyi-common/ruoyi-common-loadbalancer/src/main/java/com/ruoyi/common/loadbalance/config/CustomLoadBalanceAutoConfiguration.java @@ -1,14 +1,12 @@ package com.ruoyi.common.loadbalance.config; import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients; -import org.springframework.context.annotation.PropertySource; /** * 自定义负载均衡自动配置 * * @author Lion Li */ -@PropertySource(value = "classpath:loadbalance.properties", encoding = "UTF-8") @LoadBalancerClients(defaultConfiguration = CustomLoadBalanceClientConfiguration.class) public class CustomLoadBalanceAutoConfiguration { diff --git a/ruoyi-common/ruoyi-common-loadbalancer/src/main/resources/META-INF/spring.factories b/ruoyi-common/ruoyi-common-loadbalancer/src/main/resources/META-INF/spring.factories index 2146835e..50137508 100644 --- a/ruoyi-common/ruoyi-common-loadbalancer/src/main/resources/META-INF/spring.factories +++ b/ruoyi-common/ruoyi-common-loadbalancer/src/main/resources/META-INF/spring.factories @@ -1,2 +1,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.ruoyi.common.loadbalance.config.CustomLoadBalanceAutoConfiguration +org.springframework.boot.env.EnvironmentPostProcessor=\ + com.ruoyi.common.loadbalance.config.CustomEnvironmentPostProcessor diff --git a/ruoyi-common/ruoyi-common-loadbalancer/src/main/resources/loadbalance.properties b/ruoyi-common/ruoyi-common-loadbalancer/src/main/resources/loadbalance.properties deleted file mode 100644 index 4e11f9e6..00000000 --- a/ruoyi-common/ruoyi-common-loadbalancer/src/main/resources/loadbalance.properties +++ /dev/null @@ -1 +0,0 @@ -dubbo.consumer.loadbalance=customDubboLoadBalancer