grpc etcd服务注册发现

1、新建maven工程

pom.xml文件如下:


         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    com.chen
    grpc
    1.0-SNAPSHOT

    grpc
   
    http://www.example.com

   
        UTF-8
        1.7
        1.7
   

   
       
            io.grpc
            grpc-netty
            1.12.0
       

       
            io.grpc
            grpc-protobuf
            1.12.0
       

       
            io.grpc
            grpc-stub
            1.12.0
       

   

   
       
           
                kr.motd.maven
                os-maven-plugin
                1.5.0.Final
           

       

       
           
                org.xolstice.maven.plugins
                protobuf-maven-plugin
                0.5.1
               
                    com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}
                    grpc-java
                    io.grpc:protoc-gen-grpc-java:1.12.0:exe:${os.detected.classifier}
               

               
                   
                       
                            compile
                            compile-custom
                       

                   

               

           

       

   

2、protobuf生java代码

先protobuf:compile,然后protobuf:compile-custom

grpc etcd服务注册发现_第1张图片

compile成功后生成文件结构大致如下

grpc etcd服务注册发现_第2张图片

3、服务方代码

public class ApiserverImpl extends ApiServerGrpc.ApiServerImplBase {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApiserverImpl.class);

    @Override
    public void addJob(Apiserver.JobRequest request, StreamObserver responseObserver) {
        System.out.println("Step 1 创建任务");
        Apiserver.APIResponse response = Apiserver.APIResponse.newBuilder()
                .setRequestID("")
                .setSuccess(true).setMessage("").build();
        // 响应
        responseObserver.onNext(response);
        // 结束
        responseObserver.onCompleted();
    }
}

4、消费方代码

public class ApiserverConsumerService extends AbstractConsumer {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(ApiserverConsumerService.class);

    public ApiserverConsumerService(String serverAppName) {
        super(serverAppName);
    }

    @GrpcCallMethodAnnotation
    public Boolean addJob(String msg) {
        System.out.println("----------api server----------");
//        HashMap param = JSON.parseObject(msg, HashMap.class);
//        System.out.println("uuid = " + param.get("uuid"));

        Apiserver.Job job = Apiserver.Job.newBuilder().setUuid("kk001").setType(2).build();

        Apiserver.JobRequest jobRequest = Apiserver.JobRequest.newBuilder().setJob(job).build();
        ApiServerGrpc.ApiServerBlockingStub stub = getStub();
        Apiserver.APIResponse response = stub.withDeadlineAfter(1000, TimeUnit.MILLISECONDS).addJob(jobRequest);
        System.out.println("----------add job response = " + response.toString() + "----------");
        return true;
    }

    @Override
    public ApiServerGrpc.ApiServerBlockingStub initStub(Channel channel) {
        ApiServerGrpc.ApiServerBlockingStub stub = ApiServerGrpc.newBlockingStub(channel);
        return stub;
    }

    @Override
    public ServiceDescriptor getServiceDescriptor() {
        return ApiServerGrpc.getServiceDescriptor();
    }
}

5、测试代码

public class TestAddJob {

    private static final Logger LOGGER = LoggerFactory.getLogger(TestAddJob.class);

    public TestAddJob() {}

    public static void main(String[] args) {
        GrpcManager.setAppName("sigmax");
//        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//        ConsumerConfig consumerConfig = (ConsumerConfig)ac.getBean("consumerConfig");
//        BaseRegister register = new EtcdRegister("TestConsumer", "http://10.0.200.86:2379");
//        consumerConfig.setRegister(register);

        ConsumerConfig consumerConfig = new ConsumerConfig();
        BaseRegister register = new EtcdRegister("sigmax", "http://10.0.200.86:2379"); // dev
//        BaseRegister register = new EtcdRegister("sigmax", "http://10.0.228.114:2379"); // nx

        consumerConfig.setRegister(register);
        GrpcManager.setConsumerConfig(consumerConfig);

        ApiserverConsumerService service = new ApiserverConsumerService("sigmax");
        ConsumerServer consumerServer = new ConsumerServer(Lists.newArrayList(new AbstractConsumer[]{service}));

        try {
            consumerServer.init();
        } catch (Exception var6) {
        }

        Boolean result = service.addJob("test consumer");

    }
}

6、打包与发布

pom.xml 增加


    maven-assembly-plugin
    
        
        
        
        com.chen.sigmax.test.TestAddJob
        
        
        
            jar-with-dependencies
        
    

然后执行项目根目录下执行 mvn assembly:assembly,生成带依赖库的包target/sigmaxtest-1.0-SNAPSHOT-jar-with-dependencies.jar 。

linux只要布署java、maven环境,本地库~/.m2/repository正确,java -jar sigmaxtest-1.0-SNAPSHOT-jar-with-dependencies.jar即可执行指定的main方法。

你可能感兴趣的:(远程调用)