Home >Backend Development >Golang >Application example: Use go-micro to build a microservice recommendation system

Application example: Use go-micro to build a microservice recommendation system

王林
王林Original
2023-06-18 12:43:411859browse

With the popularity of Internet applications, microservice architecture has become a popular architecture method. Among them, the key to the microservice architecture is to split the application into different services and communicate through RPC to achieve a loosely coupled service architecture. In this article, we will introduce how to use go-micro to build a microservice recommendation system based on actual cases.

1. What is a microservice recommendation system

The microservice recommendation system is a recommendation system based on microservice architecture. It will recommend different modules in the system (such as feature engineering, classifiers , sorter, etc.) are separated into different services and communicate through RPC to achieve an efficient, scalable, and easy-to-maintain recommendation system. The microservice recommendation system can be applied to recommendation scenarios in various vertical fields, such as e-commerce, music, video, etc.

2. How to use go-micro to implement a microservice recommendation system

go-micro is a microservice framework based on Go language. It provides service registration and discovery, load balancing, and RPC. Common functions such as communication are very suitable for building microservice architecture. Next we will introduce how to use go-micro to implement a microservice recommendation system.

  1. Install go-micro

First, we need to install the go-micro framework locally. You can use the following command to install it:

go get github.com/micro/go-micro/v2
  1. Create Service

According to the idea of ​​microservice architecture, we need to split different modules in the recommendation system into different services. Here, we take feature engineering as an example to implement a feature engineering service.

First, create a go module named feature_engineering locally:

mkdir -p feature_engineering
cd feature_engineering
go mod init feature_engineering

Then, we create a service to implement feature engineering related functions. Here, we take "feature extraction from user historical behavior" as an example to implement the following code:

package main

import (
    "context"
    "github.com/micro/go-micro/v2"
    pb "github.com/username/recommender/protos"
    "log"
)

type FeatureEngineeringService struct{}

func (s *FeatureEngineeringService) ExtractFeatures(ctx context.Context, req *pb.ExtractFeaturesRequest, rsp *pb.ExtractFeaturesResponse) error {
    log.Printf("Extracting features for user %d", req.UserId)
    rsp.Features = []float32{0.1, 0.2, 0.3}
    return nil
}

func main() {
    // Create service
    service := micro.NewService(
        micro.Name("go.micro.service.feature_engineering"),
    )

    // Initialise service
    service.Init()

    // Register handler
    pb.RegisterFeatureEngineeringHandler(service.Server(), new(FeatureEngineeringService))

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

In this service, we implement an RPC method named ExtractFeatures to receive data from the client. Request, extract the historical behavioral characteristics of the specified user and return it to the client.

  1. Registration Service

To deploy services of different modules on different machines, service registration and discovery need to be implemented. In go-micro, you can use registration centers such as etcd or consul to implement service registration and discovery. Here, we use etcd as the registry.

You can use the following command to start etcd:

docker run -p 2379:2379 -p 2380:2380 
    --name etcd 
    -v /tmp/etcd:/etcd-data 
    etcd:latest 
    /usr/local/bin/etcd 
    --name my-etcd-1 
    --data-dir /etcd-data 
    --advertise-client-urls http://0.0.0.0:2379 
    --listen-client-urls http://0.0.0.0:2379 
    --initial-advertise-peer-urls http://0.0.0.0:2380 
    --listen-peer-urls http://0.0.0.0:2380 
    --initial-cluster my-etcd-1=http://0.0.0.0:2380

After starting, you can visit http://localhost:2379/v2/keys/ to check whether etcd is running normally.

Then, we need to register in the service. You can add the following code after service.Init():

import (
    "github.com/micro/go-micro/v2/registry"
    "github.com/micro/go-plugins/registry/etcdv3/v2"
)

// Create new registry
etcdRegistry := etcdv3.NewRegistry(
    registry.Addrs("127.0.0.1:2379"),
)

// Register service
if err := etcdRegistry.Register(service.Options().Service); err != nil {
    log.Fatal(err)
}

This code will use etcd as the registration center and register the service to etcd middle.

  1. Call service

In other services, we can use the client provided by go-micro to make RPC calls. The following is a code example for calling the feature engineering service:

package main

import (
    "context"
    "fmt"
    "github.com/micro/go-micro/v2"
    "github.com/micro/go-micro/v2/registry"
    "github.com/micro/go-plugins/registry/etcdv3/v2"
    pb "github.com/username/recommender/protos"
)

func main() {
    // Create new registry
    etcdRegistry := etcdv3.NewRegistry(
        registry.Addrs("127.0.0.1:2379"),
    )

    // Create new service
    service := micro.NewService(
        micro.Registry(etcdRegistry),
    )

    // Initialise service
    service.Init()

    // Call feature engineering service
    featureEngineeringClient := pb.NewFeatureEngineeringService("go.micro.service.feature_engineering", service.Client())
    rsp, err := featureEngineeringClient.ExtractFeatures(context.TODO(), &pb.ExtractFeaturesRequest{UserId: 1})
    if err != nil {
        fmt.Println(err)
    }
    fmt.Printf("Features: %v", rsp.Features)
}

This code will use etcd as the registration center and create a client for the feature engineering service through the client provided by go-micro. Then call the ExtractFeatures method and print the return result. It should be noted that the passed go.micro.service.feature_engineering parameter is the name of the feature engineering service, which can be viewed by running the etcdctl get /micro/config command.

  1. Packaging and Deployment

Finally, we need to package different services and deploy them on different machines. You can use Docker for packaging and deploy via Kubernetes, or you can manually start the service on each machine.

3. Summary

Through this article, we can understand the advantages of microservice architecture and how to use go-micro to build an efficient, scalable, and easy-to-maintain microservice recommendation system. . Of course, go-micro is just one of many microservice frameworks, and readers can choose the appropriate framework for development according to their own needs. In short, microservice architecture has become the mainstream method of Internet application development, and will undoubtedly become more popular in the future.

The above is the detailed content of Application example: Use go-micro to build a microservice recommendation system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn