mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish : /spring-cloud-incubator/spring-cloud-alibaba#386 : supports Spring Cloud Netflix Eureka and Spring Cloud Zookeeper
This commit is contained in:
@@ -1,222 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.alibaba.dubbo.bootstrap;
|
||||
|
||||
import com.alibaba.dubbo.config.annotation.Reference;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.alibaba.dubbo.annotation.DubboTransported;
|
||||
import org.springframework.cloud.alibaba.dubbo.service.RestService;
|
||||
import org.springframework.cloud.alibaba.dubbo.service.User;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
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.context.annotation.Lazy;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
|
||||
|
||||
/**
|
||||
* Dubbo Spring Cloud Bootstrap
|
||||
*/
|
||||
@EnableDiscoveryClient
|
||||
@EnableAutoConfiguration
|
||||
@EnableFeignClients
|
||||
@RestController
|
||||
public class DubboSpringCloudBootstrap {
|
||||
|
||||
@Reference(version = "1.0.0")
|
||||
private RestService restService;
|
||||
|
||||
@Autowired
|
||||
@Lazy
|
||||
private FeignRestService feignRestService;
|
||||
|
||||
@Autowired
|
||||
@Lazy
|
||||
private DubboFeignRestService dubboFeignRestService;
|
||||
|
||||
@Autowired
|
||||
@LoadBalanced
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@FeignClient("spring-cloud-alibaba-dubbo")
|
||||
public interface FeignRestService {
|
||||
|
||||
@GetMapping(value = "/param")
|
||||
String param(@RequestParam("param") String param);
|
||||
|
||||
@PostMapping("/params")
|
||||
public String params(@RequestParam("b") String b, @RequestParam("a") int a);
|
||||
|
||||
@PostMapping(value = "/request/body/map", produces = APPLICATION_JSON_UTF8_VALUE)
|
||||
User requestBody(@RequestParam("param") String param, @RequestBody Map<String, Object> data);
|
||||
|
||||
@GetMapping("/headers")
|
||||
@Path("/headers")
|
||||
@GET
|
||||
public String headers(@RequestHeader("h2") String header2,
|
||||
@RequestHeader("h") String header,
|
||||
@RequestParam("v") Integer value);
|
||||
|
||||
@GetMapping("/path-variables/{p1}/{p2}")
|
||||
public String pathVariables(@PathVariable("p2") String path2,
|
||||
@PathVariable("p1") String path1,
|
||||
@RequestParam("v") String param);
|
||||
}
|
||||
|
||||
@FeignClient("spring-cloud-alibaba-dubbo")
|
||||
@DubboTransported
|
||||
public interface DubboFeignRestService {
|
||||
|
||||
@GetMapping(value = "/param")
|
||||
String param(@RequestParam("param") String param);
|
||||
|
||||
@PostMapping("/params")
|
||||
String params(@RequestParam("b") String paramB, @RequestParam("a") int paramA);
|
||||
|
||||
@PostMapping(value = "/request/body/map", produces = APPLICATION_JSON_UTF8_VALUE)
|
||||
User requestBody(@RequestParam("param") String param, @RequestBody Map<String, Object> data);
|
||||
|
||||
@GetMapping("/headers")
|
||||
@Path("/headers")
|
||||
@GET
|
||||
public String headers(@RequestHeader("h2") String header2,
|
||||
@RequestParam("v") Integer value,
|
||||
@RequestHeader("h") String header);
|
||||
|
||||
@GetMapping("/path-variables/{p1}/{p2}")
|
||||
public String pathVariables(@RequestParam("v") String param,
|
||||
@PathVariable("p2") String path2,
|
||||
@PathVariable("p1") String path1);
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public ApplicationRunner paramRunner() {
|
||||
return arguments -> {
|
||||
|
||||
// To call /path-variables
|
||||
callPathVariables();
|
||||
|
||||
// To call /headers
|
||||
callHeaders();
|
||||
|
||||
// To call /param
|
||||
callParam();
|
||||
|
||||
// To call /params
|
||||
callParams();
|
||||
|
||||
// To call /request/body/map
|
||||
callRequestBodyMap();
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
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"));
|
||||
|
||||
// RestTemplate call
|
||||
System.out.println(restTemplate.getForEntity("http://spring-cloud-alibaba-dubbo//path-variables/{p1}/{p2}?v=c", String.class, "a", "b"));
|
||||
}
|
||||
|
||||
private void callHeaders() {
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.headers("a", "b", 10));
|
||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||
System.out.println(dubboFeignRestService.headers("b", 10, "a"));
|
||||
// Spring Cloud Open Feign REST Call
|
||||
System.out.println(feignRestService.headers("b", "a", 10));
|
||||
}
|
||||
|
||||
private void callParam() {
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.param("mercyblitz"));
|
||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||
System.out.println(dubboFeignRestService.param("mercyblitz"));
|
||||
// Spring Cloud Open Feign REST Call
|
||||
System.out.println(feignRestService.param("mercyblitz"));
|
||||
}
|
||||
|
||||
private void callParams() {
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.params(1, "1"));
|
||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||
System.out.println(dubboFeignRestService.params("1", 1));
|
||||
// Spring Cloud Open Feign REST Call
|
||||
System.out.println(feignRestService.params("1", 1));
|
||||
|
||||
// RestTemplate call
|
||||
System.out.println(restTemplate.getForEntity("http://spring-cloud-alibaba-dubbo/param?param=小马哥", String.class));
|
||||
}
|
||||
|
||||
private void callRequestBodyMap() {
|
||||
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("id", 1);
|
||||
data.put("name", "小马哥");
|
||||
data.put("age", 33);
|
||||
|
||||
// Dubbo Service call
|
||||
System.out.println(restService.requestBodyMap(data, "Hello,World"));
|
||||
// Spring Cloud Open Feign REST Call (Dubbo Transported)
|
||||
// System.out.println(dubboFeignRestService.requestBody("Hello,World", data));
|
||||
// Spring Cloud Open Feign REST Call
|
||||
System.out.println(feignRestService.requestBody("Hello,World", data));
|
||||
|
||||
// RestTemplate call
|
||||
System.out.println(restTemplate.postForObject("http://spring-cloud-alibaba-dubbo/request/body/map?param=小马哥", data, User.class));
|
||||
}
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
@DubboTransported
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(DubboSpringCloudBootstrap.class)
|
||||
.run(args);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.alibaba.dubbo.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Echo Service
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
public interface RestService {
|
||||
|
||||
String param(String param);
|
||||
|
||||
String params(int a, String b);
|
||||
|
||||
String headers(String header, String header2, Integer param);
|
||||
|
||||
String pathVariables(String path1, String path2, String param);
|
||||
|
||||
String form(String form);
|
||||
|
||||
User requestBodyMap(Map<String, Object> data, String param);
|
||||
|
||||
Map<String, Object> requestBodyUser(User user);
|
||||
|
||||
}
|
@@ -1,149 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.alibaba.dubbo.service;
|
||||
|
||||
import com.alibaba.dubbo.rpc.RpcContext;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.HeaderParam;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
|
||||
import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;
|
||||
|
||||
/**
|
||||
* Default {@link RestService}
|
||||
*
|
||||
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
|
||||
*/
|
||||
@com.alibaba.dubbo.config.annotation.Service(version = "1.0.0", protocol = {"dubbo", "rest"})
|
||||
@RestController
|
||||
@Path("/")
|
||||
public class StandardRestService implements RestService {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Override
|
||||
@GetMapping(value = "/param")
|
||||
@Path("/param")
|
||||
@GET
|
||||
public String param(@RequestParam @QueryParam("param") String param) {
|
||||
log("/param", param);
|
||||
return param;
|
||||
}
|
||||
|
||||
@Override
|
||||
@PostMapping("/params")
|
||||
@Path("/params")
|
||||
@POST
|
||||
public String params(@RequestParam @QueryParam("a") int a, @RequestParam @QueryParam("b") String b) {
|
||||
log("/params", a + b);
|
||||
return a + b;
|
||||
}
|
||||
|
||||
@Override
|
||||
@GetMapping("/headers")
|
||||
@Path("/headers")
|
||||
@GET
|
||||
public String headers(@RequestHeader("h") @HeaderParam("h") String header,
|
||||
@RequestHeader("h2") @HeaderParam("h2") String header2,
|
||||
@RequestParam("v") @QueryParam("v") Integer param) {
|
||||
String result = header + " , " + header2 + " , " + param;
|
||||
log("/headers", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@GetMapping("/path-variables/{p1}/{p2}")
|
||||
@Path("/path-variables/{p1}/{p2}")
|
||||
@GET
|
||||
public String pathVariables(@PathVariable("p1") @PathParam("p1") String path1,
|
||||
@PathVariable("p2") @PathParam("p2") String path2,
|
||||
@RequestParam("v") @QueryParam("v") String param) {
|
||||
String result = path1 + " , " + path2 + " , " + param;
|
||||
log("/path-variables", result);
|
||||
return result;
|
||||
}
|
||||
|
||||
// @CookieParam does not support : https://github.com/OpenFeign/feign/issues/913
|
||||
// @CookieValue also does not support
|
||||
|
||||
@Override
|
||||
@PostMapping("/form")
|
||||
@Path("/form")
|
||||
@POST
|
||||
public String form(@RequestParam("f") @FormParam("f") String form) {
|
||||
return String.valueOf(form);
|
||||
}
|
||||
|
||||
@Override
|
||||
@PostMapping(value = "/request/body/map", produces = APPLICATION_JSON_UTF8_VALUE)
|
||||
@Path("/request/body/map")
|
||||
@POST
|
||||
@Produces(APPLICATION_JSON_VALUE)
|
||||
public User requestBodyMap(@RequestBody Map<String, Object> data, @RequestParam("param") @QueryParam("param") String param) {
|
||||
User user = new User();
|
||||
user.setId(((Integer) data.get("id")).longValue());
|
||||
user.setName((String) data.get("name"));
|
||||
user.setAge((Integer) data.get("age"));
|
||||
log("/request/body/map", param);
|
||||
return user;
|
||||
}
|
||||
|
||||
@PostMapping(value = "/request/body/user", consumes = APPLICATION_JSON_UTF8_VALUE)
|
||||
@Path("/request/body/user")
|
||||
@POST
|
||||
@Override
|
||||
@Consumes(APPLICATION_JSON_UTF8_VALUE)
|
||||
public Map<String, Object> requestBodyUser(@RequestBody User user) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", user.getId());
|
||||
map.put("name", user.getName());
|
||||
map.put("age", user.getAge());
|
||||
return map;
|
||||
}
|
||||
|
||||
private void log(String url, Object result) {
|
||||
String message = String.format("The client[%s] uses '%s' protocol to call %s : %s",
|
||||
RpcContext.getContext().getRemoteHostName(),
|
||||
RpcContext.getContext().getUrl() == null ? "N/A" : RpcContext.getContext().getUrl().getProtocol(),
|
||||
url,
|
||||
result
|
||||
);
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info(message);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.cloud.alibaba.dubbo.service;
|
||||
|
||||
import javax.ws.rs.FormParam;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* User Entity
|
||||
*/
|
||||
public class User implements Serializable {
|
||||
|
||||
@FormParam("id")
|
||||
private Long id;
|
||||
|
||||
@FormParam("name")
|
||||
private String name;
|
||||
|
||||
@FormParam("age")
|
||||
private Integer age;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", age=" + age +
|
||||
'}';
|
||||
}
|
||||
}
|
@@ -7,7 +7,7 @@ dubbo:
|
||||
port: 12345
|
||||
rest:
|
||||
name: rest
|
||||
port: 9090
|
||||
port: 8081
|
||||
server: netty
|
||||
registry:
|
||||
address: spring-cloud://nacos
|
||||
|
@@ -7,6 +7,9 @@ spring:
|
||||
server-addr: 127.0.0.1:8848
|
||||
config:
|
||||
server-addr: 127.0.0.1:8848
|
||||
zookeeper:
|
||||
enabled: false
|
||||
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
|
||||
@@ -27,4 +30,18 @@ eureka:
|
||||
client:
|
||||
enabled: true
|
||||
service-url:
|
||||
defaultZone: http://127.0.0.1:8761/eureka/
|
||||
defaultZone: http://localhost:9090/eureka/
|
||||
|
||||
---
|
||||
spring:
|
||||
profiles: zookeeper
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: false
|
||||
register-enabled: false
|
||||
|
||||
zookeeper:
|
||||
enabled: true
|
||||
connect-string: localhost:2181
|
Reference in New Issue
Block a user