测试接口调试
This commit is contained in:
6
pom.xml
6
pom.xml
@ -74,6 +74,12 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-commons</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Actuator 监控 -->
|
<!-- Actuator 监控 -->
|
||||||
|
|||||||
@ -0,0 +1,7 @@
|
|||||||
|
package com.sczx.gateway.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class LoadBalancerConfig {
|
||||||
|
}
|
||||||
@ -3,6 +3,7 @@ package com.sczx.gateway.controller;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@ -13,12 +14,22 @@ public class TestController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private DiscoveryClient discoveryClient;
|
private DiscoveryClient discoveryClient;
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private LoadBalancerClient loadBalancerClient;
|
||||||
|
|
||||||
@GetMapping("/test")
|
@GetMapping("/test")
|
||||||
public String test() {
|
public String test() {
|
||||||
List<ServiceInstance> instances = discoveryClient.getInstances("sczx_user");
|
List<ServiceInstance> instances = discoveryClient.getInstances("sczx_user");
|
||||||
if (instances == null || instances.isEmpty()) {
|
if (instances == null || instances.isEmpty()) {
|
||||||
return "No instances found!";
|
return "No instances found!";
|
||||||
}
|
}
|
||||||
return "Found instance: " + instances.get(0).getUri();
|
|
||||||
|
ServiceInstance instance = loadBalancerClient.choose("sczx_user");
|
||||||
|
if (instance == null) {
|
||||||
|
return "No instance chosen!";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "Chosen instance: " + instance.getUri();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user