Merge pull request #876 from zhaoyuguang/zhaoyuguang/dev_3

Use Spring advanced API make design more clearer
pull/903/head
format 5 years ago committed by GitHub
commit 5b07a560be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -140,7 +140,7 @@ Sentinel 适配了 https://github.com/OpenFeign/feign[OpenFeign] 组件。如果
```java
@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);
}

@ -141,7 +141,7 @@ This is a simple usage of `FeignClient`:
```java
@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);
}

@ -19,7 +19,7 @@ package com.alibaba.cloud.examples;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@ -34,7 +34,7 @@ public class HomeController {
@Autowired
private EchoService echoService;
@RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
@GetMapping(value = "/", produces = "application/json")
public String home() {
LOGGER.info("-----------------consumer调用开始-----------------");
String param = "Nice to meet you.";

@ -19,6 +19,7 @@ package com.alibaba.cloud.examples;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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;
@ -35,7 +36,7 @@ public class HomeController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
@GetMapping(value = "/", produces = "application/json")
public String home() {
LOGGER.info("-----------------consumer调用开始-----------------");
String param = "Nice to meet you.";

@ -18,10 +18,7 @@ package com.alibaba.cloud.examples;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RestController;
import org.springframework.web.bind.annotation.*;
/**
* @author xiaolongzuo
@ -31,7 +28,7 @@ public class EchoController {
private static final Logger LOGGER = LoggerFactory.getLogger(EchoController.class);
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET, produces = "application/json")
@GetMapping(value = "/echo/{str}", produces = "application/json")
public String echo(@PathVariable String str) {
LOGGER.info("-----------收到消费者请求-----------");
LOGGER.info("收到消费者传递的参数:" + str);

@ -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;
@ -44,17 +41,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();
}

@ -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();
}

@ -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);
}

@ -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;
}

@ -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;
}
@ -102,7 +102,7 @@ Nacos Discovery Starter 默认集成了 Ribbon ,所以对于使用了 Ribbon
@FeignClient(name = "service-provider")
public interface EchoService {
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
@GetMapping(value = "/echo/{str}")
String echo(@PathVariable("str") String str);
}
@ -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);
}

@ -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;
}
@ -105,7 +105,7 @@ The code of `nacos-discovery-consumer-example` project will be analyzed below, d
@FeignClient(name = "service-provider")
public interface EchoService {
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
@GetMapping(value = "/echo/{str}")
String echo(@PathVariable("str") String str);
}
@ -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);
}

@ -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);
}

@ -20,6 +20,7 @@ import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@ -45,7 +46,7 @@ public class AccountController {
this.random = new Random();
}
@RequestMapping(value = "/account", method = RequestMethod.POST, produces = "application/json")
@PostMapping(value = "/account", produces = "application/json")
public String account(String userId, int money) {
LOGGER.info("Account Service ... xid: " + RootContext.getXID());

@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@ -54,7 +55,7 @@ public class BusinessApplication {
@FeignClient(value = "order", url = "http://127.0.0.1:18083")
public interface OrderService {
@RequestMapping(path = "/order", method = RequestMethod.POST)
@PostMapping(path = "/order")
String order(@RequestParam("userId") String userId,
@RequestParam("commodityCode") String commodityCode,
@RequestParam("orderCount") int orderCount);

@ -24,6 +24,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
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;
@ -60,7 +61,7 @@ public class HomeController {
}
@GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
@RequestMapping(value = "/seata/rest", method = RequestMethod.GET, produces = "application/json")
@GetMapping(value = "/seata/rest", produces = "application/json")
public String rest() {
String result = restTemplate.getForObject(
@ -96,7 +97,7 @@ public class HomeController {
}
@GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
@RequestMapping(value = "/seata/feign", method = RequestMethod.GET, produces = "application/json")
@GetMapping(value = "/seata/feign", produces = "application/json")
public String feign() {
String result = storageService.storage(COMMODITY_CODE, ORDER_COUNT);

@ -33,6 +33,7 @@ import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@ -62,7 +63,7 @@ public class OrderController {
this.random = new Random();
}
@RequestMapping(value = "/order", method = RequestMethod.POST, produces = "application/json")
@PostMapping(value = "/order", produces = "application/json")
public String order(String userId, String commodityCode, int orderCount) {
LOGGER.info("Order Service Begin ... xid: " + RootContext.getXID());

@ -19,10 +19,7 @@ package com.alibaba.cloud.examples;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
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.RestController;
import org.springframework.web.bind.annotation.*;
import io.seata.core.context.RootContext;
@ -43,7 +40,7 @@ public class StorageController {
this.jdbcTemplate = jdbcTemplate;
}
@RequestMapping(value = "/storage/{commodityCode}/{count}", method = RequestMethod.GET, produces = "application/json")
@GetMapping(value = "/storage/{commodityCode}/{count}", produces = "application/json")
public String echo(@PathVariable String commodityCode, @PathVariable int count) {
LOGGER.info("Storage Service Begin ... xid: " + RootContext.getXID());
int result = jdbcTemplate.update(

@ -1,6 +1,7 @@
package com.alibaba.cloud.examples;
import org.springframework.beans.factory.annotation.Autowired;
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;
@ -17,24 +18,24 @@ public class TestController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
@GetMapping(value = "/hello")
@SentinelResource("resource")
public String hello() {
return "Hello";
}
@RequestMapping(value = "/aa", method = RequestMethod.GET)
@GetMapping(value = "/aa")
@SentinelResource("aa")
public String aa(int b, int a) {
return "Hello test";
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
@GetMapping(value = "/test")
public String test1() {
return "Hello test";
}
@RequestMapping(value = "/template", method = RequestMethod.GET)
@GetMapping(value = "/template")
public String client() {
return restTemplate.getForObject("http://www.taobao.com/test", String.class);
}

Loading…
Cancel
Save