title: SpringCloudAlibaba-2-nacos负载均衡、服务上下线、权重元信息
date: 2020-01-26 17:55:33
tags:
SpringCloudAlibaba
1.nacos服务上下线
我们通过nacos管理首页点击:下线
2.nacos负载均衡
nacos默认已经集成了ribbon作为负载均衡
实现负载均衡,修改2点:
1.consumer调用provider时候,我们直接改为服务名调用
|
|
@EnableDiscoveryClient
@SpringBootApplication
public class NacosDiscoveryConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosDiscoveryConsumerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
curl ‘http://127.0.0.1:8050/test?name=qaws‘
@GetMapping(“/test”)
public String test(String name){
RibbonLoadBalancerClient.RibbonServer ribbonServer = (RibbonLoadBalancerClient.RibbonServer) loadBalancerClient.choose(“nacos-discovery-provider”);
NacosServer nacosServer = (NacosServer) ribbonServer.getServer();
Map
System.out.println(“—>”+metadata);
return restTemplate.getForObject(“http://nacos-discovery-provider/hello?name="+name, String.class);
}
```
输出:
—>{name=provider1}