Giter Club home page Giter Club logo

carrier's Introduction

define service interface

package io.carrier.example.spi

import io.carrier.rpc.Service
import javax.ws.rs.GET
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.Produces
import javax.ws.rs.core.MediaType

@Path("/")
@Produces(MediaType.APPLICATION_JSON)
interface HelloService: Service {
    @GET
    @Path("/say/{name}")
    fun sayHello(@PathParam("name") name: String): String
}

implement service

package io.carrier.example.server.impl

import io.carrier.example.spi.HelloService
import io.carrier.rpc.Export
import javax.inject.Singleton

@Singleton
@Export(HelloService::class)
class HelloServiceImpl : HelloService {
    override fun sayHello(name: String): String {
        return "hello $name"
    }
}

start server

import io.carrier.rpc.server.CarrierServer
import java.net.URI

class Application {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            val server = CarrierServer(Application::class.java.`package`.name, "io.carrier.example.spi")

            server.rpc(URI("rpc://0.0.0.0:10052")).http(URI("http://0.0.0.0:8080"))
            server.start()
            Runtime.getRuntime().addShutdownHook(Thread({ server.shutdown() }))
            server.join()
        }
    }
}

client call

import com.google.inject.AbstractModule
import com.google.inject.Guice
import io.carrier.example.spi.HelloService
import io.carrier.rpc.client.Client
import io.carrier.rpc.ProviderRegistry
import io.carrier.rpc.Registry

class Application {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            val injector = Guice.createInjector(object :AbstractModule() {
                override fun configure() {
                    bind(Registry::class.java).to(ProviderRegistry::class.java).asEagerSingleton()
                }
            })

            val registry = injector.getInstance(Registry::class.java) as ProviderRegistry
            registry.start()
            registry.register(HelloService::class.java.name, "127.0.0.1", 10052)
            val service = injector.getInstance(Client::class.java).create(HelloService::class.java)
            (0 until 10).forEach {
                println(service.sayHello("comyn"))
            }
            registry.shutdown()
        }
    }
}

http call

curl http://127.0.0.1:8080/say/comyn

carrier's People

Contributors

comynli avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.