mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Update The sample
This commit is contained in:
parent
a8b274e61b
commit
be18803104
@ -27,12 +27,15 @@ import org.springframework.beans.factory.annotation.Value;
|
|||||||
import org.springframework.boot.ApplicationRunner;
|
import org.springframework.boot.ApplicationRunner;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Lazy;
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
@ -52,194 +55,204 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
|
|||||||
@EnableDiscoveryClient
|
@EnableDiscoveryClient
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@EnableFeignClients
|
@EnableFeignClients
|
||||||
|
@EnableScheduling
|
||||||
|
@EnableCaching
|
||||||
public class DubboSpringCloudConsumerBootstrap {
|
public class DubboSpringCloudConsumerBootstrap {
|
||||||
|
|
||||||
@Reference
|
@Reference
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
@Reference(version = "1.0.0", protocol = "dubbo")
|
@Reference(version = "1.0.0", protocol = "dubbo")
|
||||||
private RestService restService;
|
private RestService restService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@Lazy
|
@Lazy
|
||||||
private FeignRestService feignRestService;
|
private FeignRestService feignRestService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@Lazy
|
@Lazy
|
||||||
private DubboFeignRestService dubboFeignRestService;
|
private DubboFeignRestService dubboFeignRestService;
|
||||||
|
|
||||||
@Value("${provider.application.name}")
|
@Value("${provider.application.name}")
|
||||||
private String providerApplicationName;
|
private String providerApplicationName;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@LoadBalanced
|
@LoadBalanced
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
@FeignClient("${provider.application.name}")
|
@FeignClient("${provider.application.name}")
|
||||||
public interface FeignRestService {
|
public interface FeignRestService {
|
||||||
|
|
||||||
@GetMapping(value = "/param")
|
@GetMapping(value = "/param")
|
||||||
String param(@RequestParam("param") String param);
|
String param(@RequestParam("param") String param);
|
||||||
|
|
||||||
@PostMapping("/params")
|
@PostMapping("/params")
|
||||||
public String params(@RequestParam("b") String b, @RequestParam("a") int a);
|
public String params(@RequestParam("b") String b, @RequestParam("a") int a);
|
||||||
|
|
||||||
@PostMapping(value = "/request/body/map", produces = APPLICATION_JSON_UTF8_VALUE)
|
@PostMapping(value = "/request/body/map", produces = APPLICATION_JSON_UTF8_VALUE)
|
||||||
User requestBody(@RequestParam("param") String param,
|
User requestBody(@RequestParam("param") String param,
|
||||||
@RequestBody Map<String, Object> data);
|
@RequestBody Map<String, Object> data);
|
||||||
|
|
||||||
@GetMapping("/headers")
|
@GetMapping("/headers")
|
||||||
public String headers(@RequestHeader("h2") String header2,
|
public String headers(@RequestHeader("h2") String header2,
|
||||||
@RequestHeader("h") String header, @RequestParam("v") Integer value);
|
@RequestHeader("h") String header, @RequestParam("v") Integer value);
|
||||||
|
|
||||||
@GetMapping("/path-variables/{p1}/{p2}")
|
@GetMapping("/path-variables/{p1}/{p2}")
|
||||||
public String pathVariables(@PathVariable("p2") String path2,
|
public String pathVariables(@PathVariable("p2") String path2,
|
||||||
@PathVariable("p1") String path1, @RequestParam("v") String param);
|
@PathVariable("p1") String path1, @RequestParam("v") String param);
|
||||||
}
|
}
|
||||||
|
|
||||||
@FeignClient("${provider.application.name}")
|
@FeignClient("${provider.application.name}")
|
||||||
@DubboTransported(protocol = "dubbo")
|
@DubboTransported(protocol = "dubbo")
|
||||||
public interface DubboFeignRestService {
|
public interface DubboFeignRestService {
|
||||||
|
|
||||||
@GetMapping(value = "/param")
|
@GetMapping(value = "/param")
|
||||||
String param(@RequestParam("param") String param);
|
String param(@RequestParam("param") String param);
|
||||||
|
|
||||||
@PostMapping("/params")
|
@PostMapping("/params")
|
||||||
String params(@RequestParam("b") String paramB, @RequestParam("a") int paramA);
|
String params(@RequestParam("b") String paramB, @RequestParam("a") int paramA);
|
||||||
|
|
||||||
@PostMapping(value = "/request/body/map", produces = APPLICATION_JSON_UTF8_VALUE)
|
@PostMapping(value = "/request/body/map", produces = APPLICATION_JSON_UTF8_VALUE)
|
||||||
User requestBody(@RequestParam("param") String param,
|
User requestBody(@RequestParam("param") String param,
|
||||||
@RequestBody Map<String, Object> data);
|
@RequestBody Map<String, Object> data);
|
||||||
|
|
||||||
@GetMapping("/headers")
|
@GetMapping("/headers")
|
||||||
public String headers(@RequestHeader("h2") String header2,
|
public String headers(@RequestHeader("h2") String header2,
|
||||||
@RequestParam("v") Integer value, @RequestHeader("h") String header);
|
@RequestParam("v") Integer value, @RequestHeader("h") String header);
|
||||||
|
|
||||||
@GetMapping("/path-variables/{p1}/{p2}")
|
@GetMapping("/path-variables/{p1}/{p2}")
|
||||||
public String pathVariables(@RequestParam("v") String param,
|
public String pathVariables(@RequestParam("v") String param,
|
||||||
@PathVariable("p2") String path2, @PathVariable("p1") String path1);
|
@PathVariable("p2") String path2, @PathVariable("p1") String path1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ApplicationRunner userServiceRunner() {
|
public ApplicationRunner userServiceRunner() {
|
||||||
return arguments -> {
|
return arguments -> {
|
||||||
|
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setId(1L);
|
user.setId(1L);
|
||||||
user.setName("小马哥");
|
user.setName("小马哥");
|
||||||
user.setAge(33);
|
user.setAge(33);
|
||||||
|
|
||||||
// save User
|
// save User
|
||||||
System.out.printf("UserService.save(%s) : %s\n", user,
|
System.out.printf("UserService.save(%s) : %s\n", user,
|
||||||
userService.save(user));
|
userService.save(user));
|
||||||
|
|
||||||
// find all Users
|
// find all Users
|
||||||
System.out.printf("UserService.findAll() : %s\n", user,
|
System.out.printf("UserService.findAll() : %s\n", user,
|
||||||
userService.findAll());
|
userService.findAll());
|
||||||
|
|
||||||
// remove User
|
// remove User
|
||||||
System.out.printf("UserService.remove(%d) : %s\n", user.getId(),
|
System.out.printf("UserService.remove(%d) : %s\n", user.getId(),
|
||||||
userService.remove(user.getId()));
|
userService.remove(user.getId()));
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ApplicationRunner callRunner() {
|
public ApplicationRunner callRunner() {
|
||||||
return arguments -> {
|
return arguments -> {
|
||||||
|
callAll();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// To call /path-variables
|
private void callAll() {
|
||||||
callPathVariables();
|
|
||||||
|
|
||||||
// To call /headers
|
// To call /path-variables
|
||||||
callHeaders();
|
callPathVariables();
|
||||||
|
|
||||||
// To call /param
|
// To call /headers
|
||||||
callParam();
|
callHeaders();
|
||||||
|
|
||||||
// To call /params
|
// To call /param
|
||||||
callParams();
|
callParam();
|
||||||
|
|
||||||
// To call /request/body/map
|
// To call /params
|
||||||
callRequestBodyMap();
|
callParams();
|
||||||
|
|
||||||
};
|
// To call /request/body/map
|
||||||
}
|
callRequestBodyMap();
|
||||||
|
}
|
||||||
|
|
||||||
private void callPathVariables() {
|
@Scheduled(fixedDelay = 10 * 1000L)
|
||||||
// Dubbo Service call
|
public void onScheduled() {
|
||||||
System.out.println(restService.pathVariables("a", "b", "c"));
|
callAll();
|
||||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
}
|
||||||
System.out.println(dubboFeignRestService.pathVariables("c", "b", "a"));
|
|
||||||
// Spring Cloud Open Feign REST Call
|
private void callPathVariables() {
|
||||||
|
// Dubbo Service call
|
||||||
|
System.out.println(restService.pathVariables("a", "b", "c"));
|
||||||
|
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||||
|
System.out.println(dubboFeignRestService.pathVariables("c", "b", "a"));
|
||||||
|
// Spring Cloud Open Feign REST Call
|
||||||
// System.out.println(feignRestService.pathVariables("b", "a", "c"));
|
// System.out.println(feignRestService.pathVariables("b", "a", "c"));
|
||||||
|
|
||||||
// RestTemplate call
|
// RestTemplate call
|
||||||
System.out.println(restTemplate.getForEntity(
|
System.out.println(restTemplate.getForEntity(
|
||||||
"http://" + providerApplicationName + "//path-variables/{p1}/{p2}?v=c",
|
"http://" + providerApplicationName + "//path-variables/{p1}/{p2}?v=c",
|
||||||
String.class, "a", "b"));
|
String.class, "a", "b"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void callHeaders() {
|
private void callHeaders() {
|
||||||
// Dubbo Service call
|
// Dubbo Service call
|
||||||
System.out.println(restService.headers("a", "b", 10));
|
System.out.println(restService.headers("a", "b", 10));
|
||||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||||
System.out.println(dubboFeignRestService.headers("b", 10, "a"));
|
System.out.println(dubboFeignRestService.headers("b", 10, "a"));
|
||||||
// Spring Cloud Open Feign REST Call
|
// Spring Cloud Open Feign REST Call
|
||||||
// System.out.println(feignRestService.headers("b", "a", 10));
|
// System.out.println(feignRestService.headers("b", "a", 10));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void callParam() {
|
private void callParam() {
|
||||||
// Dubbo Service call
|
// Dubbo Service call
|
||||||
System.out.println(restService.param("mercyblitz"));
|
System.out.println(restService.param("mercyblitz"));
|
||||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||||
System.out.println(dubboFeignRestService.param("mercyblitz"));
|
System.out.println(dubboFeignRestService.param("mercyblitz"));
|
||||||
// Spring Cloud Open Feign REST Call
|
// Spring Cloud Open Feign REST Call
|
||||||
// System.out.println(feignRestService.param("mercyblitz"));
|
// System.out.println(feignRestService.param("mercyblitz"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void callParams() {
|
private void callParams() {
|
||||||
// Dubbo Service call
|
// Dubbo Service call
|
||||||
System.out.println(restService.params(1, "1"));
|
System.out.println(restService.params(1, "1"));
|
||||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||||
System.out.println(dubboFeignRestService.params("1", 1));
|
System.out.println(dubboFeignRestService.params("1", 1));
|
||||||
// Spring Cloud Open Feign REST Call
|
// Spring Cloud Open Feign REST Call
|
||||||
// System.out.println(feignRestService.params("1", 1));
|
// System.out.println(feignRestService.params("1", 1));
|
||||||
|
|
||||||
// RestTemplate call
|
// RestTemplate call
|
||||||
System.out.println(restTemplate.getForEntity(
|
System.out.println(restTemplate.getForEntity(
|
||||||
"http://" + providerApplicationName + "/param?param=小马哥", String.class));
|
"http://" + providerApplicationName + "/param?param=小马哥", String.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void callRequestBodyMap() {
|
private void callRequestBodyMap() {
|
||||||
|
|
||||||
Map<String, Object> data = new HashMap<>();
|
Map<String, Object> data = new HashMap<>();
|
||||||
data.put("id", 1);
|
data.put("id", 1);
|
||||||
data.put("name", "小马哥");
|
data.put("name", "小马哥");
|
||||||
data.put("age", 33);
|
data.put("age", 33);
|
||||||
|
|
||||||
// Dubbo Service call
|
// Dubbo Service call
|
||||||
System.out.println(restService.requestBodyMap(data, "Hello,World"));
|
System.out.println(restService.requestBodyMap(data, "Hello,World"));
|
||||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||||
System.out.println(dubboFeignRestService.requestBody("Hello,World", data));
|
System.out.println(dubboFeignRestService.requestBody("Hello,World", data));
|
||||||
// Spring Cloud Open Feign REST Call
|
// Spring Cloud Open Feign REST Call
|
||||||
// System.out.println(feignRestService.requestBody("Hello,World", data));
|
// System.out.println(feignRestService.requestBody("Hello,World", data));
|
||||||
|
|
||||||
// RestTemplate call
|
// RestTemplate call
|
||||||
System.out.println(restTemplate.postForObject(
|
System.out.println(restTemplate.postForObject(
|
||||||
"http://" + providerApplicationName + "/request/body/map?param=小马哥", data,
|
"http://" + providerApplicationName + "/request/body/map?param=小马哥", data,
|
||||||
User.class));
|
User.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@LoadBalanced
|
@LoadBalanced
|
||||||
@DubboTransported
|
@DubboTransported
|
||||||
public RestTemplate restTemplate() {
|
public RestTemplate restTemplate() {
|
||||||
return new RestTemplate();
|
return new RestTemplate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new SpringApplicationBuilder(DubboSpringCloudConsumerBootstrap.class)
|
new SpringApplicationBuilder(DubboSpringCloudConsumerBootstrap.class)
|
||||||
.properties("spring.profiles.active=nacos").run(args);
|
.properties("spring.profiles.active=nacos").run(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user