Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update producer develop docs #63

Merged
merged 4 commits into from Apr 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 53 additions & 15 deletions _users/cn/develop-with-jax-rs.md
Expand Up @@ -16,21 +16,43 @@ redirect_from:
ServiceComb支持开发者使用JAX-RS注解,使用JAX-RS模式开发服务。

## 开发示例
* **步骤 1** 添加依赖。

* **步骤 1** 定义服务接口。
在Maven的pom.xml中添加所需的依赖:

根据开发之前定义好的契约,编写Java业务接口,代码如下:

```java
public interface Hello {
 String sayHi(String name);
 String sayHello(Person person);
}
```xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-dependencies</artifactId>
<version>1.0.0-m1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--transport根据microservice.yaml中endpoint发布需求选择,本例中两者都引入,也可以二选一-->
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-rest-vertx</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-highway</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-jaxrs</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
```

> **说明**:
该接口的位置需要与契约中x-java-interface所指定的路径一致。

* **步骤 2** 实现服务。

使用JAX-RS注解开发业务代码,Hello的服务实现如下:
Expand All @@ -40,22 +62,19 @@ ServiceComb支持开发者使用JAX-RS注解,使用JAX-RS模式开发服务。
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.servicecomb.samples.common.schema.Hello;
import org.apache.servicecomb.samples.common.schema.models.Person;

@Path("/jaxrshello")
@Produces(MediaType.APPLICATION_JSON)
public class JaxrsHelloImpl implements Hello {
public class JaxrsHelloImpl {
@Path("/sayhi")
@POST
@Override
public String sayHi(String name) {
 return "Hello " + name;
}

@Path("/sayhello")
@POST
@Override
public String sayHello(Person person) {
return "Hello person " + person.getName();
}
Expand Down Expand Up @@ -89,6 +108,25 @@ ServiceComb支持开发者使用JAX-RS注解,使用JAX-RS模式开发服务。
</beans>
```

* **步骤 4** 添加服务定义。

在resources目录中添加[microservice.yaml](http://servicecomb.incubator.apache.org/cn/users/service-definition/)。

* **步骤 5** 添加Main启动类

```java
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.apache.servicecomb.foundation.common.utils.Log4jUtils;

public class Application {
public static void main(String[] args) throws Exception {
//初始化日志, 加载Bean(包括它们的参数), 以及注册Service, 更多信息可以参见文档 : http://servicecomb.incubator.apache.org/cn/users/application-boot-process/
Log4jUtils.init();
BeanUtils.init();
}
}
```

## 涉及API

JAX-RS开发模式当前支持如下注解,所有注解的使用方法参考[JAX-RS官方文档](https://jax-rs-spec.java.net/nonav/2.0-rev-a/apidocs/index.html)。
Expand Down
39 changes: 39 additions & 0 deletions _users/cn/develop-with-rpc.md
Expand Up @@ -48,3 +48,42 @@ public class CodeFirstConsumerMain {
```

在以上代码中,服务消费者已经取得了服务提供者的服务接口`Hello`,并在代码中声明一个`Hello`类型的成员。通过在`hello`上使用`@RPCReference`注解指明微服务名称和schemaId,ServiceComb框架可以在程序启动时从服务中心获取到对应的服务提供者实例信息,并且生成一个代理注入到hello中,用户可以像调用本地类一样调用远程服务。

### 调用方式的补充说明
上面示例代码中,为了能在main函数中直接使用hello变量,我们将它标记为`static`。作为CodeFirstConsumerMain这个Bean的本地变量,我们更推荐下面两种做法:
#### 方式1:通过cse:rpc-reference定义
在你的bean.xml中添加cse:rpc-reference的配置项:

```xml
<cse:rpc-reference id="hello" microservice-name="codefirst"
schema-id="codeFirstHello" interface="org.apache.servicecomb.samples.common.schema.Hello"></cse:rpc-reference>
```

然后就可以使用`BeanUtils.getBean`直接获取服务提供者的服务接口`Hello`:

```java
Hello hello = BeanUtils.getBean("hello");
```

#### 方式2:获取Bean,再获取接口
先使用`BeanUtils.getBean`获取到CodeFirstConsumerMain这个Bean:

```java
//Spring Bean 实例默认名为类名的小写
CodeFirstConsumerMain consumer = BeanUtils.getBean("codeFirstConsumerMain");
```

然后按Getter的方式获取hello:

```java
public Hello getHello() {
return hello;
}
```

```java
Hello hello = consumer.getHello()
```

> 说明:
> `BeanUtils.getBean`有锁,因此有性能问题,无论是哪种方式,推荐一次调用(例如在构造函数中)获取缓存起来作为一个本地变量反复使用。
69 changes: 54 additions & 15 deletions _users/cn/develop-with-springmvc.md
Expand Up @@ -15,20 +15,43 @@ redirect_from:
ServiceComb支持SpringMVC注解,允许使用SpringMVC风格开发微服务。

## 开发示例
* **步骤 1** 添加依赖。

* **步骤 1** 定义服务接口。
在Maven的pom.xml中添加所需的依赖:

根据开发之前定义好的契约,编写Java业务接口,代码如下:

```java
public interface Hello {
String sayHi(String name);
String sayHello(Person person);
}
```xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-dependencies</artifactId>
<version>1.0.0-m1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--transport根据microservice.yaml中endpoint发布需求选择,本例中两者都引入,也可以二选一-->
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-rest-vertx</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-highway</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-springmvc</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
```

该接口的位置需要与契约中x-java-interface所指定的路径一致。

* **步骤 2** 实现服务。

使用Spring MVC注解开发业务代码,Hello的服务实现如下:
Expand All @@ -39,18 +62,15 @@ ServiceComb支持SpringMVC注解,允许使用SpringMVC风格开发微服务。
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.apache.servicecomb.samples.common.schema.Hello;
import org.apache.servicecomb.samples.common.schema.models.Person;

@RequestMapping(path = "/springmvchello", produces = MediaType.APPLICATION_JSON)
public class SpringmvcHelloImpl implements Hello {
@Override
public class SpringmvcHelloImpl {
@RequestMapping(path = "/sayhi", method = RequestMethod.POST)
public String sayHi(@RequestParam(name = "name") String name) {
  return "Hello " + name;
}

@Override
@RequestMapping(path = "/sayhello", method = RequestMethod.POST)
public String sayHello(@RequestBody Person person) {
  return "Hello person " + person.getName();
Expand All @@ -66,7 +86,7 @@ ServiceComb支持SpringMVC注解,允许使用SpringMVC风格开发微服务。
import org.apache.servicecomb.provider.rest.common.RestSchema;
// other code omitted
@RestSchema(schemaId = "springmvcHello")
public class SpringmvcHelloImpl implements Hello {
public class SpringmvcHelloImpl {
// other code omitted
}
```
Expand All @@ -86,6 +106,25 @@ ServiceComb支持SpringMVC注解,允许使用SpringMVC风格开发微服务。
</beans>
```

* **步骤 4** 添加服务定义。

在resources目录中添加[microservice.yaml](http://servicecomb.incubator.apache.org/cn/users/service-definition/)。

* **步骤 5** 添加Main启动类

```java
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.apache.servicecomb.foundation.common.utils.Log4jUtils;

public class Application {
public static void main(String[] args) throws Exception {
//初始化日志, 加载Bean(包括它们的参数), 以及注册Service, 更多信息可以参见文档 : http://servicecomb.incubator.apache.org/cn/users/application-boot-process/
Log4jUtils.init();
BeanUtils.init();
}
}
```

## 涉及API

Spring MVC开发模式当前支持org.springframework.web.bind.annotation包下的如下注解,所有注解的使用方法参考[Spring MVC官方文档](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html)。
Expand Down
80 changes: 64 additions & 16 deletions _users/cn/develop-with-transparent-rpc.md
Expand Up @@ -15,10 +15,44 @@ redirect_from:
透明RPC开发模式是一种基于接口和接口实现的开发模式,服务的开发者不需要使用Spring MVC和JAX-RS注解。

## 开发示例
* **步骤 1** 添加依赖:

透明RPC开发模式支持Spring xml配置和注解配置两种服务发布方式,通过Spring xml配置的方式如下
在Maven的pom.xml中添加所需的依赖

* **步骤 1** 定义服务接口。
```xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-dependencies</artifactId>
<version>1.0.0-m1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--transport根据microservice.yaml中endpoint发布需求选择,本例中两者都引入,也可以二选一-->
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-rest-vertx</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>transport-highway</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>provider-pojo</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
```

* **步骤 2** 定义服务接口:

根据开发之前定义好的契约,编写Java业务接口,代码如下:

Expand All @@ -29,10 +63,7 @@ redirect_from:
}
```

> **说明**:
> 该接口的位置需要与契约中x-java-interface所指定的路径一致。

* **步骤 2** 实现服务
* **步骤 3** 实现服务:

Hello的服务实现如下:

Expand All @@ -53,8 +84,11 @@ redirect_from:
}
```

* **步骤 3** 发布服务
* **步骤 4** 发布服务

透明RPC开发模式支持Spring xml配置和注解配置两种服务发布方式:

1. 使用Spring xml配置方式:
在resources/META-INF/spring目录下创建pojoHello.bean.xml文件,在文件中声明schema,文件内容如下:

```xml
Expand All @@ -64,19 +98,14 @@ redirect_from:
xmlns:cse=" http://www.huawei.com/schema/paas/cse/rpc "
xmlns:context=" http://www.springframework.org/schema/context "
xsi:schemaLocation=" http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.huawei.com/schema/paas/cse/rpc classpath:META-INF/spring/spring-paas-cse-rpc.xsd">

<cse:rpc-schema schema-id="pojoHello" implementation="org.apache.servicecomb.samples.pojo.provider.PojoHelloImpl"/>
</beans>
```

> **说明**:
> 每一个服务接口都需要定义一个schema声明。

## 通过注解配置的开发方式
2. 使用注解配置方式:

1. 定义服务接口,与使用Spring xml的方式相同。
2. 实现服务,与使用Spring xml的方式相同。
3. 发布服务。在接口Hello的实现类上使用@RpcSchema注解定义schema,代码如下:
在接口Hello的实现类上使用@RpcSchema注解定义schema,代码如下:

```java
import org.apache.servicecomb.provider.pojo.RpcSchema;
Expand All @@ -102,4 +131,23 @@ redirect_from:
```

> **说明**:
> 与Spring MVC开发模式和JAX-RS开发模式不同的是,透明RPC开发模式使用的注解是`@RpcSchema`而非`@RestSchema`。
与Spring MVC开发模式和JAX-RS开发模式不同的是,透明RPC开发模式使用的注解是`@RpcSchema`而非`@RestSchema`。

* **步骤 5** 添加服务定义。

在resources目录中添加[microservice.yaml](http://servicecomb.incubator.apache.org/cn/users/service-definition/)。

* **步骤 6** 添加Main启动类

```java
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.apache.servicecomb.foundation.common.utils.Log4jUtils;

public class Application {
public static void main(String[] args) throws Exception {
//初始化日志, 加载Bean(包括它们的参数), 以及注册Service, 更多信息可以参见文档 : http://servicecomb.incubator.apache.org/cn/users/application-boot-process/
Log4jUtils.init();
BeanUtils.init();
}
}
```