package com.gaoli.impl; import com.gaoli.AccountQryServiceGrpc; import java.io.IOException; public class Server { private static int port = 8883; private static io.grpc.Server server; private void start() throws IOException { AccountQryServiceGrpc.AccountQryServiceImplBase accountServiceImpl = new AccountQryServiceImpl();// 能继续使用.addService添加多个服务 server = io.grpc.ServerBuilder.forPort(port).addService(accountServiceImpl).build(); server.start(); System.out.println("Server start success on port:" + port); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { Server.this.stop(); } }); } private void stop() { if (server != null) { server.shutdown(); } } private void blockUntilShutdown() throws InterruptedException { if (server != null) { server.awaitTermination(); } } public static void main(String[] args) throws IOException, InterruptedException { final Server server = new Server(); server.start(); server.blockUntilShutdown(); } }