add proxy interface and move router packages

This commit is contained in:
Asim Aslam 2019-06-06 17:55:32 +01:00
parent 64459c54a1
commit a58bc8e75c
8 changed files with 27 additions and 16 deletions

View File

@ -10,14 +10,15 @@ import (
"github.com/micro/go-micro/client" "github.com/micro/go-micro/client"
"github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec"
"github.com/micro/go-micro/codec/bytes" "github.com/micro/go-micro/codec/bytes"
"github.com/micro/go-micro/init"
"github.com/micro/go-micro/server" "github.com/micro/go-micro/server"
"github.com/micro/go-micro/service/grpc" "github.com/micro/go-micro/service/grpc"
) )
// Router will transparently proxy requests to the backend. // Proxy will transparently proxy requests to the backend.
// If no backend is specified it will call a service using the client. // If no backend is specified it will call a service using the client.
// If the service matches the Name it will use the server.DefaultRouter. // If the service matches the Name it will use the server.DefaultProxy.
type Router struct { type Proxy struct {
// Name of the local service. In the event it's to be left alone // Name of the local service. In the event it's to be left alone
Name string Name string
@ -33,13 +34,16 @@ type Router struct {
// The client to use for outbound requests // The client to use for outbound requests
Client client.Client Client client.Client
// The proxy options
Options init.Options
} }
var ( var (
// The default name of this local service // The default name of this local service
DefaultName = "go.micro.proxy" DefaultName = "go.micro.proxy"
// The default router // The default router
DefaultRouter = &Router{} DefaultProxy = &Proxy{}
) )
// read client request and write to server // read client request and write to server
@ -75,8 +79,8 @@ func readLoop(r server.Request, s client.Stream) error {
} }
} }
// ServeRequest honours the server.Router interface // ServeRequest honours the server.Proxy interface
func (p *Router) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error { func (p *Proxy) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error {
// set the default name e.g local proxy // set the default name e.g local proxy
if p.Name == "" { if p.Name == "" {
p.Name = DefaultName p.Name = DefaultName
@ -90,7 +94,7 @@ func (p *Router) ServeRequest(ctx context.Context, req server.Request, rsp serve
// check service route // check service route
if req.Service() == p.Name { if req.Service() == p.Name {
// use the default router // use the default router
return server.DefaultRouter.ServeRequest(ctx, req, rsp) return server.DefaultProxy.ServeRequest(ctx, req, rsp)
} }
opts := []client.CallOption{} opts := []client.CallOption{}
@ -165,7 +169,14 @@ func (p *Router) ServeRequest(ctx context.Context, req server.Request, rsp serve
return nil return nil
} }
// NewSingleHostRouter returns a router which sends requests to a single backend // NewProxy returns a new grpc proxy server
func NewProxy(opts ...init.Option) *Proxy {
return &Proxy{
Options: init.NewOptions(opts...),
}
}
// NewSingleHostProxy returns a router which sends requests to a single backend
// //
// It is used by setting it in a new micro service to act as a proxy for a backend. // It is used by setting it in a new micro service to act as a proxy for a backend.
// //
@ -173,19 +184,19 @@ func (p *Router) ServeRequest(ctx context.Context, req server.Request, rsp serve
// //
// Create a new router to the http backend // Create a new router to the http backend
// //
// r := NewSingleHostRouter("localhost:10001") // r := NewSingleHostProxy("localhost:10001")
// //
// // Create your new service // // Create your new service
// service := micro.NewService( // service := micro.NewService(
// micro.Name("greeter"), // micro.Name("greeter"),
// // Set the router // // Set the router
// http.WithRouter(r), // http.WithProxy(r),
// ) // )
// //
// // Run the service // // Run the service
// service.Run() // service.Run()
func NewSingleHostRouter(url string) *Router { func NewSingleHostProxy(url string) *Proxy {
return &Router{ return &Proxy{
Backend: url, Backend: url,
} }
} }
@ -216,13 +227,13 @@ func NewSingleHostRouter(url string) *Router {
// ) // )
// //
func NewService(opts ...micro.Option) micro.Service { func NewService(opts ...micro.Option) micro.Service {
router := DefaultRouter router := DefaultProxy
name := DefaultName name := DefaultName
// prepend router to opts // prepend router to opts
opts = append([]micro.Option{ opts = append([]micro.Option{
micro.Name(name), micro.Name(name),
WithRouter(router), WithProxy(router),
}, opts...) }, opts...)
// create the new service // create the new service

View File

@ -4,13 +4,13 @@ package proxy
import ( import (
"context" "context"
"github.com/micro/go-micro/init" "github.com/micro/go-micro/options"
"github.com/micro/go-micro/server" "github.com/micro/go-micro/server"
) )
// Proxy can be used as a proxy server for go-micro services // Proxy can be used as a proxy server for go-micro services
type Proxy interface { type Proxy interface {
init.Options options.Options
// ServeRequest will serve a request // ServeRequest will serve a request
ServeRequest(context.Context, Request, Response) error ServeRequest(context.Context, Request, Response) error
// run the proxy // run the proxy