更改负载均衡(改为随机)
This commit is contained in:
parent
d9bfd4e70b
commit
28d1906cf1
@ -1,6 +1,7 @@
|
|||||||
package com.test.config;
|
package com.test.config;
|
||||||
|
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||||
|
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
@ -13,10 +14,12 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
* @Create 2023/8/15 10:08
|
* @Create 2023/8/15 10:08
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@LoadBalancerClient(value = "userservice", //指定为 userservice 服务,只要是调用此服务都会使用我们指定的策略
|
||||||
|
configuration = LoadBalancerConfig.class) //指定我们刚刚定义好的配置类
|
||||||
public class BeanConfig {
|
public class BeanConfig {
|
||||||
@Bean
|
@Bean
|
||||||
@LoadBalanced
|
@LoadBalanced
|
||||||
RestTemplate template(){
|
RestTemplate template() {
|
||||||
return new RestTemplate();
|
return new RestTemplate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.test.config;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
|
||||||
|
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
|
||||||
|
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||||
|
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ClassName: LoadBalancerConfig
|
||||||
|
* Package: com.test.config
|
||||||
|
*
|
||||||
|
* @author yovinchen
|
||||||
|
* @Create 2023/8/15 16:46
|
||||||
|
*/
|
||||||
|
public class LoadBalancerConfig {
|
||||||
|
//将官方提供的 RandomLoadBalancer 注册为Bean
|
||||||
|
@Bean
|
||||||
|
public ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory){
|
||||||
|
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
|
||||||
|
return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
|
||||||
|
}
|
||||||
|
}
|
@ -9,8 +9,7 @@ eureka:
|
|||||||
prefer-ip-address: false
|
prefer-ip-address: false
|
||||||
instance-id: ${spring.cloud.client.ip-address}:${server.port}
|
instance-id: ${spring.cloud.client.ip-address}:${server.port}
|
||||||
client:
|
client:
|
||||||
fetch-registry: true
|
fetch-registry: false
|
||||||
register-with-eureka: false
|
|
||||||
serviceUrl:
|
serviceUrl:
|
||||||
defaultZone: http://eureka02:8802/eureka, http://eureka01:8801/eureka
|
defaultZone: http://eureka02:8802/eureka, http://eureka01:8801/eureka
|
||||||
server:
|
server:
|
||||||
|
@ -9,8 +9,7 @@ eureka:
|
|||||||
prefer-ip-address: false
|
prefer-ip-address: false
|
||||||
instance-id: ${spring.cloud.client.ip-address}:${server.port}
|
instance-id: ${spring.cloud.client.ip-address}:${server.port}
|
||||||
client:
|
client:
|
||||||
fetch-registry: true
|
fetch-registry: false
|
||||||
register-with-eureka: false
|
|
||||||
serviceUrl:
|
serviceUrl:
|
||||||
defaultZone: http://eureka01:8801/eureka, http://eureka02:8802/eureka
|
defaultZone: http://eureka01:8801/eureka, http://eureka02:8802/eureka
|
||||||
server:
|
server:
|
||||||
|
Loading…
Reference in New Issue
Block a user