Skip to content

Commit

Permalink
optimize quick start (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
tianxiaoliang committed Jul 23, 2018
1 parent 25a6b2c commit eadbf95
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 76 deletions.
50 changes: 3 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ Go-chassis is based on [Go-Micro](https://github.com/micro/go-micro) A pluggable
You can check [plugins](https://github.com/go-chassis/go-chassis-plugins) to see more features

# Quick Start
You can see documentations in [here](http://go-chassis.readthedocs.io/en/latest/)
You can see more documentations in [here](http://go-chassis.readthedocs.io/en/latest/)

## Installation
1. Install [go 1.8+](https://golang.org/doc/install)

2. Clone the project
Expand All @@ -36,7 +35,7 @@ You can see documentations in [here](http://go-chassis.readthedocs.io/en/latest/
git clone git@github.com:ServiceComb/go-chassis.git
```

3. Use [glide](https://github.com/Masterminds/glide) to download deps
3. Use [glide](https://github.com/Masterminds/glide) to download dependencies

```sh
cd go-chassis
Expand All @@ -45,52 +44,9 @@ glide intall

4. Install ServiceComb [service-center](https://github.com/ServiceComb/service-center/releases)

## Write a http service provider

<b>Step 1:</b>
Define your Schema and your business logic.

```go
//API
func (s *HelloServer) SayHello(b *restful.Context) {
b.Write([]byte("Hello : Welcome to Go-Chassis."))
}
//Specify URL pattern
func (s *HelloServer) URLPatterns() []restful.Route {
return []restful.RouteSpec{
{http.MethodGet, "/sayhello", "SayHello"},
}
}
```

<b>Step 2:</b>
Register your Schema to go-chassis
```go
chassis.RegisterSchema("rest", &HelloServer{},server.WithSchemaID("HelloServer"))
```

<b>Step 3:</b>
Start the Chassis as a Server
```go
chassis.Init()
chassis.Run()
```

## Write a Http Consumer
5. [Write your first http micro service](http://go-chassis.readthedocs.io/en/latest/getstarted/writing-rest.html)

<b>Step 1:</b>
Initialize your Chassis
```go
chassis.Init()

```
<b>Step 2:</b>
Use Rest Invoker to call the provider
```go
restInvoker := core.NewRestInvoker()
req, _ := rest.NewRequest("GET", "cse://"+providerName+"/sayhello")
resp1, err := restInvoker.ContextDo(context.TODO(), req)
```

# Examples
You can check examples [here](examples)
Expand Down
10 changes: 5 additions & 5 deletions docs/getstarted/install.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
最小化安装
Minimize Installation
=====
1. Install [go 1.8+](https://golang.org/doc/install)

Expand All @@ -8,7 +8,7 @@
git clone git@github.com:ServiceComb/go-chassis.git
```

3. Use [glide](https://github.com/Masterminds/glide) to download deps
3. Use [glide](https://github.com/Masterminds/glide) to download dependencies

```sh
cd go-chassis
Expand All @@ -18,8 +18,8 @@ glide intall
4. Install ServiceComb [service-center](https://github.com/ServiceComb/service-center/releases)


使用RPC通信
Use RPC communication
===================
安装protobuff 3.2.0 https://github.com/google/protobuf
Install protobuff 3.2.0 https://github.com/google/protobuf

安装golang插件 https://github.com/golang/protobuf
Install https://github.com/golang/protobuf
54 changes: 30 additions & 24 deletions docs/getstarted/writing-rest.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Writing Rest service
==========================
服务端
1个工程或者go package,推荐结构如下
Checkout full example in [here](https://github.com/ServiceComb/go-chassis/tree/master/examples/rest)
### Provider
this section show you how to write a http server

Create 1 project or go package as recommended

server/

Expand All @@ -13,48 +16,49 @@ server/

└── main.go

1.编写接口
1.Write a struct to hold http logic and url patterns
```go
type RestFulHello struct {}

func (r *RestFulHello) Sayhello(b *restful.Context) {
b.Write([]byte("get user id: " + b.ReadPathParameter("userid")))
}
```
2.注册路由
2.Write your url patterns
```go
func (s *RestFulHello) URLPatterns() []restful.Route {
return []restful.RouteSpec{
{http.MethodGet, "/sayhello/{userid}", "Sayhello"},
}
}
```
3.注册接口

第一个参数表示你要向哪个协议注册,第三个为schema ID,为可选,会在调用中使用
3.Register this struct

chassis.RegisterSchema("rest", &RestFulHello{}, server.WithSchemaId("RestHelloService"))
说明:
the first params means which server you want to register your struct to.
```go
chassis.RegisterSchema("rest", &RestFulHello{})
```

想注册的rest协议的接口,必须实现URLPatterns方法定义路由
路由中暴露为API的方法都要入参均为*restful.Context
**Notice**
>>Must implement URLPatterns, and for other functions must use \*restful.Context as the only input,
and certainly the method name must start with uppercase

4.修改配置文件chassis.yaml
4.Modify chassis.yaml
```yaml
cse:
service:
registry:
address: http://127.0.0.1:30100
protocols:
rest:
address: http://127.0.0.1:30100
protocols: # what kind of server you want to launch
rest: #launch a http server
listenAddress: 127.0.0.1:5001
```
5.修改microservice.yaml, 为服务起名
5.Modify microservice.yaml
```yaml
service_description:
name: RESTServer
name: RESTServer # name your provider
```
6.main.go中启动服务
6.In main.go init and start the chassis
```go
func main() {
//start all server you register in server/schemas.
Expand All @@ -65,8 +69,10 @@ func main() {
chassis.Run()
}
```
客户端
1个工程或者go package,推荐结构如下
### Consumer
this section show you how to write a http client

Create 1 project or go package as recommended

client/

Expand All @@ -78,19 +84,19 @@ client/

└── main.go

1.修改配置文件chassis.yaml
1. modify chassis.yaml
```yaml
cse:
service:
registry:
address: http://127.0.0.1:30100
```
2.修改microservice.yaml
2. modify microservice.yaml
```yaml
service_description:
name: RESTClient
name: RESTClient #name your consumer
```
3.main中调用服务端,请求包括服务名,schema,operation及参数
3.in main.go call your service
```go
func main() {
//Init framework
Expand Down

0 comments on commit eadbf95

Please sign in to comment.