github.com/wfusion/gofusion@v1.1.14/mq/pulsar.go (about)

     1  package mq
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/pkg/errors"
     9  
    10  	"github.com/wfusion/gofusion/common/infra/watermill"
    11  	"github.com/wfusion/gofusion/common/infra/watermill/pubsub/pulsar"
    12  	"github.com/wfusion/gofusion/common/utils"
    13  	"github.com/wfusion/gofusion/config"
    14  
    15  	plsDrv "github.com/apache/pulsar-client-go/pulsar"
    16  )
    17  
    18  func newPulsar(ctx context.Context, appName, name string, conf *Conf, logger watermill.LoggerAdapter) (
    19  	pub Publisher, sub Subscriber) {
    20  	if conf.Producer {
    21  		pub = newPulsarPublisher(ctx, appName, name, conf, logger)
    22  	}
    23  
    24  	if conf.Consumer {
    25  		sub = newPulsarSubscriber(ctx, appName, name, conf, logger)
    26  	}
    27  
    28  	return
    29  }
    30  
    31  type pulsarPublisher struct {
    32  	*abstractMQ
    33  	publisher *pulsar.Publisher
    34  }
    35  
    36  func newPulsarPublisher(ctx context.Context, appName, name string,
    37  	conf *Conf, logger watermill.LoggerAdapter) Publisher {
    38  	cfg := pulsar.PublisherConfig{
    39  		URL:   fmt.Sprintf("pulsar://%s", strings.TrimPrefix(conf.Endpoint.Addresses[0], "pulsar://")),
    40  		AppID: config.Use(appName).AppName(),
    41  	}
    42  	hasUser := utils.IsStrNotBlank(conf.Endpoint.User)
    43  	hasPassword := utils.IsStrNotBlank(conf.Endpoint.Password)
    44  	hasAuthType := utils.IsStrNotBlank(conf.Endpoint.AuthType)
    45  	if hasUser && hasPassword {
    46  		cfg.Authentication = utils.Must(plsDrv.NewAuthenticationBasic(conf.Endpoint.User, conf.Endpoint.Password))
    47  	}
    48  	if hasAuthType {
    49  		cfg.Authentication = utils.Must(plsDrv.NewAuthentication(conf.Endpoint.AuthType, conf.Endpoint.Password))
    50  	}
    51  
    52  	pub, err := pulsar.NewPublisher(cfg, logger)
    53  	if err != nil {
    54  		panic(errors.Wrapf(err, "initialize mq component pulsar publisher failed: %s", err))
    55  	}
    56  
    57  	return &pulsarPublisher{
    58  		abstractMQ: newPub(ctx, pub, appName, name, conf, logger),
    59  		publisher:  pub,
    60  	}
    61  }
    62  
    63  func (p *pulsarPublisher) close() (err error) {
    64  	return p.publisher.Close()
    65  }
    66  
    67  type pulsarSubscriber struct {
    68  	*abstractMQ
    69  	subscriber *pulsar.Subscriber
    70  }
    71  
    72  func newPulsarSubscriber(ctx context.Context, appName, name string,
    73  	conf *Conf, logger watermill.LoggerAdapter) Subscriber {
    74  	cfg := &pulsar.SubscriberConfig{
    75  		URL:        fmt.Sprintf("pulsar://%s", strings.TrimPrefix(conf.Endpoint.Addresses[0], "pulsar://")),
    76  		QueueGroup: conf.ConsumerGroup,
    77  		Persistent: conf.Persistent,
    78  	}
    79  	hasUser := utils.IsStrNotBlank(conf.Endpoint.User)
    80  	hasPassword := utils.IsStrNotBlank(conf.Endpoint.Password)
    81  	hasAuthType := utils.IsStrNotBlank(conf.Endpoint.AuthType)
    82  	if hasUser && hasPassword {
    83  		cfg.Authentication = utils.Must(plsDrv.NewAuthenticationBasic(conf.Endpoint.User, conf.Endpoint.Password))
    84  	}
    85  	if hasAuthType {
    86  		params := conf.Endpoint.Password
    87  		switch conf.Endpoint.AuthType {
    88  		case "basic", "org.apache.pulsar.client.impl.auth.AuthenticationBasic":
    89  			params = utils.MustJsonMarshalString(map[string]string{
    90  				"username": conf.Endpoint.User,
    91  				"password": conf.Endpoint.Password,
    92  			})
    93  		}
    94  		cfg.Authentication = utils.Must(plsDrv.NewAuthentication(conf.Endpoint.AuthType, params))
    95  	}
    96  
    97  	sub, err := pulsar.NewSubscriber(cfg, logger)
    98  	if err != nil {
    99  		panic(errors.Wrapf(err, "initialize mq component pulsar subscriber failed: %s", err))
   100  	}
   101  
   102  	return &pulsarSubscriber{
   103  		abstractMQ: newSub(ctx, sub, appName, name, conf, logger),
   104  		subscriber: sub,
   105  	}
   106  }
   107  func (p *pulsarSubscriber) close() (err error) {
   108  	return p.subscriber.Close()
   109  }