You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.5 KiB
44 lines
1.5 KiB
package cc.bnblogs.consumer.controller; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
import org.springframework.beans.factory.annotation.Autowired; |
|
import org.springframework.cloud.client.ServiceInstance; |
|
import org.springframework.cloud.client.discovery.DiscoveryClient; |
|
import org.springframework.cloud.client.loadbalancer.LoadBalanced; |
|
import org.springframework.web.bind.annotation.GetMapping; |
|
import org.springframework.web.bind.annotation.RestController; |
|
import org.springframework.web.client.RestTemplate; |
|
|
|
import java.util.List; |
|
import java.util.concurrent.ThreadLocalRandom; |
|
|
|
@RestController |
|
@Slf4j |
|
public class ConsumerController { |
|
@Autowired |
|
private DiscoveryClient discoveryClient; |
|
|
|
@Autowired |
|
private RestTemplate restTemplate; |
|
|
|
@GetMapping("/instances") |
|
public List<ServiceInstance> AllInstances() { |
|
return discoveryClient.getInstances("provider"); |
|
} |
|
// |
|
// @GetMapping("/index") |
|
// public String index() { |
|
// // 返回当前注册的所有实例 |
|
// List<ServiceInstance> list = AllInstances(); |
|
// // 随机选择一个实例 |
|
// int index = ThreadLocalRandom.current().nextInt(list.size()); |
|
// log.info("当前访问的是第{}个服务",index + 1); |
|
// ServiceInstance serviceInstance = list.get(index); |
|
// String url = serviceInstance.getUri() + "/index"; |
|
// return restTemplate.getForObject(url,String.class); |
|
// } |
|
@GetMapping("/index") |
|
public String index() { |
|
return restTemplate.getForObject("http://provider/index",String.class); |
|
} |
|
}
|
|
|