micro / go-micro

A Go microservices framework
https://go-micro.dev
Apache License 2.0
21.91k stars 2.35k forks source link

How can I create a new micro service in a micro service? #1609

Closed TiannV closed 4 years ago

TiannV commented 4 years ago

Example like this:

func newMicro() {
    // New Service
    service := micro.NewService(
        micro.Name("go.micro.srv.world"),
        micro.Version("latest"),

    // Initialise service
    service.Init()
    if err := service.Run(); err != nil {
        log.Fatal(err)
    }
}

func main() {
    // New Service
    service := micro.NewService(
        micro.Name("go.micro.srv.hello"),
        micro.Version("latest"),

    // Initialise service
    service.Init()

        go newMicro()
    if err := service.Run(); err != nil {
        log.Fatal(err)
    }
}

But, Something is wrong. Is there any better way?

ajenpan commented 4 years ago

you'd batter not do that. the two services created by mirco.NewService have same default opts if you donot set them. like this:

func newOptions(opts ...Option) Options 
    opt := Options{
        Auth:      auth.DefaultAuth,
        Broker:    broker.DefaultBroker,
        Cmd:       cmd.DefaultCmd,
        Config:    config.DefaultConfig,
        Client:    client.DefaultClient,
        Server:    server.DefaultServer,
        Store:     store.DefaultStore,
        Registry:  registry.DefaultRegistry,
        Runtime:   runtime.DefaultRuntime,
        Transport: transport.DefaultTransport,
        Context:   context.Background(),
        Signal:    true,
    }

so this default opts been inited twice and pinac

domwong commented 4 years ago

This isn't the intended use of micro or micro services in general. You should create them as separate services. Behaviour is undefined if you try to run two in a single process like that.