Skip to content
Snippets Groups Projects
Commit 60ff7dfd authored by 高辛格's avatar 高辛格
Browse files

add consul example

parent bf3cb2db
No related branches found
No related tags found
No related merge requests found
Showing
with 516 additions and 0 deletions
# consul
Examples for consul registry. Before running examples below, make sure that consul has been start.
## 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 example/consul/java-server
$ gradle build
$ java -jar build/libs/java-server-1.0-SNAPSHOT.jar
```
## java-client
```
$ cd examples/consul/java-client
$ gradle build
$ java -jar build/libs/java-client-1.0-SNAPSHOT.jar
```
\ No newline at end of file
/*
* 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/common/proxy/proxy_factory"
_ "github.com/apache/dubbo-go/protocol/dubbo"
_ "github.com/apache/dubbo-go/registry/protocol"
_ "github.com/apache/dubbo-go/filter/impl"
_ "github.com/apache/dubbo-go/cluster/cluster_impl"
_ "github.com/apache/dubbo-go/cluster/loadbalance"
_ "github.com/apache/dubbo-go/registry/consul"
"github.com/apache/dubbo-go/config"
)
func main() {
config.Load()
message, err := dubboService.SayHello([]interface{}{"world"})
if err != nil {
panic(err)
}
fmt.Println(message)
}
# dubbo client yaml configure file
check: true
# client
request_timeout : "3s"
# connect timeout
connect_timeout : "3s"
# application config
application_config:
organization : "ikurento.com"
name : "BDTService"
module : "dubbogo user-info client"
version : "0.0.1"
owner : "ZX"
environment : "test"
registries :
"hangzhouzk":
protocol: "consul"
timeout : "3s"
address: "127.0.0.1:8500"
username: ""
password: ""
references:
"UserProvider":
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"
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:
/*
* 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) Service() string {
return "dubbo.DubboService"
}
func (s *DubboService) Version() string {
return ""
}
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:
# dubbo server yaml configure file
# application config
application_config:
organization : "ikurento.com"
name : "BDTService"
module : "dubbogo user-info server"
version : "0.0.1"
owner : "ZX"
environment : "test"
registries :
"hangzhouzk":
protocol: "consul"
timeout : "3s"
address: "127.0.0.1:8500"
username: ""
password: ""
services:
"UserProvider":
protocol : "dubbo"
interface : "dubbo.DubboService"
loadbalance: "random"
warmup: "100"
cluster: "failover"
methods:
- name: "SayHello"
retries: 1
loadbalance: "random"
protocols:
"dubbo1":
name: "dubbo"
# ip : "127.0.0.1"
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"
/*
* 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 (
"os"
"os/signal"
"syscall"
"time"
"fmt"
)
import (
_ "github.com/apache/dubbo-go/protocol/dubbo"
_ "github.com/apache/dubbo-go/registry/protocol"
_ "github.com/apache/dubbo-go/common/proxy/proxy_factory"
_ "github.com/apache/dubbo-go/filter/impl"
_ "github.com/apache/dubbo-go/cluster/cluster_impl"
_ "github.com/apache/dubbo-go/cluster/loadbalance"
_ "github.com/apache/dubbo-go/registry/consul"
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/common/logger"
)
var (
survivalTimeout = int(3e9)
)
func main() {
config.Load()
initSignal()
}
func initSignal() {
signals := make(chan os.Signal, 1)
// It is not possible to block SIGKILL or syscall.SIGSTOP
signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
sig := <-signals
logger.Infof("get signal %s", sig.String())
switch sig {
case syscall.SIGHUP:
// reload()
default:
go time.AfterFunc(time.Duration(float64(survivalTimeout)*float64(time.Second)), func() {
logger.Warnf("app exit now by force...")
os.Exit(1)
})
// 要么fastFailTimeout时间内执行完毕下面的逻辑然后程序退出,要么执行上面的超时函数程序强行退出
fmt.Println("provider app exit now...")
return
}
}
}
/*
* 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"
)
func init() {
config.SetProviderService(new(DubboService))
}
type DubboService struct {}
func (s *DubboService) SayHello(message string) (string, error) {
return "hello " + message, nil
}
func (s *DubboService) Service() string {
return "dubbo.DubboService"
}
func (s *DubboService) Version() string {
return ""
}
.idea
.gradle
*.iml
build/
out/
\ No newline at end of file
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
maven {url 'http://maven.aliyun.com/nexus/cotent/groups/public/'}
mavenCentral()
}
dependencies {
compile 'log4j:log4j:1.2.17'
compile 'org.apache.dubbo:dubbo:2.7.1'
compile 'com.ecwid.consul:consul-api:1.4.2'
compile 'commons-logging:commons-logging:1.2'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
jar {
from {configurations.compile.collect {it.isDirectory() ? it : zipTree(it)}}
manifest {
attributes 'Main-Class': 'dubbo.client.Main'
}
}
rootProject.name = 'java-client'
package dubbo;
public interface DubboService {
String SayHello(String message);
}
package dubbo.client;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import dubbo.DubboService;
public class Main {
public static void main(String[] args) {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("java-client");
applicationConfig.setQosPort(33333);
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("consul://127.0.0.1:8500");
ReferenceConfig<DubboService> referenceConfig = new ReferenceConfig<>();
referenceConfig.setApplication(applicationConfig);
referenceConfig.setRegistry(registryConfig);
referenceConfig.setInterface(DubboService.class);
DubboService dubboService = referenceConfig.get();
System.out.println(dubboService.SayHello("world"));
}
}
log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.Stdout.layout.conversionPattern=[%p] %c | %m\n
log4j.rootLogger=INFO,Stdout
\ No newline at end of file
.idea
.gradle
*.iml
build/
out/
\ No newline at end of file
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
maven {url 'http://maven.aliyun.com/nexus/cotent/groups/public/'}
mavenCentral()
}
dependencies {
compile 'log4j:log4j:1.2.17'
compile 'org.apache.dubbo:dubbo:2.7.1'
compile 'com.ecwid.consul:consul-api:1.4.2'
compile 'commons-logging:commons-logging:1.2'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
jar {
from {configurations.compile.collect {it.isDirectory() ? it : zipTree(it)}}
manifest {
attributes 'Main-Class': 'dubbo.server.Main'
}
}
\ No newline at end of file
rootProject.name = 'java-server'
package dubbo;
public interface DubboService {
String SayHello(String message);
}
package dubbo.server.Impl;
import dubbo.DubboService;
public class DubboServiceImpl implements DubboService {
@Override
public String SayHello(String message) {
return "hello " + message;
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment