亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot項目同時啟動web服務(wù)和grpc服務(wù)的方法

 更新時間:2024年02月02日 10:18:18   作者:神的孩子都在歌唱  
本文主要介紹了springboot項目同時啟動web服務(wù)和grpc服務(wù)的方法,通過實際代碼示例展示了實現(xiàn),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

這是我在這個網(wǎng)站整理的筆記,有錯誤的地方請指出

一. 創(chuàng)建項目

我們創(chuàng)建一個maven項目

image-20230918105933085

如下,maven項目創(chuàng)建成功了

image-20230918112649578

二. 引入依賴

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

 <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>

然后我們創(chuàng)建一個application.yml文件,指定兩個服務(wù)的啟動端口,不要設(shè)置為一樣的端口,因為HTTP和gRPC是兩個不同的協(xié)議,它們的實現(xiàn)方式和通信方式都不同。在同一個端口上同時使用HTTP和gRPC會導(dǎo)致端口沖突,無法正常工作。

# 開發(fā)環(huán)境配置
server:
  # 服務(wù)器的HTTP端口,默認(rèn)為8089
  port: 8089
grpc:
  server:
    port:
      9091

三. 測試

做完以上操作后,我們就可以編寫http服務(wù)和grpc服務(wù)了,以下是一個簡單的測試代碼

啟動類

/**
 * @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服務(wù)

創(chuàng)建一個HelloWordWebController接口

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

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

測試結(jié)果

image-20230918145649999

3.2 grpc服務(wù)

proto文件

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文件里面有這幾個對象

image-20230918151931848

創(chuàng)建一個HelloWorldGrpcController接口

@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();
  }
}

測試結(jié)果

image-20230918151504401

四. 整體代碼結(jié)構(gòu)

image-20230918152154104

 到此這篇關(guān)于springboot項目同時啟動web服務(wù)和grpc服務(wù)的方法的文章就介紹到這了,更多相關(guān)springboot啟動web和grpc服務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring mvc rest 接口選擇性加密解密詳情

    spring mvc rest 接口選擇性加密解密詳情

    這篇文章主要介紹了spring mvc rest 接口選擇性加密解密詳情,spring mvc rest接口以前是采用https加密的,但是現(xiàn)在需要更加安全的加密。而且不是對所有的接口進(jìn)行加密,是對部分接口進(jìn)行加密,接口返回值進(jìn)行解密
    2022-07-07
  • Java數(shù)據(jù)脫敏實現(xiàn)的方法總結(jié)

    Java數(shù)據(jù)脫敏實現(xiàn)的方法總結(jié)

    數(shù)據(jù)脫敏,指的是對某些敏感信息通過脫敏規(guī)則進(jìn)行數(shù)據(jù)的變形,實現(xiàn)敏感隱私數(shù)據(jù)的可靠保護(hù),本文主要是對后端數(shù)據(jù)脫敏實現(xiàn)的簡單總結(jié),希望對大家有所幫助
    2023-07-07
  • 解決IDEA中下載free maven plugin插件無效的問題

    解決IDEA中下載free maven plugin插件無效的問題

    這篇文章主要介紹了解決IDEA中下載free maven plugin插件無效的問題,本文通過圖文并茂的形式給大家分享解決方案,供大家參考,需要的朋友可以參考下
    2020-11-11
  • Java基礎(chǔ)之super關(guān)鍵字淺析

    Java基礎(chǔ)之super關(guān)鍵字淺析

    java中的super關(guān)鍵字是一個引用變量,用于引用直接父類對象,下面這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)之super關(guān)鍵字的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • java8、jdk8日期轉(zhuǎn)化成字符串詳解

    java8、jdk8日期轉(zhuǎn)化成字符串詳解

    在本篇文章中小編給大家整理了關(guān)于java8、jdk8日期轉(zhuǎn)化成字符串的相關(guān)知識點和代碼,需要的朋友們學(xué)習(xí)下。
    2019-04-04
  • SpringMVC接收頁面表單參數(shù)

    SpringMVC接收頁面表單參數(shù)

    本篇文章主要介紹了SpringMVC接收頁面表單參數(shù)的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-05-05
  • SpringBoot?注解?@AutoConfiguration?在?2.7?版本中被新增的使用方法詳解

    SpringBoot?注解?@AutoConfiguration?在?2.7?版本中被新增的使用方法詳解

    這篇文章主要介紹了SpringBoot?注解?@AutoConfiguration?在?2.7?版本中被新增(使用方法),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-09-09
  • Java EasyExcel導(dǎo)出報內(nèi)存溢出的解決辦法

    Java EasyExcel導(dǎo)出報內(nèi)存溢出的解決辦法

    使用EasyExcel進(jìn)行大數(shù)據(jù)量導(dǎo)出時容易導(dǎo)致內(nèi)存溢出,特別是在導(dǎo)出百萬級別的數(shù)據(jù)時,你有遇到過這種情況嗎,以下是小編整理的解決該問題的一些常見方法,需要的朋友可以參考下
    2024-10-10
  • spring中BeanUtils.copyProperties的使用(深拷貝,淺拷貝)

    spring中BeanUtils.copyProperties的使用(深拷貝,淺拷貝)

    本文主要介紹了spring中BeanUtils.copyProperties的使用(深拷貝,淺拷貝),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Mybatis中SqlMapper配置的擴(kuò)展與應(yīng)用詳細(xì)介紹(1)

    Mybatis中SqlMapper配置的擴(kuò)展與應(yīng)用詳細(xì)介紹(1)

    這篇文章主要介紹了Mybatis中SqlMapper配置的擴(kuò)展與應(yīng)用(1)的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11

最新評論