开发者社区 问答 正文

从String启动应用程序添加GRPC服务

我需要添加一个GRPC服务,但是我的应用程序在Spring启动时使用以下代码运行:

@SpringBootApplication
    @EnableCaching
    public class MyApplication {
        public static void main(String[] args) throws IOException, InterruptedException {
            ConfigurableApplicationContext server = SpringApplication.run(MyApplication.class, args);
        }
    }

在这段代码中,可以看到如何添加服务:

Server greetingServer = ServerBuilder.forPort(8080)
        .addService(ServerInterceptors.intercept(new GreetingServiceImpl(), jwtInterceptor, new TraceIdServerInterceptor()))
        .build();
    greetingServer.start();

但是这种方式不能在春季启动方式中启用。

如何从Sring引导代码添加GRPC服务?

提前致谢。

展开
收起
垚tutu 2019-12-12 09:55:27 1544 分享 版权
1 条回答
写回答
取消 提交回答
  • 从贵州电信网厅,到阿尔巴尼亚TTNET、委内瑞拉Cantv的固网激活等项目,再到激活产品研发,在中兴软创国际OSS产品线开发岗位上有6年开发经验,如今在阿里云业务运营中台做商品和账务,对企业级应用开发和架构有丰富经验。

    我没有实操过,可以考虑这样试试 添加spring-boot-starter-grpc

    <dependency>  
        <groupId>com.anoyi</groupId>  
        <artifactId>spring-boot-starter-grpc</artifactId>  
        <version>1.1.2.RELEASE</version>  
    </dependency>  
    

    添加配置

    spring:  
      grpc:  
        enable: true  
        port: 8080 -- 即grpc与springboot共用端口  
    

    然后以正常的@SpringBootApplication启动

    2020-03-31 17:30:29
    赞同 展开评论
问答分类:
问答地址: