Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go Cloud を触ってみる / gopher-dojo-lt

d-kuro
September 18, 2018

Go Cloud を触ってみる / gopher-dojo-lt

Gopher 道場#3 卒業式& LT 大会の LT スライドになります
https://mercari.connpass.com/event/101178/

d-kuro

September 18, 2018
Tweet

More Decks by d-kuro

Other Decks in Programming

Transcript

  1. Go Cloud Λ
    ৮ͬͯΈΔ
    Gopherಓ৔#3 ଔۀࣜˍLTେձ
    2018.9.18
    Classmethod, Inc. Dai Kurosawa @ponde_m

    View full-size slide

  2. ࣗݾ঺հ
    Name: Dai Kurosawa
    Company: Classmethod, Inc.
    Position: Software Engineer
    Account:
    Twitter: @ponde_m
    GitHub: @d-kuro

    View full-size slide

  3. Go Cloud ͬͯ
    ͝ଘ஌Ͱ͔͢

    View full-size slide

  4. ֤छΫϥ΢υϓϩόΠμͰ
    ࣅͨΑ͏ͳαʔϏεͬͯ
    ͋Γ·͢ΑͶ

    View full-size slide

  5. ֤छΫϥ΢υϓϩόΠμͰ
    ࣅͨΑ͏ͳαʔϏε
    • Storage
    • Amazon S3
    • Google Cloud Storage
    • MySQL
    • Amazon RDS
    • Cloud SQL
    • ͳͲͳͲ (ଞʹ΋͍ͬͺ͍)

    View full-size slide

  6. ࣅͨΑ͏ͳαʔϏεͰ΋
    API ͷΠϯλϑΣʔε͸ҧ͏
    // Amazon S3
    func (c *S3) PutObject(input *PutObjectInput)
    (*PutObjectOutput, error)
    // Google Cloud Strage
    func (o *ObjectHandle) NewWriter
    (ctx context.Context) *Writer

    View full-size slide

  7. Ϋϥ΢υϓϩόΠμʹΑΔ
    ϩοΫΠϯ໰୊
    • ֤छΫϥ΢υϓϩόΠμͰࣅͨΑ͏ͳαʔϏε͕ల։͞Ε͍ͯΔͱݴͬ
    ͯ΋ API ͷΠϯλϑΣʔεͱ͔͸ҧ͏
    • ΞʔΩςΫνϟ΍ίʔυϨϕϧͰͷϩοΫΠϯ͸Ͳ͏ͯ͠΋ൃੜͯ͠
    ͠·͏
    • ͜ͷγεςϜ/ϥΠϒϥϦ͸ಛఆͷΫϥ΢υͰ͔͠ಈ͖·ͤΜ໰୊
    • “΋͠”Ϋϥ΢υҠߦ͢Δ͜ͱʹͳͬͨΒͭΒ͍
    • ͭΒ͍

    View full-size slide

  8. ͭΒ͍Ͱ͢ΑͶ

    View full-size slide

  9. ಛఆͷΫϥ΢υϓϩόΠμʹ
    ґଘ͠ͳ͍
    ൚༻తͳ API ͕ཉ͍͠……ʁ

    View full-size slide

  10. ϚϧνΫϥ΢υ
    (͔͍͍ͬ͜)

    View full-size slide

  11. ͦ͜Ͱ Go Cloud

    View full-size slide

  12. Go Cloud ͱ͸
    • ༷ʑͳΫϥ΢υͷػೳΛ൚༻తͳ API Ͱར༻Ͱ͖Δ

    (Α͏ʹͳΔ͜ͱΛ໨ࢦͯ͠Δ)
    • OSS
    • ݱ࣌఺Ͱ͸ AWS ͱ GCP ʹରԠ
    • ࠓޙ΋ͬͱଟ͘ͷΫϥ΢υϓϩόΠμʹରԠ͢Δܭը
    • ·ͩ v0.2.0 (ൃද͞Εͨͷ͸ 18.07.24)

    View full-size slide

  13. ·ͩ v0.2.0
    • Ͱ͖Δ͜ͱ͕গͳ͍
    • Blob storage
    • Runtime configuration
    • Connecting MySQL
    • Server startup, request logging, tracing,

    health checking

    View full-size slide

  14. Go Cloud Λ࢖ͬͯ
    Ϋϥ΢υετϨʔδʹ
    ϑΝΠϧΛ PUT ͢Δ

    View full-size slide

  15. ؀ڥߏங
    go get github.com/google/go-cloud
    go get github.com/google/go-cloud/wire/cmd/wire
    • go get ͢Δ
    • Wire ͸ DI πʔϧ
    • Ϋϥ΢υݻ༗ͷηοτΞοϓίʔυΛࣗಈͰੜ੒ͯ͘͠ΕΔ
    • ࠓճ͸࢖Θͳ͍ͷͰৄ͘͠͸υΩϡϝϯτΛಡ΋͏

    (https://github.com/google/go-cloud/tree/master/wire)

    View full-size slide

  16. Go 1.11 Modules
    • Go 1.11 ͔Βࢼݧಋೖ͞Ε͍ͯΔ Modules Ͱ

    go build ͨ͠ͱ͖ʹґଘύοέʔδΛউखʹμ΢ϯϩʔυ͢Δ
    • go.mod ʹґଘύοέʔδ͕ॻ͍ͯ͋Δ
    • https://github.com/google/go-cloud/blob/master/go.mod
    • ͪΌΜͱ Go ͷόʔδϣϯΛ্͛Α͏
    • GO111MODULE=on Λ๨Εͣʹ
    # ໌ࣔతͳґଘύοέʔδͷμ΢ϯϩʔυΛ͍ͨ͠ͱ͖
    $ go mod download

    View full-size slide

  17. TIPS
    • IntelliJ ͷͻͱ͸ Go Module ༗ޮʹ͠ͱ͖·͠ΐ͏
    • GoLand ͷਓͷͻͱ͸ͬͪ͜
    • (https://pleiades.io/help/go/create-a-project-with-vgo-integration.html)

    View full-size slide

  18. ηοτΞοϓίʔυ
    • Go Cloud Ͱ͸ಛఆͷϓϥοτϑΥʔϜʹґଘ͢Δ΋ͷ͸
    ηοτΞοϓίʔυͷΈ
    • ֤ϓϥοτϑΥʔϜͷ SDK Ͱૢ࡞Λ͍ͯͨ͠Γ͢Δͱ
    ϏδωεϩδοΫ΋ϓϥοτϑΥʔϜʹґଘͯ͠͠·͏
    • ϏδωεϩδοΫ͸ಛఆͷϓϥοτϑΥʔϜʹґଘ͠ͳ͍
    • ผͷΫϥ΢υʹҠߦ͢Δͷ͕؆୯ʹͳΔ

    View full-size slide

  19. Setup GCP
    func setupGCP(ctx context.Context, bucket string) (*blob.Bucket, error) {
    // DefaultCredentials assumes a user has logged in with gcloud.
    // See here for more information:
    // https://cloud.google.com/docs/authentication/getting-started
    creds, err := gcp.DefaultCredentials(ctx)
    if err != nil {
    return nil, err
    }
    c, err := gcp.NewHTTPClient(gcp.DefaultTransport(),
    gcp.CredentialsTokenSource(creds))
    if err != nil {
    return nil, err
    }
    // The bucket name must be globally unique.
    return gcsblob.OpenBucket(ctx, bucket, c)
    }

    View full-size slide

  20. Setup AWS
    func setupAWS(ctx context.Context, bucket string) (*blob.Bucket, error)
    {
    c := &aws.Config{
    // Either hard-code the region or use AWS_REGION.
    Region: aws.String("us-east-2"),
    // credentials.NewEnvCredentials assumes two environment
    variables are
    // present:
    // 1. AWS_ACCESS_KEY_ID, and
    // 2. AWS_SECRET_ACCESS_KEY.
    Credentials: credentials.NewEnvCredentials(),
    }
    s := session.Must(session.NewSession(c))
    return s3blob.OpenBucket(ctx, s, bucket)
    }

    View full-size slide

  21. ಉ͡໭Γ஋Λฦ͢
    // AWS
    func OpenBucket(ctx context.Context, sess client.ConfigProvider,
    bucketName string) (*blob.Bucket, error)
    // GCP
    func OpenBucket(ctx context.Context, bucketName string,
    client *gcp.HTTPClient) (*blob.Bucket, error)

    View full-size slide

  22. blob.Bucket
    type Bucket struct {
    b driver.Bucket
    }
    type Bucket interface {
    NewRangeReader(ctx context.Context, key string, offset, length int64)
    (Reader, error)
    NewTypedWriter(ctx context.Context, key string, contentType string,
    opt *WriterOptions) (Writer, error)
    Delete(ctx context.Context, key string) error
    }

    View full-size slide

  23. ֤ૢ࡞ͷந৅Խ
    • Go Cloud Ͱ͸֤Ϋϥ΢υαʔϏεͷૢ࡞ΛΠϯλϑΣʔε
    ͱͯ͠ந৅Խ͍ͯ͠Δ
    • ͦΕͧΕͷ SDK ͷૢ࡞Λ Go Cloud ͕ϥοϓ
    • ͲͷΫϥ΢υͳͷ͔Λҙࣝ͢Δ͜ͱͳ͘αʔϏεΛར༻
    Ͱ͖Δ
    b, err := setupBucket(context.Background(), *cloud, *bucketName)

    w, err := b.NewWriter(ctx, file, nil)

    View full-size slide

  24. ·ͱΊ
    • Go Cloud ͸֤Ϋϥ΢υϓϩόΠμؒͷΠϯλ
    ϑΣʔεͷҧ͍Λٵऩ͢Δ൚༻తͳ API Λఏ
    ڙͯ͘͠ΕΔ
    • ·ͩ·ͩͰ͖Δ͜ͱ͸গͳ͍ͷͰࠓޙʹظ଴
    • ࠓͳΒίϯτϦϏϡʔγϣϯνϟϯε

    View full-size slide

  25. Enjoy Go Cloud :D

    View full-size slide