update nacos-discovery-consumer-sclb-example

pull/2152/head
XuDaojie 4 years ago
parent ae5f25f22d
commit 35a67497c0

@ -16,17 +16,14 @@
package com.alibaba.cloud.examples; package com.alibaba.cloud.examples;
import java.util.List;
import java.util.Random;
import com.alibaba.cloud.examples.ConsumerSCLBApplication.EchoService; import com.alibaba.cloud.examples.ConsumerSCLBApplication.EchoService;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate; import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
@ -50,6 +47,11 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Random;
import reactor.core.publisher.Mono;
/** /**
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a> * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/ */
@ -161,12 +163,21 @@ public class ConsumerSCLBApplication {
@Autowired @Autowired
private DiscoveryClient discoveryClient; private DiscoveryClient discoveryClient;
@Value("${spring.cloud.loadbalancer.zone:null}")
private String zone;
@GetMapping("/echo-rest/{str}") @GetMapping("/echo-rest/{str}")
public String rest(@PathVariable String str) { public String rest(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str, return restTemplate.getForObject("http://service-provider/echo/" + str,
String.class); String.class);
} }
@GetMapping("/zone")
public String zone() {
return "consumer zone " + zone + "\n" + restTemplate
.getForObject("http://service-provider/zone", String.class);
}
@GetMapping("/echo-feign/{str}") @GetMapping("/echo-feign/{str}")
public String feign(@PathVariable String str) { public String feign(@PathVariable String str) {
return echoService.echo(str); return echoService.echo(str);

@ -7,6 +7,8 @@ spring.cloud.nacos.username=nacos
spring.cloud.nacos.password=nacos spring.cloud.nacos.password=nacos
spring.cloud.loadbalancer.ribbon.enabled=false spring.cloud.loadbalancer.ribbon.enabled=false
spring.cloud.loadbalancer.configurations=zone-preference
spring.cloud.loadbalancer.zone=hangzhou
feign.sentinel.enabled=true feign.sentinel.enabled=true

@ -16,6 +16,12 @@
package com.alibaba.cloud.examples; package com.alibaba.cloud.examples;
import java.util.Map;
import javax.annotation.Resource;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@ -40,6 +46,9 @@ public class ProviderApplication {
@RestController @RestController
class EchoController { class EchoController {
@Resource
private NacosDiscoveryProperties nacosDiscoveryProperties;
@GetMapping("/") @GetMapping("/")
public ResponseEntity index() { public ResponseEntity index() {
return new ResponseEntity("index error", HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity("index error", HttpStatus.INTERNAL_SERVER_ERROR);
@ -71,6 +80,12 @@ public class ProviderApplication {
return String.valueOf(a / b); return String.valueOf(a / b);
} }
@GetMapping("/zone")
public String zone() {
Map<String, String> metadata = nacosDiscoveryProperties.getMetadata();
return "provider zone " + metadata.get("zone");
}
} }
} }

Loading…
Cancel
Save