springboot项目同时启动web服务和grpc服务

springboot项目同时启动web服务和grpc服务

      • [一. 创建项目](#一. 创建项目)
      • [二. 引入依赖](#二. 引入依赖)
      • [三. 测试](#三. 测试)
        • [3.1 http服务](#3.1 http服务)
        • [3.2 grpc服务](#3.2 grpc服务)
      • [四. 整体代码结构](#四. 整体代码结构)

前言

这是我在这个网站整理的笔记,有错误的地方请指出,关注我,接下来还会持续更新。 作者:神的孩子都在歌唱

一. 创建项目

我们创建一个maven项目

如下,maven项目创建成功了

二. 引入依赖

引入spring-boot-starter-web依赖和grpc-client-spring-boot-starter依赖

xml 复制代码
 <dependencies>
        <!-- BEGIN 如果想使用 Tomcat 注释掉以下代码 -->
        <!-- SpringBoot Web容器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
            <version>2.5.4</version>
        </dependency>
        <!-- web 容器使用 undertow 性能更强 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
            <version>2.5.4</version>
        </dependency>

        <dependency>
            <groupId>net.devh</groupId>
            <artifactId>grpc-client-spring-boot-starter</artifactId>
            <version>2.13.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>net.devh</groupId>
            <artifactId>grpc-server-spring-boot-starter</artifactId>
            <version>2.13.1.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.2</version>
            </extension>
        </extensions>
        <plugins>

            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.16.1:exe:${os.detected.classifier}</pluginArtifact>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

然后我们创建一个application.yml文件,指定两个服务的启动端口,不要设置为一样的端口,因为HTTP和gRPC是两个不同的协议,它们的实现方式和通信方式都不同。在同一个端口上同时使用HTTP和gRPC会导致端口冲突,无法正常工作。

yml 复制代码
# 开发环境配置
server:
  # 服务器的HTTP端口,默认为8089
  port: 8089
grpc:
  server:
    port:
      9091

三. 测试

做完以上操作后,我们就可以编写http服务和grpc服务了,以下是一个简单的测试代码

启动类

java 复制代码
/**
 * @author: 那就叫小智吧
 * @date: 2023/9/18 14:50
 * @Version 1.0
 * @Description:
 */
@SpringBootApplication
public class ChenApplication {

    public static void main(String[] args)
    { SpringApplication.run(ChenApplication.class, args);
        System.out.println("启动成功");
    }
}
3.1 http服务

创建一个HelloWordWebController接口

java 复制代码
/**
 * @author: 那就叫小智吧
 * @date: 2023/9/18 13:43
 * @Version 1.0
 * @Description:
 */
@RestController
public class HelloWordWebController {

    @GetMapping(value = "/hello")
    public String hello() {
        return "神的孩子都在歌唱";
    }
}

测试结果

3.2 grpc服务

proto文件

protobuf 复制代码
syntax = "proto3";

option java_multiple_files = true;
package helloWordGrpc;

message Person {
  string first_name = 1;
  string last_name = 2;
}

message Greeting {
  string message = 1;
}

service HelloWorldService {
  rpc sayHello (Person) returns (Greeting);
}

这里需要mvn clean 和mvn install 一下 ,确保target文件里面有这几个对象

创建一个HelloWorldGrpcController接口

java 复制代码
@GrpcService
public class HelloWorldGrpcController extends HelloWorldServiceGrpc.HelloWorldServiceImplBase {
  @Override
  public void sayHello(Person request,
                       StreamObserver<Greeting> responseObserver) {
      String message = "Hello " + request.getFirstName() + " "
          + request.getLastName() + "!";
      Greeting greeting =
          Greeting.newBuilder().setMessage(message).build();

      responseObserver.onNext(greeting);
      responseObserver.onCompleted();
  }
}

测试结果

四. 整体代码结构

作者:神的孩子都在歌唱

本人博客:https://blog.csdn.net/weixin_46654114

转载说明:务必注明来源,附带本人博客连接。

相关推荐
AAA修煤气灶刘哥14 分钟前
别让Redis「歪脖子」!一次搞定数据倾斜与请求倾斜的捉妖记
redis·分布式·后端
AAA修煤气灶刘哥28 分钟前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
你的人类朋友1 小时前
什么是API签名?
前端·后端·安全
昵称为空C3 小时前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
会豪3 小时前
Electron-Vite (一)快速构建桌面应用
前端
中微子3 小时前
React 执行阶段与渲染机制详解(基于 React 18+ 官方文档)
前端
唐某人丶3 小时前
教你如何用 JS 实现 Agent 系统(2)—— 开发 ReAct 版本的“深度搜索”
前端·人工智能·aigc
中微子3 小时前
深入剖析 useState产生的 setState的完整执行流程
前端
架构师沉默3 小时前
设计多租户 SaaS 系统,如何做到数据隔离 & 资源配额?
java·后端·架构
遂心_3 小时前
JavaScript 函数参数传递机制:一道经典面试题解析
前端·javascript