项目文件夹

文件
Asim Aslam c7657f73f4
goreleaser / goreleaser (push) Has been cancelled
Refactor agent plan storage, update docs, and release v6 (#2977)
* test(harness): read agent plan from the scoped store

The store-scoping change moved an agent's plan from the default table
key agent/{name}/plan to its own table (database "agent", table {name},
key "plan"). The plan-delegate harness tests still read the old key and
failed with 'not found'; read through store.Scope(mem, "agent", name)
like the agent does.

* docs: orient agents-first across README, landing, and docs overview

Lead with agents (then services and flows), surface MCP + A2A as the
interop story, and frame agents as services. Landing hero and feature
grid reordered agents-first with an A2A gateway card.

* v6: module path go-micro.dev/v6, TLS secure by default, NewService

Cut v6. Three breaking changes, bundled so the major bump is paid once:

- Module path go-micro.dev/v5 -> go-micro.dev/v6 across all imports + go.mod.
- TLS verification on by default (was off). MICRO_TLS_SECURE removed;
  MICRO_TLS_INSECURE=true opts out for self-signed/dev.
- micro.NewService(name, opts...) is the canonical service constructor,
  symmetric with NewAgent/NewFlow; micro.New kept as a deprecated alias;
  the old name-less NewService(opts...) removed. Generators emit NewService.

Also ports the JWT auth token provider in-module (go-micro.dev/v6/auth/jwt/token
on golang-jwt/jwt/v5), dropping the v5-pinned github.com/micro/plugins/v5/auth/jwt
and the deprecated dgrijalva/jwt-go.

Docs/README/landing updated to v6 and @latest; v5->v6 migration guide added;
CHANGELOG cut as [6.0.0]. Blog posts left at their historical versions.

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-06-18 11:55:35 +01:00

224 行
4.8 KiB
Go

package service
import (
"os"
"os/signal"
rtime "runtime"
"sync"
"go-micro.dev/v6/client"
"go-micro.dev/v6/cmd"
signalutil "go-micro.dev/v6/internal/util/signal"
log "go-micro.dev/v6/logger"
"go-micro.dev/v6/model"
"go-micro.dev/v6/server"
"go-micro.dev/v6/store"
)
// Service is the interface for a go-micro service.
type Service interface {
// Name returns the service name.
Name() string
// Init initializes options. Parses command line flags on first call.
Init(...Option)
// Options returns the current options.
Options() Options
// Handle registers a handler with optional server.HandlerOption args.
Handle(v interface{}, opts ...server.HandlerOption) error
// Client returns the RPC client.
Client() client.Client
// Server returns the RPC server.
Server() server.Server
// Model returns the data model backend.
Model() model.Model
// Start the service (non-blocking).
Start() error
// Stop the service.
Stop() error
// Run starts the service, blocks on signal/context, then stops.
Run() error
// String returns the implementation name.
String() string
}
type serviceImpl struct {
opts Options
once sync.Once
}
// New creates a new service with the given options.
func New(opts ...Option) Service {
return &serviceImpl{
opts: newOptions(opts...),
}
}
func (s *serviceImpl) Name() string {
return s.opts.Server.Options().Name
}
// Init initializes options. Additionally it calls cmd.Init
// which parses command line flags. cmd.Init is only called
// on first Init.
func (s *serviceImpl) Init(opts ...Option) {
// process options
for _, o := range opts {
o(&s.opts)
}
s.once.Do(func() {
// set cmd name
if len(s.opts.Cmd.App().Name) == 0 {
s.opts.Cmd.App().Name = s.Server().Options().Name
}
// Initialize the command flags, overriding new service
if err := s.opts.Cmd.Init(
cmd.Auth(&s.opts.Auth),
cmd.Broker(&s.opts.Broker),
cmd.Registry(&s.opts.Registry),
cmd.Transport(&s.opts.Transport),
cmd.Client(&s.opts.Client),
cmd.Config(&s.opts.Config),
cmd.Server(&s.opts.Server),
cmd.Store(&s.opts.Store),
cmd.Profile(&s.opts.Profile),
); err != nil {
s.opts.Logger.Log(log.FatalLevel, err)
}
// Scope the service's store to its own table (database "service",
// table = service name), consistent with how agents ("agent/{name}")
// and flows ("flow/{name}") scope their state. This replaces the
// older Init(store.Table(name)) global mutation with a composable
// scoped handle: each service gets an isolated handle that works
// even when several run in one process. When the service uses the
// package default store, bridge it to the same scope so handlers
// that reach for store.DefaultStore stay isolated too.
name := s.opts.Cmd.App().Name
wasDefault := s.opts.Store == store.DefaultStore
s.opts.Store = store.Scope(s.opts.Store, "service", name)
if wasDefault {
store.DefaultStore = s.opts.Store
}
})
}
func (s *serviceImpl) Options() Options {
return s.opts
}
func (s *serviceImpl) Client() client.Client {
return s.opts.Client
}
func (s *serviceImpl) Server() server.Server {
return s.opts.Server
}
func (s *serviceImpl) Model() model.Model {
return s.opts.Model
}
func (s *serviceImpl) String() string {
return "micro"
}
func (s *serviceImpl) Start() error {
for _, fn := range s.opts.BeforeStart {
if err := fn(); err != nil {
return err
}
}
if err := s.opts.Server.Start(); err != nil {
return err
}
for _, fn := range s.opts.AfterStart {
if err := fn(); err != nil {
return err
}
}
return nil
}
func (s *serviceImpl) Stop() error {
var gerr error
for _, fn := range s.opts.BeforeStop {
if err := fn(); err != nil {
gerr = err
}
}
if err := s.opts.Server.Stop(); err != nil {
return err
}
for _, fn := range s.opts.AfterStop {
if err := fn(); err != nil {
gerr = err
}
}
return gerr
}
func (s *serviceImpl) Handle(v interface{}, opts ...server.HandlerOption) error {
return s.opts.Server.Handle(
s.opts.Server.NewHandler(v, opts...),
)
}
func (s *serviceImpl) Run() (err error) {
logger := s.opts.Logger
// exit when help flag is provided
for _, v := range os.Args[1:] {
if v == "-h" || v == "--help" {
os.Exit(0)
}
}
// start the profiler
if s.opts.Profile != nil {
// to view mutex contention
rtime.SetMutexProfileFraction(5)
// to view blocking profile
rtime.SetBlockProfileRate(1)
if err = s.opts.Profile.Start(); err != nil {
return err
}
defer func() {
if nerr := s.opts.Profile.Stop(); nerr != nil {
logger.Log(log.ErrorLevel, nerr)
}
}()
}
logger.Logf(log.InfoLevel, "Starting [service] %s", s.Name())
if err = s.Start(); err != nil {
return err
}
ch := make(chan os.Signal, 1)
if s.opts.Signal {
signal.Notify(ch, signalutil.Shutdown()...)
}
select {
// wait on kill signal
case <-ch:
// wait on context cancel
case <-s.opts.Context.Done():
}
return s.Stop()
}