github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_server_registry.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package ghttp
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  
    13  	"github.com/gogf/gf/v2/net/gipv4"
    14  	"github.com/gogf/gf/v2/net/gsvc"
    15  	"github.com/gogf/gf/v2/os/gctx"
    16  	"github.com/gogf/gf/v2/text/gstr"
    17  	"github.com/gogf/gf/v2/util/gconv"
    18  )
    19  
    20  // doServiceRegister registers current service to Registry.
    21  func (s *Server) doServiceRegister() {
    22  	if s.registrar == nil {
    23  		return
    24  	}
    25  	s.serviceMu.Lock()
    26  	defer s.serviceMu.Unlock()
    27  	var (
    28  		ctx      = gctx.GetInitCtx()
    29  		protocol = gsvc.DefaultProtocol
    30  		insecure = true
    31  		err      error
    32  	)
    33  	if s.config.TLSConfig != nil {
    34  		protocol = `https`
    35  		insecure = false
    36  	}
    37  	metadata := gsvc.Metadata{
    38  		gsvc.MDProtocol: protocol,
    39  		gsvc.MDInsecure: insecure,
    40  	}
    41  	s.service = &gsvc.LocalService{
    42  		Name:      s.GetName(),
    43  		Endpoints: s.calculateListenedEndpoints(ctx),
    44  		Metadata:  metadata,
    45  	}
    46  	s.Logger().Debugf(ctx, `service register: %+v`, s.service)
    47  	if len(s.service.GetEndpoints()) == 0 {
    48  		s.Logger().Warningf(ctx, `no endpoints found to register service, abort service registering`)
    49  		return
    50  	}
    51  	if s.service, err = s.registrar.Register(ctx, s.service); err != nil {
    52  		s.Logger().Fatalf(ctx, `%+v`, err)
    53  	}
    54  }
    55  
    56  // doServiceDeregister de-registers current service from Registry.
    57  func (s *Server) doServiceDeregister() {
    58  	if s.registrar == nil {
    59  		return
    60  	}
    61  	s.serviceMu.Lock()
    62  	defer s.serviceMu.Unlock()
    63  	if s.service == nil {
    64  		return
    65  	}
    66  	var ctx = gctx.GetInitCtx()
    67  	s.Logger().Debugf(ctx, `service deregister: %+v`, s.service)
    68  	if err := s.registrar.Deregister(ctx, s.service); err != nil {
    69  		s.Logger().Errorf(ctx, `%+v`, err)
    70  	}
    71  	s.service = nil
    72  }
    73  
    74  func (s *Server) calculateListenedEndpoints(ctx context.Context) gsvc.Endpoints {
    75  	var (
    76  		configAddr = s.config.Address
    77  		endpoints  = make(gsvc.Endpoints, 0)
    78  		addresses  = s.config.Endpoints
    79  	)
    80  	if configAddr == "" {
    81  		configAddr = s.config.HTTPSAddr
    82  	}
    83  	if len(addresses) == 0 {
    84  		addresses = gstr.SplitAndTrim(configAddr, ",")
    85  	}
    86  	for _, address := range addresses {
    87  		var (
    88  			addrArray     = gstr.Split(address, ":")
    89  			listenedIps   []string
    90  			listenedPorts []int
    91  		)
    92  		if len(addrArray) == 1 {
    93  			addrArray = append(addrArray, gconv.String(defaultEndpointPort))
    94  		}
    95  		// IPs.
    96  		switch addrArray[0] {
    97  		case "127.0.0.1":
    98  			// Nothing to do.
    99  		case "0.0.0.0", "":
   100  			intranetIps, err := gipv4.GetIntranetIpArray()
   101  			if err != nil {
   102  				s.Logger().Errorf(ctx, `error retrieving intranet ip: %+v`, err)
   103  				return nil
   104  			}
   105  			// If no intranet ips found, it uses all ips that can be retrieved,
   106  			// it may include internet ip.
   107  			if len(intranetIps) == 0 {
   108  				allIps, err := gipv4.GetIpArray()
   109  				if err != nil {
   110  					s.Logger().Errorf(ctx, `error retrieving ip from current node: %+v`, err)
   111  					return nil
   112  				}
   113  				s.Logger().Noticef(
   114  					ctx,
   115  					`no intranet ip found, using internet ip to register service: %v`,
   116  					allIps,
   117  				)
   118  				listenedIps = allIps
   119  				break
   120  			}
   121  			listenedIps = intranetIps
   122  		default:
   123  			listenedIps = []string{addrArray[0]}
   124  		}
   125  		// Ports.
   126  		switch addrArray[1] {
   127  		case "0":
   128  			listenedPorts = s.GetListenedPorts()
   129  		default:
   130  			listenedPorts = []int{gconv.Int(addrArray[1])}
   131  		}
   132  		for _, ip := range listenedIps {
   133  			for _, port := range listenedPorts {
   134  				endpoints = append(endpoints, gsvc.NewEndpoint(fmt.Sprintf(`%s:%d`, ip, port)))
   135  			}
   136  		}
   137  	}
   138  	return endpoints
   139  }