1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

example annotations clearer

This commit is contained in:
赵禹光
2019-08-23 15:36:31 +08:00
parent 00835121cb
commit 3eb75988e3
16 changed files with 53 additions and 67 deletions

View File

@@ -7,10 +7,7 @@ import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import com.alibaba.cloud.examples.ConsumerApplication.EchoService;
@@ -43,17 +40,17 @@ public class ConsumerApplication {
@FeignClient(name = "service-provider", fallback = EchoServiceFallback.class, configuration = FeignConfiguration.class)
public interface EchoService {
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
@GetMapping(value = "/echo/{str}")
String echo(@PathVariable("str") String str);
@RequestMapping(value = "/divide", method = RequestMethod.GET)
@GetMapping(value = "/divide")
String divide(@RequestParam("a") Integer a, @RequestParam("b") Integer b);
default String divide(Integer a) {
return divide(a, 0);
}
@RequestMapping(value = "/notFound", method = RequestMethod.GET)
@GetMapping(value = "/notFound")
String notFound();
}

View File

@@ -2,11 +2,7 @@ package com.alibaba.cloud.examples;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import com.alibaba.cloud.examples.ConsumerApplication.EchoService;
@@ -44,53 +40,53 @@ public class TestController {
// });
// }
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
@GetMapping(value = "/echo-rest/{str}")
public String rest(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str,
String.class);
}
@RequestMapping(value = "/index", method = RequestMethod.GET)
@GetMapping(value = "/index")
public String index() {
return restTemplate1.getForObject("http://service-provider", String.class);
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
@GetMapping(value = "/test")
public String test() {
return restTemplate1.getForObject("http://service-provider/test", String.class);
}
@RequestMapping(value = "/sleep", method = RequestMethod.GET)
@GetMapping(value = "/sleep")
public String sleep() {
return restTemplate1.getForObject("http://service-provider/sleep", String.class);
}
@RequestMapping(value = "/notFound-feign", method = RequestMethod.GET)
@GetMapping(value = "/notFound-feign")
public String notFound() {
return echoService.notFound();
}
@RequestMapping(value = "/divide-feign", method = RequestMethod.GET)
@GetMapping(value = "/divide-feign")
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
return echoService.divide(a, b);
}
@RequestMapping(value = "/divide-feign2", method = RequestMethod.GET)
@GetMapping(value = "/divide-feign2")
public String divide(@RequestParam Integer a) {
return echoService.divide(a);
}
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
@GetMapping(value = "/echo-feign/{str}")
public String feign(@PathVariable String str) {
return echoService.echo(str);
}
@RequestMapping(value = "/services/{service}", method = RequestMethod.GET)
@GetMapping(value = "/services/{service}")
public Object client(@PathVariable String service) {
return discoveryClient.getInstances(service);
}
@RequestMapping(value = "/services", method = RequestMethod.GET)
@GetMapping(value = "/services")
public Object services() {
return discoveryClient.getServices();
}

View File

@@ -5,11 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
/**
* @author xiaojing
@@ -25,17 +21,17 @@ public class ProviderApplication {
@RestController
class EchoController {
@RequestMapping(value = "/", method = RequestMethod.GET)
@GetMapping(value = "/")
public ResponseEntity index() {
return new ResponseEntity("index error", HttpStatus.INTERNAL_SERVER_ERROR);
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
@GetMapping(value = "/test")
public ResponseEntity test() {
return new ResponseEntity("error", HttpStatus.INTERNAL_SERVER_ERROR);
}
@RequestMapping(value = "/sleep", method = RequestMethod.GET)
@GetMapping(value = "/sleep")
public String sleep() {
try {
Thread.sleep(1000L);
@@ -46,12 +42,12 @@ public class ProviderApplication {
return "ok";
}
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return "hello Nacos Discovery " + string;
}
@RequestMapping(value = "/divide", method = RequestMethod.GET)
@GetMapping(value = "/divide")
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
return String.valueOf(a / b);
}

View File

@@ -17,6 +17,7 @@
package com.alibaba.cloud.examples;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@@ -27,7 +28,7 @@ public class GetConfigController {
@Value("${config}")
private String config;
@RequestMapping(value = "/config", method = RequestMethod.GET)
@GetMapping(value = "/config")
public String getConfig() {
return config;
}

View File

@@ -35,7 +35,7 @@
@RestController
class EchoController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return string;
}
@@ -120,11 +120,11 @@ Nacos Discovery Starter 默认集成了 Ribbon ,所以对于使用了 Ribbon
@Autowired
private EchoService echoService;
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
@GetMapping(value = "/echo-rest/{str}")
public String rest(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
}
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
@GetMapping(value = "/echo-feign/{str}")
public String feign(@PathVariable String str) {
return echoService.echo(str);
}

View File

@@ -34,7 +34,7 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl
@RestController
class EchoController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return string;
}
@@ -123,11 +123,11 @@ The code of `nacos-discovery-consumer-example` project will be analyzed below, d
@Autowired
private EchoService echoService;
@RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
@GetMapping(value = "/echo-rest/{str}")
public String rest(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
}
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
@GetMapping(value = "/echo-feign/{str}")
public String feign(@PathVariable String str) {
return echoService.echo(str);
}

View File

@@ -3,11 +3,7 @@ package com.alibaba.cloud.examples;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
/**
* @author xiaojing
@@ -22,12 +18,12 @@ public class ProviderApplication {
@RestController
class EchoController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return "hello Nacos Discovery " + string;
}
@RequestMapping(value = "/divide", method = RequestMethod.GET)
@GetMapping(value = "/divide")
public String divide(@RequestParam Integer a, @RequestParam Integer b) {
return String.valueOf(a / b);
}