Skip to content

Commit

Permalink
Merge pull request #121 from dubbo-x/consul
Browse files Browse the repository at this point in the history
Ftr: consul registry
  • Loading branch information
hxmhlt authored Aug 26, 2019
2 parents 23b95a0 + 5dd2bd0 commit 9a7642b
Show file tree
Hide file tree
Showing 37 changed files with 1,792 additions and 44 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ coverage.txt
target/
classes


# Gopkg.lock
# go mod, go test
vendor/
coverage.txt

logs/
.vscode/
Expand Down
4 changes: 4 additions & 0 deletions common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,19 @@ func WithParams(params url.Values) option {
url.Params = params
}
}

func WithParamsValue(key, val string) option {
return func(url *URL) {
url.Params.Set(key, val)
}
}

func WithProtocol(proto string) option {
return func(url *URL) {
url.Protocol = proto
}
}

func WithIp(ip string) option {
return func(url *URL) {
url.Ip = ip
Expand All @@ -141,6 +144,7 @@ func WithLocation(location string) option {
url.Location = location
}
}

func NewURLWithOptions(opts ...option) *URL {
url := &URL{}
for _, opt := range opts {
Expand Down
11 changes: 5 additions & 6 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ var (
maxWait = 3
)

// loaded comsumer & provider config from xxx.yml, and log config from xxx.xml
// Namely: dubbo.comsumer.xml & dubbo.provider.xml in java dubbo
// loaded consumer & provider config from xxx.yml, and log config from xxx.xml
// Namely: dubbo.consumer.xml & dubbo.provider.xml in java dubbo
func init() {
var (
confConFile, confProFile string
Expand Down Expand Up @@ -70,9 +70,8 @@ func Load() {
SetConsumerService(genericService)
}
rpcService := GetConsumerService(key)

if rpcService == nil {
logger.Warnf("%s is not exsist!", key)
logger.Warnf("%s does not exist!", key)
continue
}
ref.id = key
Expand All @@ -99,7 +98,7 @@ func Load() {
break
}
if refconfig.invoker == nil {
logger.Warnf("The interface %s invoker not exsist , may you should check your interface config.", refconfig.InterfaceName)
logger.Warnf("The interface %s invoker not exist , may you should check your interface config.", refconfig.InterfaceName)
}
}
}
Expand All @@ -120,7 +119,7 @@ func Load() {
for key, svs := range providerConfig.Services {
rpcService := GetProviderService(key)
if rpcService == nil {
logger.Warnf("%s is not exsist!", key)
logger.Warnf("%s does not exist!", key)
continue
}
svs.id = key
Expand Down
44 changes: 44 additions & 0 deletions examples/consul/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# consul

Examples for consul registry. Before running examples below, make sure that consul has been started.

## requirement

- consul
- go 1.12
- java 8
- maven 3.6.1

## go-server

```
$ cd examples/consul/go-server
$ export CONF_PROVIDER_FILE_PATH="config/server.yml"
$ export APP_LOG_CONF_FILE="config/log.yml"
$ go run .
```

## go-client

```
$ cd examples/consul/go-client
$ export CONF_CONSUMER_FILE_PATH="config/client.yml"
$ export APP_LOG_CONF_FILE="config/log.yml"
$ go run .
```

## java-server

```
$ cd examples/consul/java-server
$ mvn clean package
$ java -jar target/java-server-1.0.0.jar
```

## java-client

```
$ cd examples/consul/java-client
$ mvn clean package
$ java -jar target/java-client-1.0.0.jar
```
43 changes: 43 additions & 0 deletions examples/consul/go-client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 main

import (
"fmt"
)

import (
_ "github.com/apache/dubbo-go/cluster/cluster_impl"
_ "github.com/apache/dubbo-go/cluster/loadbalance"
_ "github.com/apache/dubbo-go/common/proxy/proxy_factory"
"github.com/apache/dubbo-go/config"
_ "github.com/apache/dubbo-go/filter/impl"
_ "github.com/apache/dubbo-go/protocol/dubbo"
_ "github.com/apache/dubbo-go/registry/consul"
_ "github.com/apache/dubbo-go/registry/protocol"
)

func main() {
config.Load()

message, err := dubboService.SayHello([]interface{}{"world"})
if err != nil {
panic(err)
}
fmt.Println(message)
}
52 changes: 52 additions & 0 deletions examples/consul/go-client/config/client.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
application_config:
organization : "ikurento.com"
name : "BDTService"
module : "dubbogo user-info client"
version : "0.0.1"
owner : "ZX"
environment : "test"

registries :
"consul":
protocol: "consul"
timeout : "3s"
address: "127.0.0.1:8500"
username: ""
password: ""

references:
"DubboService":
protocol : "dubbo"
interface : "dubbo.DubboService"
cluster: "failover"
methods :
- name: "SayHello"
retries: 3

protocol_conf:
dubbo:
reconnect_interval: 0
connection_number: 2
heartbeat_period: "5s"
session_timeout: "20s"
fail_fast_timeout: "5s"
pool_size: 64
pool_ttl: 600
getty_session_param:
compress_encoding: false
tcp_no_delay: true
tcp_keep_alive: true
keep_alive_period: "120s"
tcp_r_buf_size: 262144
tcp_w_buf_size: 65536
pkg_rq_size: 1024
pkg_wq_size: 512
tcp_read_timeout: "1s"
tcp_write_timeout: "5s"
wait_timeout: "1s"
max_msg_len: 10240
session_name: "client"

check: true
request_timeout : "3s"
connect_timeout : "3s"
28 changes: 28 additions & 0 deletions examples/consul/go-client/config/log.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

level: "info"
development: false
disableCaller: false
disableStacktrace: true
sampling:
encoding: "console"

# encoder
encoderConfig:
messageKey: "message"
levelKey: "level"
timeKey: "time"
nameKey: "logger"
callerKey: "caller"
stacktraceKey: "stacktrace"
lineEnding: ""
levelEncoder: "capitalColor"
timeEncoder: "iso8601"
durationEncoder: "seconds"
callerEncoder: "short"
nameEncoder: ""

outputPaths:
- "stderr"
errorOutputPaths:
- "stderr"
initialFields:
36 changes: 36 additions & 0 deletions examples/consul/go-client/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 main

import (
"github.com/apache/dubbo-go/config"
)

var dubboService = new(DubboService)

func init() {
config.SetConsumerService(dubboService)
}

type DubboService struct {
SayHello func(req interface{}) (string, error)
}

func (s *DubboService) Reference() string {
return "DubboService"
}
28 changes: 28 additions & 0 deletions examples/consul/go-server/config/log.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

level: "info"
development: false
disableCaller: false
disableStacktrace: true
sampling:
encoding: "console"

# encoder
encoderConfig:
messageKey: "message"
levelKey: "level"
timeKey: "time"
nameKey: "logger"
callerKey: "caller"
stacktraceKey: "stacktrace"
lineEnding: ""
levelEncoder: "capitalColor"
timeEncoder: "iso8601"
durationEncoder: "seconds"
callerEncoder: "short"
nameEncoder: ""

outputPaths:
- "stderr"
errorOutputPaths:
- "stderr"
initialFields:
52 changes: 52 additions & 0 deletions examples/consul/go-server/config/server.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
application_config:
organization : "ikurento.com"
name : "BDTService"
module : "dubbogo user-info server"
version : "0.0.1"
owner : "ZX"
environment : "test"

registries :
"consul":
protocol: "consul"
timeout : "3s"
address: "127.0.0.1:8500"
username: ""
password: ""

services:
"DubboService":
protocol : "dubbo"
interface : "dubbo.DubboService"
loadbalance: "random"
warmup: "100"
cluster: "failover"
methods:
- name: "SayHello"
retries: 1
loadbalance: "random"

protocols:
"dubbo1":
name: "dubbo"
port: 20000

protocol_conf:
dubbo:
session_number: 700
fail_fast_timeout: "5s"
session_timeout: "20s"
getty_session_param:
compress_encoding: false
tcp_no_delay: true
tcp_keep_alive: true
keep_alive_period: "120s"
tcp_r_buf_size: 262144
tcp_w_buf_size: 65536
pkg_rq_size: 1024
pkg_wq_size: 512
tcp_read_timeout: "1s"
tcp_write_timeout: "5s"
wait_timeout: "1s"
max_msg_len: 1024
session_name: "server"
Loading

0 comments on commit 9a7642b

Please sign in to comment.