Building Modern Distributed Applications in Go with Service Weaver Shiju Varghese https://shijuvar.medium.com
About Me • Solutions Architect, focused on Distributed Systems • Consulting and Training on Go and Distributed Systems • Published Author: Web Development with Go (Apress, 2015, US edition), Go Recipes (Apress, 2016, US edition) • Awarded Microsoft MVP seven times • Speaker at numerous conferences • shijuvar.medium.com | github.com/shijuvar | linkedin.com/in/shijuvar
Agenda • Introduction to Service Weaver • Service Weaver components • Demo: Writing Service Weaver applications as modular monolith • Running and deploying Service Weaver applications Source Code: https://github.com/shijuvar/service-weaver
Monolith Vs Microservices
Microservices Architecture • Composed of a suite of small services • Each service runs in its own process • Greater fl exibility for scalability
Coupling of Logical and Physical Boundaries Microservice Microservice Microservice Logical Boundary Physical Boundary Deployment Deployment Deployment
Microservices • Combines logical boundaries where how code is written with physical boundaries where how code is deployed • Tight coupling between your logical separation of code with the deployment infrastructure.
Service Weaver
Decoupling of Logical and Physical Boundaries • Write as a modular monolith and compile it into a single binary • Deploy as a set of microservices (or as Monolith itself) • Best of both Monolith and Microservices
Service Weaver is a programming framework that makes it easy to write, deploy, and manage distributed applications written in the Go programming language. You can run, test, and debug a Service Weaver application locally on your machine, and then deploy the application to the cloud with a single command. $ go run . # Run locally. $ weaver gke deploy weaver.toml # Run in the cloud.
Installation $ go install github.com/ServiceWeaver/weaver/cmd/weaver@latest For GKE deployments $ go install github.com/ServiceWeaver/weaver-gke/cmd/weaver-gke@latest Install Service Weaver
Service Weaver Components • Modularise applications by using components • A component is a kind of an Actor (of Actor model) that represents a computational entity • Components built around core business logic • Components are de fi ned as Go interface • Components interact with each other by calling the methods de fi ned by the interfaces. • Service Weaver uses its own RPC mechanism for making the method calls in Microservices based deployment. Otherwise, it will be a local method call.
Component Interface Valid signatures of behavior contracts • Every method in a component interface must receive a context.Context as its fi rst argument and return an error as its fi nal result • All other arguments must be serializable. type Service interface { MakePayment(ctx context.Context, orderPayment model.OrderPayment) error }
type OrderPayment struct { weaver.AutoMarshal OrderID string CustomerID string Amount float64 } type Notification struct { weaver.AutoMarshal OrderID string CustomerID string Event string Modes []string } Serializable Types Embedding weaver.AutoMarshal to make types as serialisable
type Service interface { MakePayment(ctx context.Context, orderPayment model.OrderPayment) error } type implementation struct { weaver.Implements[Service] } func (s *implementation) MakePayment(ctx context.Context, orderPayment model.OrderPayment) error { } Component de fi ned as an interface Provides an implementation by embedding the generic type weaver.Implements[T]
type Server struct { weaver.Implements[weaver.Main] handler http.Handler // http handler instance paymentService weaver.Ref[paymentservice.Service] notificationService weaver.Ref[notificationservice.Service] orderRepository weaver.Ref[cockroachdb.Repository] orderapi weaver.Listener //`weaver:"orderapi"` } Component references other components using weaver.Ref[T] • Embedded weaver.Implements[weaver.Main] into main component • A component implementation may use network listeners using weaver.Listener
if err := s.orderRepository.Get().CreateOrder(ctx, order); err != nil { } if err := s.paymentService.Get().MakePayment(ctx, orderPayment); err != nil { } if err := s.notificationService.Get().Send(ctx, notification); err != nil { } Calling the methods of referenced components. Get method returns the component Client.
Configuration files (.toml)
Running App
Better integration with Observability • Provides a Logging API (In Google Cloud, logs are automatically exported to Google Cloud Logging) • Provides an API for Metrics (In Google Cloud, metrics are automatically exported to the Google Cloud Metrics Explorer ) • Tracing support with OpenTelemetry (In Google Cloud, traces are automatically exported to Google Cloud Trace)
Running the app locally with single process $ SERVICEWEAVER_CONFIG=weaver.toml go run . $ weaver single deploy weaver.toml Or with weaver single deploy
Running the app locally with Multiprocess $ weaver multi deploy weaver.toml
Deploying Service Weaver Apps • weaver-kube allows you to deploy a service weaver app in any kubernetes environment (GKE, Amazon EKS, AKS , etc.): https://github.com/ ServiceWeaver/weaver-kube • SSH deployer that allows you to deploy a service weaver app on bare metal machines/VMs: https://github.com/ServiceWeaver/weaver/tree/main/internal/ tool/ssh
Demo: Writing Service Weaver Applications Source Code: https://github.com/shijuvar/service-weaver
Thanks Shiju Varghese Senior Consulting Solutions Architect https://shijuvar.medium.com https://github.com/shijuvar https://linkedin.com/in/shijuvar Shiju Varghese’s Go Masterclass: https://bit.ly/shiju-go

Building Modern Distributed Applications in Go with Service Weaver

  • 1.
    Building Modern Distributed Applicationsin Go with Service Weaver Shiju Varghese https://shijuvar.medium.com
  • 2.
    About Me • SolutionsArchitect, focused on Distributed Systems • Consulting and Training on Go and Distributed Systems • Published Author: Web Development with Go (Apress, 2015, US edition), Go Recipes (Apress, 2016, US edition) • Awarded Microsoft MVP seven times • Speaker at numerous conferences • shijuvar.medium.com | github.com/shijuvar | linkedin.com/in/shijuvar
  • 3.
    Agenda • Introduction toService Weaver • Service Weaver components • Demo: Writing Service Weaver applications as modular monolith • Running and deploying Service Weaver applications Source Code: https://github.com/shijuvar/service-weaver
  • 4.
  • 5.
    Microservices Architecture • Composedof a suite of small services • Each service runs in its own process • Greater fl exibility for scalability
  • 6.
    Coupling of Logicaland Physical Boundaries Microservice Microservice Microservice Logical Boundary Physical Boundary Deployment Deployment Deployment
  • 7.
    Microservices • Combines logicalboundaries where how code is written with physical boundaries where how code is deployed • Tight coupling between your logical separation of code with the deployment infrastructure.
  • 9.
  • 10.
    Decoupling of Logicaland Physical Boundaries • Write as a modular monolith and compile it into a single binary • Deploy as a set of microservices (or as Monolith itself) • Best of both Monolith and Microservices
  • 11.
    Service Weaver isa programming framework that makes it easy to write, deploy, and manage distributed applications written in the Go programming language. You can run, test, and debug a Service Weaver application locally on your machine, and then deploy the application to the cloud with a single command. $ go run . # Run locally. $ weaver gke deploy weaver.toml # Run in the cloud.
  • 12.
    Installation $ go installgithub.com/ServiceWeaver/weaver/cmd/weaver@latest For GKE deployments $ go install github.com/ServiceWeaver/weaver-gke/cmd/weaver-gke@latest Install Service Weaver
  • 13.
    Service Weaver Components •Modularise applications by using components • A component is a kind of an Actor (of Actor model) that represents a computational entity • Components built around core business logic • Components are de fi ned as Go interface • Components interact with each other by calling the methods de fi ned by the interfaces. • Service Weaver uses its own RPC mechanism for making the method calls in Microservices based deployment. Otherwise, it will be a local method call.
  • 14.
    Component Interface Valid signaturesof behavior contracts • Every method in a component interface must receive a context.Context as its fi rst argument and return an error as its fi nal result • All other arguments must be serializable. type Service interface { MakePayment(ctx context.Context, orderPayment model.OrderPayment) error }
  • 15.
    type OrderPayment struct{ weaver.AutoMarshal OrderID string CustomerID string Amount float64 } type Notification struct { weaver.AutoMarshal OrderID string CustomerID string Event string Modes []string } Serializable Types Embedding weaver.AutoMarshal to make types as serialisable
  • 16.
    type Service interface{ MakePayment(ctx context.Context, orderPayment model.OrderPayment) error } type implementation struct { weaver.Implements[Service] } func (s *implementation) MakePayment(ctx context.Context, orderPayment model.OrderPayment) error { } Component de fi ned as an interface Provides an implementation by embedding the generic type weaver.Implements[T]
  • 17.
    type Server struct{ weaver.Implements[weaver.Main] handler http.Handler // http handler instance paymentService weaver.Ref[paymentservice.Service] notificationService weaver.Ref[notificationservice.Service] orderRepository weaver.Ref[cockroachdb.Repository] orderapi weaver.Listener //`weaver:"orderapi"` } Component references other components using weaver.Ref[T] • Embedded weaver.Implements[weaver.Main] into main component • A component implementation may use network listeners using weaver.Listener
  • 18.
    if err :=s.orderRepository.Get().CreateOrder(ctx, order); err != nil { } if err := s.paymentService.Get().MakePayment(ctx, orderPayment); err != nil { } if err := s.notificationService.Get().Send(ctx, notification); err != nil { } Calling the methods of referenced components. Get method returns the component Client.
  • 19.
  • 20.
  • 22.
    Better integration withObservability • Provides a Logging API (In Google Cloud, logs are automatically exported to Google Cloud Logging) • Provides an API for Metrics (In Google Cloud, metrics are automatically exported to the Google Cloud Metrics Explorer ) • Tracing support with OpenTelemetry (In Google Cloud, traces are automatically exported to Google Cloud Trace)
  • 23.
    Running the applocally with single process $ SERVICEWEAVER_CONFIG=weaver.toml go run . $ weaver single deploy weaver.toml Or with weaver single deploy
  • 25.
    Running the applocally with Multiprocess $ weaver multi deploy weaver.toml
  • 27.
    Deploying Service WeaverApps • weaver-kube allows you to deploy a service weaver app in any kubernetes environment (GKE, Amazon EKS, AKS , etc.): https://github.com/ ServiceWeaver/weaver-kube • SSH deployer that allows you to deploy a service weaver app on bare metal machines/VMs: https://github.com/ServiceWeaver/weaver/tree/main/internal/ tool/ssh
  • 28.
    Demo: Writing ServiceWeaver Applications Source Code: https://github.com/shijuvar/service-weaver
  • 29.
    Thanks Shiju Varghese Senior ConsultingSolutions Architect https://shijuvar.medium.com https://github.com/shijuvar https://linkedin.com/in/shijuvar Shiju Varghese’s Go Masterclass: https://bit.ly/shiju-go