Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Go Cloud ͬͯ ͝ଘ஌Ͱ͔͢

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

ͭΒ͍Ͱ͢ΑͶ

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

ͦ͜Ͱ Go Cloud

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

؀ڥߏங

Slide 16

Slide 16 text

؀ڥߏங 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)

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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) }

Slide 21

Slide 21 text

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) }

Slide 22

Slide 22 text

ಉ͡໭Γ஋Λฦ͢ // 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)

Slide 23

Slide 23 text

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 }

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Enjoy Go Cloud :D