Skip to content

Commit

Permalink
update porter to 3.0.0 (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored Nov 23, 2023
1 parent fe64185 commit cec906e
Show file tree
Hide file tree
Showing 41 changed files with 464 additions and 669 deletions.
131 changes: 10 additions & 121 deletions porter_lightweight/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
This project shows how to using java-chassis and service-center to create a simple microservice application. It shows basic authentication and authorization, uploading files and delete files operations.
This project shows how to use Java Chassis to create a simple microservice application. It shows basic authentication and authorization, uploading files and delete files operations.

## Precondition
see [Precondition](../README.md)
Expand All @@ -12,20 +12,19 @@ mvn clean install
```

* run
* create a database using mysql with user name and password(e.g. root/root)
* create a database using mysql with username and password(e.g. root/root)
* execute create_db_user.sql

* run user-service:

```
java $JAVA_OPT -Ddb.url="jdbc:mysql://localhost/porter_user_db?useSSL=false" -Ddb.username=root -Ddb.password=root -jar porter-user-service-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
java $JAVA_OPT -Ddb.url="jdbc:mysql://localhost/porter_user_db?useSSL=false" -Ddb.username=root -Ddb.password=root -jar porter-user-service-0.0.1-SNAPSHOT.jar
```
Note: If startup fails, try removing "$JAVA_OPT" and ">/dev/null 2>&1 &" (same below)

* run file-service:

```
java $JAVA_OPT -jar porter-file-service-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
java $JAVA_OPT -jar porter-file-service-0.0.1-SNAPSHOT.jar
```

* run gateway-service:
Expand All @@ -35,12 +34,12 @@ gateway-service contains static web pages in resources/ui. First copy to web roo
Is the location of its own native code).

```
java $JAVA_OPT -Dgateway.webroot=webapp -jar porter-gateway-service-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
java $JAVA_OPT -Dgateway.webroot=webapp -jar porter-gateway-service-0.0.1-SNAPSHOT.jar
```

## Try it

1. Using browser: http://localhost:9090/ui/login.html with user admin or guest to login, password is test.
1. Using browser: http://localhost:9090/ui/login.html with user admin or guest, password is test.
2. Choose a file to upload. Uploaded file is stored in file-service working directory. And name is random number generated.
3. Delete file. Input the random number generated in step 2.

Expand All @@ -67,22 +66,22 @@ mvn clean install
* 启动user-service:

```
java $JAVA_OPT -Ddb.url="jdbc:mysql://localhost/porter_user_db?useSSL=false" -Ddb.username=root -Ddb.password=root -jar porter-user-service-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
java $JAVA_OPT porter-user-service-0.0.1-SNAPSHOT.jar
```
注意:如果启动不了,可以尝试去掉“$JAVA_OPT”和“>/dev/null 2>&1 &”(下同)

* 启动file-service:

```
java $JAVA_OPT -jar porter-file-service-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
java $JAVA_OPT -jar porter-file-service-0.0.1-SNAPSHOT.jar
```

* 启动gateway-serivce:

gateway-service包含了静态页面文件,在resources/ui目录。首先需要将页面文件拷贝到WEB主目录(相对路径,当前运行目录),比如: webapp,然后将ui目录整体拷贝到webapp/ui目录(注意:porter_lightweight/gateway-service/src/main/resources/microservice.yaml中的相关配置
gateway:webroot: /code/servicecomb-samples/porter_lightweight/gateway-service/src/main/resources是自己本地代码的位置)。启动:

```
java $JAVA_OPT -Dgateway.webroot=webapp -jar porter-gateway-service-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
java $JAVA_OPT -Dgateway.webroot=webapp -jar porter-gateway-service-0.0.1-SNAPSHOT.jar
```

# 使用
Expand All @@ -91,113 +90,3 @@ java $JAVA_OPT -Dgateway.webroot=webapp -jar porter-gateway-service-0.0.1-SNAPSH
2. 选择一个文件上传,上传成功,上传成功后的文件会保存在file-service的当前目录, 文件名称是一个随机的数字,这个数字就是文件ID。
3. 删除文件:输入上一步的文件ID,点击删除。 如果是admin用户,上传成功;如果是guest用户,上传失败。

# 接口使用说明
ServiceComb推荐先定义接口,再定义实现的开发模式。将接口定义为一个独立项目,可以由设计者统一管控,对于接口的修改,需要设计者进行审核。先定义接口还可以让开发者培养良好的开发习惯,避免将对外接口采用内部实现数据结构(比如JsonObject)、运行平台有关的数据结构(比如HttpServletResponse、HttpServletRequest)来定义,使得以后将项目改造为其他技术框架变得复杂。采用这种方式组织的项目,用户很容易在不同的开发框架上进行迁移,比如gRPC、Spring Cloud等。这里的接口定义代码,对于这些运行框架都是通用的,并且具备跨平台特性。

## 对于接口实现者(provider)
* 依赖api对应的jar包

```
<dependencies>
<dependency>
<groupId>org.apache.servicecomb.samples.porter</groupId>
<artifactId>user-service-api-endpoint</artifactId>
</dependency>
</dependencies>
```

* 实现Service接口
```
@Service
public class UserServiceImpl implements UserService
```

## 对于接口使用者(consumer)
* 依赖service对应的jar包(只包含model和接口)

```
<dependencies>
<dependency>
<groupId>org.apache.servicecomb.samples.porter</groupId>
<artifactId>user-service-api-service</artifactId>
</dependency>
</dependencies>
```

* 采用RPC方式调用
```
@RpcReference(microserviceName = "user-service", schemaId = "user")
private static UserService sserService
```

# REST接口常见争议问题和处理

## 查询接口参数很复杂

比如:

```
public List<Goods> queryGoodsByTags(String orgId, List<GoodsTag> tags)
```

当查询参数很复杂的时候,不建议采用query参数或者path参数。主要有如下原因:

* HTTP对于URL的长度有限制
* 复杂参数可能包含特殊字符,需要客户端在拼接URL的时候,进行URL转码,客户端开发者通常会遗漏。
* 对象无法和query参数进行合理的映射。
* query参数或者path参数会被proxy、web server等记录到日志里面,导致日志过大,或者造成敏感信息泄露。

对于复杂的查询操作,建议使用POST方法,相关复杂参数都封装为body。比如:

```
@PostMapping(path = "queryGoodsByTags")
public List<Goods> queryGoodsByTags(@RequestParam(name = "orgId") String orgId, @RequestBody List<GoodsTag> tags)
```

一般的,通过query传递参数的场景,尽可能要保证参数个数少,参数类型为基础类型(字符串、数字等)。参数比较多的场景采用POST来传参。

本项目的处理原则是“尽可能遵循HTTP REST语义,但是不盲目,以系统可靠优先”。

对于DELETE请求,也有类似的情况。在设计GET和DELETE方法时,建议都不使用body参数,尽管HTTP协议并没有强制要求不能使用body,但是由于历史因素,很多WEB服务器支持上会有问题,接口设计应该尽可能避免不必要的麻烦和陷阱。只在POST、PUT、PATCH方法中使用body参数。

## REST接口的Path是否包含操作

比如:下面的接口定义path是否包含deleteGoodsUnitConvertor。

```
@DeleteMapping(path = "deleteGoodsUnitConvertor")
public ResponseBase deleteGoodsUnitConvertor(String goodsUnitConvertorId)
```

由于HTTP的方法POST/PUT/PATCH/GET/DELETE已经包含了增、改、查、删语义,path里面包含delete显得多余。不过由于项目的接口通常比较多,过多的思考接口语义反而增加了理解的难度。所以本项目path全部都包含了方法名字。包含名称有个好处,可以从URL中看出operation id,从而很简单的将URL和契约对应起来,方便查找。此外就是上面提到的原因,并不是所有的删除操作都一定对应于DELETE操作,出于系统可靠性、安全等方面考虑,可能使用POST/PATCH等代表查询或者删除操作。

## 多个对象参数

由于HTTP只能有一个body,所有多个对象参数需要包装为一个参数。 比如:

```
public ResponseBase inboundOrder(InboundOrder inboundOrder, Set<InboundOrderItem> inboundOrderItems)
```

封装为下面的REST接口定义:

```
@PostMapping(path = "inboundOrder")
public ResponseBase inboundOrder(@RequestBody InboundOrderRequest inboundOrderRequest) {
return stockService.inboundOrder(inboundOrderRequest.getInboundOrder(), inboundOrderRequest.getInboundOrderItems());
}
```

## 敏感信息不能采用query参数

Query参数可能被各种proxy、web server记录,因此对于用户敏感信息,不能使用query参数。 比如:

```
public ResponseBase rechargePrepaidCard(String cardId, double amount)
```

涉及到卡号和金额的数据,需要采用POST提交,参数存储在body。 虽然有些接口仅仅只是查询, 但也可能被设计为POST。 调整后的接口:

```
public ResponseBase rechargePrepaidCard(@RequestBody PrepaidAmountRequest prepaidAmountRequest)
```
2 changes: 1 addition & 1 deletion porter_lightweight/api/common/endpoint/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.servicecomb.samples.porter</groupId>
<artifactId>common-api</artifactId>
<version>2.8.2</version>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>common-api-endpoint</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,19 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.servicecomb.foundation.common.LegacyPropertyFactory;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.apache.servicecomb.samples.porter.common.api.LogService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.netflix.config.DynamicPropertyFactory;

@RestSchema(schemaId = "log")
@RequestMapping(path = "/v1/log")
public class LogEndpoint implements LogService {
// protect your file in real applications
private static final File LOG_DIR =
new File(DynamicPropertyFactory.getInstance().getStringProperty("servicecomb.samples.logdir", ".").get());
new File(LegacyPropertyFactory.getStringProperty("servicecomb.samples.logdir", "."));

private static final String FILE_POST_FIX = ".log";

Expand Down
2 changes: 1 addition & 1 deletion porter_lightweight/api/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.servicecomb.samples.porter</groupId>
<artifactId>porter-api</artifactId>
<version>2.8.2</version>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>common-api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion porter_lightweight/api/common/service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.servicecomb.samples.porter</groupId>
<artifactId>common-api</artifactId>
<version>2.8.2</version>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>common-api-service</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion porter_lightweight/api/file-service/endpoint/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.servicecomb.samples.porter</groupId>
<artifactId>file-service-api</artifactId>
<version>2.8.2</version>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>file-service-api-endpoint</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestSchema(schemaId = "InternalAccessEndpoint")
@RequestMapping(path = "/")
@Api(tags = {"INTERNAL"})
@OpenAPIDefinition(tags = {@Tag(name = "INTERNAL")})
public class InternalAccessEndpoint {
@Autowired
private InternalAccessService internalAccessService;

@GetMapping(path = "localAccess")
public String localAccess(String name) {
return internalAccessService.localAccess(name);
Expand Down
2 changes: 1 addition & 1 deletion porter_lightweight/api/file-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.servicecomb.samples.porter</groupId>
<artifactId>porter-api</artifactId>
<version>2.8.2</version>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>file-service-api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion porter_lightweight/api/file-service/service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.servicecomb.samples.porter</groupId>
<artifactId>file-service-api</artifactId>
<version>2.8.2</version>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>file-service-api-service</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion porter_lightweight/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.servicecomb.samples.porter</groupId>
<artifactId>porter-application</artifactId>
<version>2.8.2</version>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>porter-api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion porter_lightweight/api/user-service/endpoint/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.servicecomb.samples.porter</groupId>
<artifactId>user-service-api</artifactId>
<version>2.8.2</version>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>user-service-api-endpoint</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion porter_lightweight/api/user-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.apache.servicecomb.samples.porter</groupId>
<artifactId>porter-api</artifactId>
<version>2.8.2</version>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>user-service-api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion porter_lightweight/api/user-service/service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.servicecomb.samples.porter</groupId>
<artifactId>user-service-api</artifactId>
<version>2.8.2</version>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>user-service-api-service</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion porter_lightweight/file-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.apache.servicecomb.samples.porter</groupId>
<artifactId>porter-application</artifactId>
<version>2.8.2</version>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>porter-file-service</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@

package org.apache.servicecomb.samples.porter.file;

import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class FileMain {
public static void main(String[] args) throws Exception {
BeanUtils.init();
public static void main(String[] args) throws Exception {
try {
new SpringApplicationBuilder()
.web(WebApplicationType.NONE)
.sources(FileMain.class)
.run(args);
} catch (Exception e) {
e.printStackTrace();
}
}
}

This file was deleted.

Loading

0 comments on commit cec906e

Please sign in to comment.