Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2
22a7f0099
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Summer2022
22a7f0099
Commits
c1947c32
Commit
c1947c32
authored
Jun 21, 2019
by
高辛格
Browse files
Options
Downloads
Patches
Plain Diff
consul init
parent
cf94b2f9
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
common/url.go
+4
-7
4 additions, 7 deletions
common/url.go
registry/consul/listener.go
+48
-0
48 additions, 0 deletions
registry/consul/listener.go
registry/consul/registry.go
+96
-0
96 additions, 0 deletions
registry/consul/registry.go
registry/consul/utils.go
+57
-0
57 additions, 0 deletions
registry/consul/utils.go
with
205 additions
and
7 deletions
common/url.go
+
4
−
7
View file @
c1947c32
...
@@ -201,16 +201,13 @@ func NewURL(ctx context.Context, urlString string, opts ...option) (URL, error)
...
@@ -201,16 +201,13 @@ func NewURL(ctx context.Context, urlString string, opts ...option) (URL, error)
for
_
,
opt
:=
range
opts
{
for
_
,
opt
:=
range
opts
{
opt
(
&
s
)
opt
(
&
s
)
}
}
//fmt.Println(s.String())
return
s
,
nil
return
s
,
nil
}
}
//
func
NewURLFromString
(
url
string
)
URL
{
//func (c URL) Key() string {
// return fmt.Sprintf(
}
// "%s://%s:%s@%s:%s/%s",
// c.Protocol, c.Username, c.Password, c.Ip, c.Port, c.Path)
//}
func
(
c
URL
)
URLEqual
(
url
URL
)
bool
{
func
(
c
URL
)
URLEqual
(
url
URL
)
bool
{
c
.
Ip
=
""
c
.
Ip
=
""
...
...
This diff is collapsed.
Click to expand it.
registry/consul/listener.go
0 → 100644
+
48
−
0
View file @
c1947c32
package
consul
import
(
consul
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/watch"
)
import
(
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/registry"
)
type
consulListener
struct
{
plan
*
watch
.
Plan
addrsChan
chan
[]
*
consul
.
ServiceEntry
}
func
newConsulListener
(
url
common
.
URL
)
(
registry
.
Listener
,
error
)
{
var
err
error
addrsChan
:=
make
(
chan
[]
*
consul
.
ServiceEntry
,
1
)
params
:=
make
(
map
[
string
]
interface
{})
params
[
"type"
]
=
"service"
params
[
"service"
]
=
url
.
Service
()
plan
,
err
:=
watch
.
Parse
(
params
)
if
err
!=
nil
{
return
nil
,
err
}
plan
.
Handler
=
func
(
idx
uint64
,
raw
interface
{})
{
addrs
,
_
:=
raw
.
([]
*
consul
.
ServiceEntry
)
addrsChan
<-
addrs
}
listener
:=
&
consulListener
{
plan
:
plan
,
addrsChan
:
addrsChan
,
}
return
listener
,
nil
}
func
(
l
*
consulListener
)
Next
()
(
*
registry
.
ServiceEvent
,
error
)
{
return
nil
,
nil
}
func
(
l
*
consulListener
)
Close
()
{
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
registry/consul/registry.go
0 → 100644
+
96
−
0
View file @
c1947c32
package
consul
import
(
"strconv"
)
import
(
perrors
"github.com/pkg/errors"
consul
"github.com/hashicorp/consul/api"
)
import
(
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/registry"
"github.com/apache/dubbo-go/common/constant"
)
type
consulRegistry
struct
{
*
common
.
URL
client
*
consul
.
Client
}
func
newConsulRegistry
(
url
*
common
.
URL
)
(
registry
.
Registry
,
error
)
{
var
err
error
config
:=
&
consul
.
Config
{
Address
:
url
.
Location
}
client
,
err
:=
consul
.
NewClient
(
config
)
if
err
!=
nil
{
return
nil
,
err
}
r
:=
&
consulRegistry
{
URL
:
url
,
client
:
client
,
}
return
r
,
nil
}
func
(
r
*
consulRegistry
)
Register
(
url
common
.
URL
)
error
{
var
err
error
role
,
_
:=
strconv
.
Atoi
(
r
.
URL
.
GetParam
(
constant
.
ROLE_KEY
,
""
))
if
role
==
common
.
PROVIDER
{
err
=
r
.
register
(
url
)
if
err
!=
nil
{
return
perrors
.
WithStack
(
err
)
}
}
return
nil
}
func
(
r
*
consulRegistry
)
register
(
url
common
.
URL
)
error
{
var
err
error
service
,
err
:=
buildService
(
url
)
if
err
!=
nil
{
return
err
}
return
r
.
client
.
Agent
()
.
ServiceRegister
(
service
)
}
func
(
r
*
consulRegistry
)
Subscribe
(
url
common
.
URL
)
(
registry
.
Listener
,
error
)
{
var
(
listener
registry
.
Listener
err
error
)
role
,
_
:=
strconv
.
Atoi
(
r
.
URL
.
GetParam
(
constant
.
ROLE_KEY
,
""
))
if
role
==
common
.
CONSUMER
{
listener
,
err
=
r
.
subscribe
(
url
)
if
err
!=
nil
{
return
nil
,
err
}
}
return
listener
,
nil
}
func
(
r
*
consulRegistry
)
subscribe
(
url
common
.
URL
)
(
registry
.
Listener
,
error
)
{
var
err
error
listener
,
err
:=
newConsulListener
(
url
)
return
listener
,
err
}
func
(
r
*
consulRegistry
)
GetUrl
()
common
.
URL
{
return
*
r
.
URL
}
func
(
r
*
consulRegistry
)
IsAvailable
()
bool
{
}
func
(
r
*
consulRegistry
)
Destroy
()
{
}
This diff is collapsed.
Click to expand it.
registry/consul/utils.go
0 → 100644
+
57
−
0
View file @
c1947c32
package
consul
import
(
"strconv"
"crypto/md5"
)
import
(
consul
"github.com/hashicorp/consul/api"
)
import
(
"github.com/apache/dubbo-go/common"
)
func
buildId
(
url
common
.
URL
)
string
{
return
string
(
md5
.
Sum
([]
byte
(
url
.
String
()))[
:
])
}
func
buildService
(
url
common
.
URL
)
(
*
consul
.
AgentServiceRegistration
,
error
)
{
var
err
error
// id
id
:=
buildId
(
url
)
// port
port
,
err
:=
strconv
.
Atoi
(
url
.
Port
)
if
err
!=
nil
{
return
nil
,
err
}
// tags
tags
:=
make
([]
string
,
0
)
for
k
:=
range
url
.
Params
{
tags
=
append
(
tags
,
k
+
"="
+
url
.
Params
.
Get
(
k
))
}
// meta
meta
:=
make
(
map
[
string
]
string
)
meta
[
"url"
]
=
url
.
String
()
service
:=
&
consul
.
AgentServiceRegistration
{
Name
:
url
.
Service
(),
ID
:
id
,
Address
:
url
.
Ip
,
Port
:
port
,
Tags
:
tags
,
Meta
:
meta
,
}
return
service
,
nil
}
func
retrieveURL
(
service
*
consul
.
ServiceEntry
)
common
.
URL
{
url
:=
service
.
Service
.
Meta
[
"url"
]
return
common
.
NewURLFromString
(
url
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment