This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-docs.git
commit 4e90180ce463a09fa023597e9e18fdfdd3159330 Author: DeanLee <[email protected]> AuthorDate: Tue Aug 21 18:05:07 2018 +0800 update java-chassis-reference/en_US/build-consumer/using-resttemplate.md --- .../en_US/build-consumer/using-resttemplate.md | 60 +++++++--------------- 1 file changed, 19 insertions(+), 41 deletions(-) diff --git a/java-chassis-reference/en_US/build-consumer/using-resttemplate.md b/java-chassis-reference/en_US/build-consumer/using-resttemplate.md index 8f4528b..879ec94 100644 --- a/java-chassis-reference/en_US/build-consumer/using-resttemplate.md +++ b/java-chassis-reference/en_US/build-consumer/using-resttemplate.md @@ -1,20 +1,25 @@ -# 使用RestTemplate开发服务消费者 +# Develop with Rest Template +## Concept Description -## 概念阐述 + Rest Template is a RESTful API provide by the Spring framework. ServiceComb provides the API for service calling -RestTemplate是Spring提供的RESTful访问接口,ServiceComb提供该接口的实现类用于服务的调用。 +## Scenario -## 场景描述 + You can call microservices using your customized URL and the RestTemplate instance provided by ServiceComb regardless of the specific address of the service. -用户使用ServiceComb提供的RestTemplate实例,可以使用自定义的URL进行服务调用,而不用关心服务的具体地址。 +## Sample Code -## 示例代码 + Obtain RestTemplate by calling `RestTemplateBuilder.create()`. Then, use the instance and the customized URL to call microservices. The code is as follows: -RestTemplate实例通过调用`RestTemplateBuilder.create()`方法获取,再使用该实例通过自定义的URL进行服务调用,代码如下: +```java +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; -* Spring MVC 客户端示例代码: +import org.apache.servicecomb.foundation.common.utils.BeanUtils; +import org.apache.servicecomb.foundation.common.utils.Log4jUtils; +import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder; +import org.apache.servicecomb.samples.common.schema.models.Person; -```java @Component public class SpringmvcConsumerMain { private static RestTemplate restTemplate = RestTemplateBuilder.create(); @@ -38,36 +43,10 @@ public class SpringmvcConsumerMain { } ``` -* JAX RS 客户端示例代码: - -```java - -@Component -public class JaxrsConsumerMain { - - public static void main(String[] args) throws Exception { - init(); - //其他都类似spring MVC示例的客户端代码,注意如果服务端只接收 GET 请求,要使用方法 getForObject() - RestTemplate restTemplate = RestTemplateBuilder.create(); - String result = restTemplate.getForObject("cse://jaxrs/jaxrshello/saybye", String.class); - } - - public static void init() throws Exception { - Log4jUtils.init(); - BeanUtils.init(); - } -} -``` - -> 说明: -> -> * URL格式为:`cse://microserviceName/path?querystring`。以[用SpringMVC开发微服务](/用SpringMVC开发微服务)中定义的服务提供者为例,其微服务名称是springmvc,basePath是`/springmvchello`,那么URL中的microserviceName=`springmvc`,请求sayhi时的path=`springmvchello/sayhi`,所以示例代码中请求sayhi的URL是`cse://springmvc/springmvchello/sayhi?name=Java Chassis`。具体代码示例如下 : - - ```java @RestSchema(schemaId = "springmvcHello") @RequestMapping(path = "/springmvchello", produces = MediaType.APPLICATION_JSON) -//这里 path = “/springmvchello” 中的 springmvchello 就是 上述的basePath +//Here, springmvchello in path = "/springmvchello" is the above basePath. public class SpringmvcHelloImpl implements Hello { @Override @RequestMapping(path = "/sayhi", method = RequestMethod.POST) @@ -82,7 +61,7 @@ public class SpringmvcHelloImpl implements Hello { } } ``` -> 下述代码是示例项目 [ SpringMVC ](https://github.com/apache/incubator-servicecomb-java-chassis/tree/master/samples/springmvc-sample)的 springmvc-provider 模块 中 resources 目录下 microservice.yaml +> The following code is microservice.yaml in the resources directory of the springmvc-provider module of the sample project [Spring MVC] (https://github.com/apache/incubator-service comb-java-chassis/tree/master/samples/springmvc-sample) ```yaml APPLICATION_ID: springmvc-sample @@ -107,7 +86,6 @@ cse: address: http://127.0.0.1:30100 #service center address ``` - - - -> * 使用上述这种URL格式,ServiceComb框架会在内部进行服务发现、熔断容错等处理并最终将请求发送到服务提供者。 +> NOTE: +- The URL must be in format of ServiceComb: `cse://microserviceName/path?querystring`. Use the provider defined in [Develop Microservice with SpringMVC](/users/develop-with-springmvc/) as an example. The microservice name is `springmvc`, and its base path is `/springmvchello`, so you should set microserviceName in the URL to `springmvchello/sayhi` when requesting sayhi. Therefore, the URL for requesting sayhi is `cse://springmvc/springmvchello/sayhi?name=Java Chassis`. +- During use of this URL format, the ServiceComb framework will perform internal microservice descovery, fallbreak, and fault tolerance and send the requests to the microservice providers.
