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

Cad656ed619672b702191833dc819943?s=47 d-kuro
September 18, 2018

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

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

Cad656ed619672b702191833dc819943?s=128

d-kuro

September 18, 2018
Tweet

Transcript

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

    Kurosawa @ponde_m
  2. ࣗݾ঺հ Name: Dai Kurosawa Company: Classmethod, Inc. Position: Software Engineer

    Account: Twitter: @ponde_m GitHub: @d-kuro
  3. Go Cloud ͬͯ ͝ଘ஌Ͱ͔͢

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

  5. ֤छΫϥ΢υϓϩόΠμͰ ࣅͨΑ͏ͳαʔϏε • Storage • Amazon S3 • Google Cloud

    Storage • MySQL • Amazon RDS • Cloud SQL • ͳͲͳͲ (ଞʹ΋͍ͬͺ͍)
  6. ࣅͨΑ͏ͳαʔϏεͰ΋ API ͷΠϯλϑΣʔε͸ҧ͏ // Amazon S3 func (c *S3) PutObject(input

    *PutObjectInput) (*PutObjectOutput, error) // Google Cloud Strage func (o *ObjectHandle) NewWriter (ctx context.Context) *Writer
  7. Ϋϥ΢υϓϩόΠμʹΑΔ ϩοΫΠϯ໰୊ • ֤छΫϥ΢υϓϩόΠμͰࣅͨΑ͏ͳαʔϏε͕ల։͞Ε͍ͯΔͱݴͬ ͯ΋ API ͷΠϯλϑΣʔεͱ͔͸ҧ͏ • ΞʔΩςΫνϟ΍ίʔυϨϕϧͰͷϩοΫΠϯ͸Ͳ͏ͯ͠΋ൃੜͯ͠ ͠·͏

    • ͜ͷγεςϜ/ϥΠϒϥϦ͸ಛఆͷΫϥ΢υͰ͔͠ಈ͖·ͤΜ໰୊ • “΋͠”Ϋϥ΢υҠߦ͢Δ͜ͱʹͳͬͨΒͭΒ͍ • ͭΒ͍
  8. ͭΒ͍Ͱ͢ΑͶ

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

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

  11. ͦ͜Ͱ Go Cloud

  12. Go Cloud ͱ͸ • ༷ʑͳΫϥ΢υͷػೳΛ൚༻తͳ API Ͱར༻Ͱ͖Δ
 (Α͏ʹͳΔ͜ͱΛ໨ࢦͯ͠Δ) • OSS

    • ݱ࣌఺Ͱ͸ AWS ͱ GCP ʹରԠ • ࠓޙ΋ͬͱଟ͘ͷΫϥ΢υϓϩόΠμʹରԠ͢Δܭը • ·ͩ v0.2.0 (ൃද͞Εͨͷ͸ 18.07.24)
  13. ·ͩ v0.2.0 • Ͱ͖Δ͜ͱ͕গͳ͍ • Blob storage • Runtime configuration

    • Connecting MySQL • Server startup, request logging, tracing,
 health checking
  14. Go Cloud Λ࢖ͬͯ Ϋϥ΢υετϨʔδʹ ϑΝΠϧΛ PUT ͢Δ

  15. ؀ڥߏங

  16. ؀ڥߏங 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)
  17. 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
  18. TIPS • IntelliJ ͷͻͱ͸ Go Module ༗ޮʹ͠ͱ͖·͠ΐ͏ • GoLand ͷਓͷͻͱ͸ͬͪ͜

    • (https://pleiades.io/help/go/create-a-project-with-vgo-integration.html)
  19. ηοτΞοϓίʔυ • Go Cloud Ͱ͸ಛఆͷϓϥοτϑΥʔϜʹґଘ͢Δ΋ͷ͸ ηοτΞοϓίʔυͷΈ • ֤ϓϥοτϑΥʔϜͷ SDK Ͱૢ࡞Λ͍ͯͨ͠Γ͢Δͱ

    ϏδωεϩδοΫ΋ϓϥοτϑΥʔϜʹґଘͯ͠͠·͏ • ϏδωεϩδοΫ͸ಛఆͷϓϥοτϑΥʔϜʹґଘ͠ͳ͍ • ผͷΫϥ΢υʹҠߦ͢Δͷ͕؆୯ʹͳΔ
  20. 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) }
  21. 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) }
  22. ಉ͡໭Γ஋Λฦ͢ // 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)
  23. 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 }
  24. ֤ૢ࡞ͷந৅Խ • Go Cloud Ͱ͸֤Ϋϥ΢υαʔϏεͷૢ࡞ΛΠϯλϑΣʔε ͱͯ͠ந৅Խ͍ͯ͠Δ • ͦΕͧΕͷ SDK ͷૢ࡞Λ

    Go Cloud ͕ϥοϓ • ͲͷΫϥ΢υͳͷ͔Λҙࣝ͢Δ͜ͱͳ͘αʔϏεΛར༻ Ͱ͖Δ b, err := setupBucket(context.Background(), *cloud, *bucketName) … w, err := b.NewWriter(ctx, file, nil)
  25. ·ͱΊ • Go Cloud ͸֤Ϋϥ΢υϓϩόΠμؒͷΠϯλ ϑΣʔεͷҧ͍Λٵऩ͢Δ൚༻తͳ API Λఏ ڙͯ͘͠ΕΔ •

    ·ͩ·ͩͰ͖Δ͜ͱ͸গͳ͍ͷͰࠓޙʹظ଴ • ࠓͳΒίϯτϦϏϡʔγϣϯνϟϯε
  26. Enjoy Go Cloud :D