提交 db1f1669 编写于 作者: 檀越@新空间's avatar 檀越@新空间 🐭

fix:获取实例

上级 210111cd
package com.kwan.springcloudalibaba;
import com.alibaba.cloud.nacos.ribbon.NacosRule;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.PingUrl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
......@@ -26,11 +28,19 @@ public class NacosConsumerApplication {
@Bean
public IRule randomRule() {
return new RandomRule(); //随机
return new NacosRule(); //nacos的策略
// return new RandomRule(); //随机
//return new RoundRobinRule(); //轮训
//return new BestAvailableRule();
//return new ZoneAvoidanceRule(); //新版本默认的策略
}
@Bean
public IPing iping() {
return new PingUrl();
}
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
......
......@@ -14,26 +14,19 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RefreshScope
@RestController
@RequestMapping("/nacos/consumer")
public class ConsumerUserController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private RestTemplate restTemplate;
@Autowired
private FeignUserService computeClient;
@Autowired
private DiscoveryClient discoveryClient;
/**
* 使用LoadBalanceClient和RestTemplate结合的方式来访问
* http://127.0.0.1:8085/nacos/consumer/loadBalancerClient/1
......@@ -50,7 +43,6 @@ public class ConsumerUserController {
return Result.ok(restTemplate.getForObject(url, Result.class));
}
/**
* 使用ip访问
* http://127.0.0.1:8085/nacos/consumer/loadBalancerClientIp/1
......@@ -68,7 +60,6 @@ public class ConsumerUserController {
return Result.ok(restTemplate.getForObject(url, Result.class));
}
/**
* 通过服务名进行负载均衡访问
* http://127.0.0.1:8085/nacos/consumer/serverName/1
......@@ -83,7 +74,6 @@ public class ConsumerUserController {
return Result.ok(restTemplate.getForObject(url, Result.class));
}
/**
* feign调用
* http://127.0.0.1:8085/nacos/consumer/1
......@@ -99,6 +89,35 @@ public class ConsumerUserController {
}
/**
* 获取服务实例的具体信息
* 如:实例个数
* 实例url
* 实例ip
* 实例在nacos中的定位信息
* http://127.0.0.1:8085/nacos/consumer/services/nacos-server-producer
*
* @param service
* @return
*/
@GetMapping("/services/{service}")
public Result services(@PathVariable String service) {
return Result.ok(discoveryClient.getInstances(service));
}
/**
* 获取所有服务
* http://127.0.0.1:8085/nacos/consumer/services
*
* @return
*/
@GetMapping("/services")
public Result services() {
System.out.println(discoveryClient.description());
System.out.println(discoveryClient.getOrder());
return Result.ok(discoveryClient.getServices());
}
/**
* 获取配置的变量
*/
......@@ -106,6 +125,4 @@ public class ConsumerUserController {
public Result nameInfo() {
return Result.ok();
}
}
}
\ No newline at end of file
......@@ -30,4 +30,7 @@ spring:
file-extension: yaml #文件类型
group: DEV_GROUP #组别
namespace: e750dcd5-657b-489b-8d15-b7b71aa3e984 #命名空间
refresh-enabled: true #默认自动刷新
\ No newline at end of file
refresh-enabled: true #默认自动刷新
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册