开发者社区> 问答> 正文

从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 1514 0
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
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多