欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot項(xiàng)目同時(shí)啟動(dòng)web服務(wù)和grpc服務(wù)的方法

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

前言

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

一. 創(chuàng)建項(xiàng)目

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

image-20230918105933085

如下,maven項(xiàng)目創(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 性能更強(qiáng) -->
        <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)建一個(gè)application.yml文件,指定兩個(gè)服務(wù)的啟動(dòng)端口,不要設(shè)置為一樣的端口,因?yàn)镠TTP和gRPC是兩個(gè)不同的協(xié)議,它們的實(shí)現(xiàn)方式和通信方式都不同。在同一個(gè)端口上同時(shí)使用HTTP和gRPC會(huì)導(dǎo)致端口沖突,無(wú)法正常工作。

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

三. 測(cè)試

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

啟動(dòng)類

/**
 * @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("啟動(dòng)成功");
    }
}

3.1 http服務(wù)

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

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

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

測(cè)試結(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文件里面有這幾個(gè)對(duì)象

image-20230918151931848

創(chuàng)建一個(gè)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();
  }
}

測(cè)試結(jié)果

image-20230918151504401

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

image-20230918152154104

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

相關(guān)文章

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

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

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

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

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

    解決IDEA中下載free maven plugin插件無(wú)效的問(wèn)題

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

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

    java中的super關(guān)鍵字是一個(gè)引用變量,用于引用直接父類對(duì)象,下面這篇文章主要給大家介紹了關(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)知識(shí)點(diǎn)和代碼,需要的朋友們學(xué)習(xí)下。
    2019-04-04
  • SpringMVC接收頁(yè)面表單參數(shù)

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

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

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

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

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

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

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

    本文主要介紹了spring中BeanUtils.copyProperties的使用(深拷貝,淺拷貝),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(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)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-11-11

最新評(píng)論