Skip to content

Commit

Permalink
feat(p2) Supplement DM's description of JavaClassName and the use of …
Browse files Browse the repository at this point in the history
…enumerations (kitex-contrib#88)
  • Loading branch information
s5364733 committed Apr 25, 2024
1 parent ef52685 commit c2c538e
Show file tree
Hide file tree
Showing 14 changed files with 479 additions and 19 deletions.
55 changes: 51 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ service EchoService {

#### 其它类型(java.lang.Object, java.util.Date)

由于 **thrift** 类型的局限性,**kitex****dubbo-java** 映射时有一些不兼容的类型。
由于 **thrift** 类型的局限性,**kitex****dubbo-java** 映射时有一些不兼容的类型。
DubboCodec 在 [codec-dubbo/java](https://github.com/kitex-contrib/codec-dubbo/tree/main/java) 包中提供了更多 **thrift** 不支持的 **java** 类型。

为了启用这些类型,你可以在 **Thrift IDL** 中使用 `include "java.thrift"` 导入它们,并且在使用 **kitex** 脚手架工具生成代码时添加 `-hessian2 java_extension` 参数来拉取该拓展包。
Expand Down Expand Up @@ -322,6 +322,53 @@ service EchoService {
string EchoMethodD(1: bool req1, 2: i32 req2) (JavaMethodName="EchoMethod")
}
```
### 枚举支持

支持Java的枚举类型,需要用户在枚举上加上注解映射到具体的Java类型,您可以在客户端做基本的枚举配置已经对应服务端代码,如下

#### thrift 配置
```thrift
enum KitexEnum {
ONE,
TWO,
THREE,
FOUR,
FIVE,
}(JavaClassName="org.cloudwego.kitex.samples.enumeration.KitexEnum")
service GreetEnumService {
KitexEnum GreetEnum(1: KitexEnum req)
}
```
#### Dubbo服务侧代码
```java

package org.cloudwego.kitex.samples.enumeration;

import java.io.Serializable;

public enum KitexEnum implements Serializable {

ONE("1"),TWO("2"),THREE("3"),FOUR("4"),FIVE("5");

final String codeStr ;

KitexEnum(String number) {
this.codeStr = number;
}

// 枚举类型的 getter 方法
public String getCode() {
return this.codeStr;
}
}

```
**重要提示**
1. 这里强制您配置JavaClassName来映射具体的Java类型,如果您没有配置可能会导致不可预知的错误


### 异常处理

Expand Down Expand Up @@ -402,7 +449,7 @@ exception CustomizedException {
}(JavaClassName="org.cloudwego.kitex.samples.api.CustomizedException")
```

[其它类型](#其它类型javalangobject-javautildate)一样,需要在使用 **kitex** 脚手架工具生成代码时添加 `-hessian2 java_extension` 参数来拉取拓展包。
[其它类型](#其它类型javalangobject-javautildate)一样,需要在使用 **kitex** 脚手架工具生成代码时添加 `-hessian2 java_extension` 参数来拉取拓展包。

使用方法与[常见异常](#常见异常)一致。

Expand All @@ -412,7 +459,7 @@ exception CustomizedException {
用于该功能的配置分为以下两个层次:
1. [registry/options.go](https://github.com/kitex-contrib/codec-dubbo/tree/main/registries/zookeeper/registry/options.go)[resolver/options.go](https://github.com/kitex-contrib/codec-dubbo/tree/main/registries/zookeeper/resolver/options.go) 中的WithXXX函数提供注册中心级别的配置,请使用这些函数生成```registry.Registry```
```discovery.Resolver```实例。
```discovery.Resolver```实例。
2. 服务级别的配置由```client.WithTag``````server.WithRegistryInfo```进行传递,/registries/common.go提供Tag Keys:

| Tag Key | client侧作用 | server侧作用 |
Expand Down Expand Up @@ -545,7 +592,7 @@ func main() {

## 性能测试

### 测试环境
### 测试环境

CPU: **Intel(R) Xeon(R) Gold 5118 CPU @ 2.30GHz**
内存: **192GB**
Expand Down
46 changes: 46 additions & 0 deletions README_ENG.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,52 @@ service EchoService {
string EchoMethodD(1: bool req1, 2: i32 req2) (JavaMethodName="EchoMethod")
}
```
### Enumeration support
To support Java enumeration types, users need to add annotations on the enumeration to map it to specific Java types. You can make basic enumeration configurations on the client and correspond to the server code, as follows
#### Thrift configuration
```thrift
enum KitexEnum {
ONE,
TWO,
THREE,
FOUR,
FIVE,
}(JavaClassName="org.cloudwego.kitex.samples.enumeration.KitexEnum")
service GreetEnumService {
KitexEnum GreetEnum(1: KitexEnum req)
}
```
#### Dubbo service side code
```java
package org.cloudwego.kitex.samples.enumeration;
import java.io.Serializable;
public enum KitexEnum implements Serializable {
ONE("1"),TWO("2"),THREE("3"),FOUR("4"),FIVE("5");
final String codeStr ;
KitexEnum(String number) {
this.codeStr = number;
}
// Getter methods of enumeration types
public String getCode() {
return this.codeStr;
}
}
```
**Important notes:**
1. This forces you to configure JavaClassName to map specific Java types. If you do not configure it, it may cause unpredictable errors.
### Exception Handling
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ require (
github.com/apache/dubbo-go-hessian2 v1.12.4
github.com/cloudwego/kitex v0.8.0
github.com/cloudwego/thriftgo v0.3.3
github.com/kitex-contrib/codec-dubbo v0.2.5
github.com/stretchr/testify v1.8.2
golang.org/x/net v0.17.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kitex-contrib/codec-dubbo v0.2.5 h1:hM6MzbWyVFiNjXzB42sN+BtUXrbPr/N0UBocLxU+rKQ=
github.com/kitex-contrib/codec-dubbo v0.2.5/go.mod h1:0pp4flNA+oPBDxAcOCxH7D+McLAjFrLzGXVABdw22xE=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.1.0/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 CloudWeGo Authors
*
* 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.cloudwego.kitex.samples.enumeration;

import java.io.Serializable;

public enum KitexEnum implements Serializable {

ONE("1"),TWO("2"),THREE("3"),FOUR("4"),FIVE("5");

final String codeStr ;

KitexEnum(String number) {
this.codeStr = number;
}

// 枚举类型的 getter 方法
public String getCode() {
return this.codeStr;
}
}
12 changes: 12 additions & 0 deletions samples/helloworld/kitex/api.thrift
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
namespace go hello

enum KitexEnum {
ONE,
TWO,
THREE,
FOUR,
FIVE,
}(JavaClassName="org.cloudwego.kitex.samples.enumeration.KitexEnum")

struct GreetRequest {
1: required string req,
}(JavaClassName="org.cloudwego.kitex.samples.api.GreetRequest")
Expand All @@ -11,4 +19,8 @@ struct GreetResponse {
service GreetService {
string Greet(1: string req)
GreetResponse GreetWithStruct(1: GreetRequest req)
}

service GreetEnumService {
KitexEnum GreetEnum(1: KitexEnum req)
}
2 changes: 1 addition & 1 deletion samples/helloworld/kitex/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ replace github.com/kitex-contrib/codec-dubbo => ../../../
require (
github.com/apache/thrift v0.16.0
github.com/cloudwego/kitex v0.9.0
github.com/kitex-contrib/codec-dubbo v0.0.0-20231009160704-aad6a2705290
github.com/kitex-contrib/codec-dubbo v0.2.5
github.com/kitex-contrib/obs-opentelemetry v0.2.6
github.com/pkg/errors v0.9.1
)
Expand Down
106 changes: 105 additions & 1 deletion samples/helloworld/kitex/kitex_gen/hello/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c2c538e

Please sign in to comment.