gpt4 book ai didi

How can i use REST and gRPC in same spring boot project?(如何在同一个Spring Boot项目中使用REST和GRPC?)

转载 作者:bug小助手 更新时间:2023-10-25 21:43:36 28 4
gpt4 key购买 nike



I am creating a service that will have gRPC endpoints for inter-service communications
and REST endpoint for the client browser.

我正在创建一个服务,该服务将具有用于服务间通信的GRPC端点和用于客户端浏览器的REST端点。


what will the pom.xml be for it?

Pom.xml对它有什么作用呢?


since the gRPC server runs on 9000 and REST runs on 8080, how will this work?

由于gRPC服务器运行在9000上,而REST运行在8080上,这将如何工作?


更多回答
优秀答案推荐

pom.xml

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>your-service</artifactId>
<version>1.0.0</version>

<dependencies>
<!-- gRPC dependencies -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.42.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.42.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.42.0</version>
</dependency>

<!-- REST dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>


application.properties

application.properties


# gRPC server port
grpc.server.port=9000

# REST server port
server.port=8080


You can use grpc-gateway to bind them. You would find the link below and regarding your same port issue if you are going to use gRPC in microservice you can use the grpc-spring-boot-starters. You will the link to both below:

您可以使用GRPC-Gateway进行绑定。如果您要在微服务中使用GRPC,您可以使用GRPC-Spring-Boot-starters,您可以在下面找到关于您的相同端口问题的链接。您将链接到下面的这两个链接:


https://github.com/grpc-ecosystem/grpc-gateway

Https://github.com/grpc-ecosystem/grpc-gateway


https://github.com/yidongnan/grpc-spring-boot-starter/

https://github.com/yidongnan/grpc-spring-boot-starter/


更多回答

how does one application run the server in two ports? can you please share the maven compiler plugin as well? if I start the main function will it create the rest application server or rpc application server?

一个应用程序如何在两个端口上运行服务器?你能不能也分享一下maven编译器插件?如果我启动主函数,它将创建REST应用服务器还是RPC应用服务器?

is it possible in the first place?

首先,这有可能吗?

@Kakarot it is - they're hosted on separate ports, so you'll have to provide them with your domain, e.g.: example.com:9000 for gRPC. If you want to have them accessible on domain without a port, but rather: example.com/grpc & example.com/api - then you need to setup a reverse proxy, like Nginx.

@Kakarot就是-它们托管在不同的端口上,所以你必须为它们提供您的域名,例如:Example.com:9000 for GRPC。如果您希望在没有端口的情况下在域中访问它们,那么您需要设置一个反向代理,如nginx。

Please post detailed answer and then references.

请张贴详细的答案,然后参考。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com