当前位置: 首页 > news >正文

台州网站建设费用贵阳网站建设

台州网站建设费用,贵阳网站建设,今日国内31省市最新疫情通报,门户网站建设文章目录 前言项目结构代码示例父工程api moduleservice module 注意事项区别 本文记录下SpringBoot集成Dubbo启用gRPC协议,以及与原生 gRPC 在代码编写过程中的区别。 下面还有投票,帮忙投个票👍 前言 Dubbo 在 2.7.5 版本开始支持原生 gRP…

文章目录

  • 前言
  • 项目结构
  • 代码示例
    • 父工程
    • api module
    • service module
  • 注意事项
  • 区别

本文记录下SpringBoot集成Dubbo启用gRPC协议,以及与原生 gRPC 在代码编写过程中的区别。
下面还有投票,帮忙投个票👍

前言

Dubbo 在 2.7.5 版本开始支持原生 gRPC 协议,对于计划使用 HTTP/2 通信或者期望 gRPC 协议支持服务治理能力的,都可以考虑接入 Dubbo 体系启用 gRPC 协议。

由于官网给的 代码示例 是基于 spring,现在基本上都是基于SpringBoot开发,所以本文提供一下 SpringBoot 的代码示例。

此外还会简单说明 Dubbo 支持的原生 gRPC 协议与原生 gRPC 协议在代码开发时的区别。

如果对gRPC协议不了解的,后续文章会有更新,请持续关注。

项目结构

根据现在微服务开发的常见方式,先搭建一个项目,结构如下
在这里插入图片描述

这样的项目结构可以将服务的声明和实现隔离开,如果有 client 调用,直接添加api module 的依赖即可。

代码示例

项目结构确定好后需要做三件事

  1. 在项目中需要用到 grpc 和 dubbo 相关依赖,所以在父工程中的 pom.xml 文件添加两者的 BOM。
  2. gRPC 支持的序列化协议为 protobuf,我们在 api module 下添加 gRPC 所需依赖、插件以及 proto IDL文件。
  3. 在 service module 添加相关配置并进行 api service 的实现。

详细代码如下:

父工程

父工程中的 pom.xml 文件添加 grpc 和 dubbo 的 BOM。
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.demo</groupId><artifactId>nava</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><name>nava</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><dubbo.version>3.1.7</dubbo.version><grpc.version>1.44.1</grpc.version><spring-boot.version>2.6.11</spring-boot.version></properties><modules><module>nava-api</module><module>nava-service</module></modules><dependencyManagement><dependencies><dependency><groupId>io.grpc</groupId><artifactId>grpc-bom</artifactId><version>${grpc.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-bom</artifactId><version>${dubbo.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.demo</groupId><artifactId>nava-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>com.demo</groupId><artifactId>nava-service</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build></project>

api module

在 api module 中的 pom.xml 文件添加 dubbo 、gRPC 所需依赖、插件。
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.demo</groupId><artifactId>nava</artifactId><version>0.0.1-SNAPSHOT</version><relativePath>../pom.xml</relativePath></parent><artifactId>nava-api</artifactId><name>nava-api</name><description>api 模块,对外提供的 API</description><dependencies><dependency><groupId>io.grpc</groupId><artifactId>grpc-netty</artifactId><exclusions><exclusion><groupId>io.netty</groupId><artifactId>netty-codec-http2</artifactId></exclusion><exclusion><groupId>io.netty</groupId><artifactId>netty-handler-proxy</artifactId></exclusion></exclusions></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-netty-shaded</artifactId></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-protobuf</artifactId></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-stub</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-common</artifactId></dependency></dependencies><build><plugins><plugin><groupId>kr.motd.maven</groupId><artifactId>os-maven-plugin</artifactId><version>1.7.1</version><executions><execution><id>os-maven-plugin</id><phase>initialize</phase><goals><goal>detect</goal></goals></execution></executions></plugin><plugin><groupId>org.xolstice.maven.plugins</groupId><artifactId>protobuf-maven-plugin</artifactId><version>0.6.1</version><configuration><protocArtifact>com.google.protobuf:protoc:3.7.1:exe:${os.detected.classifier}</protocArtifact><pluginId>grpc-java</pluginId><pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact><protocPlugins><protocPlugin><id>dubbo-grpc</id><groupId>org.apache.dubbo</groupId><artifactId>dubbo-compiler</artifactId><version>0.0.1</version><mainClass>org.apache.dubbo.gen.grpc.DubboGrpcGenerator</mainClass></protocPlugin></protocPlugins></configuration><executions><execution><id>protobuf-maven-plugin</id><goals><goal>compile</goal><goal>compile-custom</goal></goals></execution></executions></plugin></plugins></build></project>

在main文件夹下面创建proto文件夹,以及 DemoService.proto 文件。
在这里插入图片描述

DemoService.proto

syntax = "proto3";option java_multiple_files = true;
option java_package = "com.demo.nava";
option java_outer_classname = "DemoServiceProto";
option objc_class_prefix = "DSP";// The greeting service definition.
service DemoService {// Sends a greetingrpc service (RequestData) returns (ResponseData) {}
}// The request message containing the user's name.
message RequestData {string name = 1;
}// The response message containing the greetings
message ResponseData {string message = 1;
}

service module

在 service module 中的 pom.xml 文件添加 api module 的依赖以及 dubbo 其他依赖。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.demo</groupId><artifactId>nava</artifactId><version>0.0.1-SNAPSHOT</version><relativePath>../pom.xml</relativePath></parent><artifactId>nava-service</artifactId><name>nava-service</name><description>service 模块,存放核心业务逻辑代码</description><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>com.demo</groupId><artifactId>nava-api</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-nacos</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.6.11</version><configuration><mainClass>com.demo.nava.NavaApplication</mainClass></configuration><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>

application.properties 文件中添加 dubbo 相关配置

application.properties


# 设置dubbo传输协议
dubbo.protocol.name=grpc
dubbo.protocol.port=-1
# dubbo nacos注册中心说明 https://cn.dubbo.apache.org/zh-cn/overview/mannual/java-sdk/reference-manual/registry/nacos/
dubbo.registry.address: nacos://nacos:nacos@${nacos.address:127.0.0.1}:8848

在 SpringBoot 启动类添加 @EnableDubbo 注解

在这里插入图片描述

添加 DemoServiceImpl 实现类进行业务编码。

DemoServiceImpl.java

package com.demo.nava.service;import com.demo.nava.DubboDemoServiceGrpc;
import com.demo.nava.RequestData;
import com.demo.nava.ResponseData;
import io.grpc.stub.StreamObserver;
import org.apache.dubbo.config.annotation.DubboService;@DubboService
public class DemoServiceImpl extends DubboDemoServiceGrpc.DemoServiceImplBase implements DubboDemoServiceGrpc.IDemoService {@Overridepublic void service(RequestData request, StreamObserver<ResponseData> responseObserver) {ResponseData reply = ResponseData.newBuilder().setMessage("Hello " + request.getName()).build();responseObserver.onNext(reply);responseObserver.onCompleted();}
}

注意事项

经过以上的步骤,一个简单的 SpringBoot 集成 Dubbo 启用 gRPC 协议的示例就完成了。这个时候直接启动项目是会报错的,因为protobuf相关的代码还没生成,我们需要对项目进行 maven install 以及 maven reload 操作。

maven install 的目的是为了生成protobuf相关代码,这个时候我们可以在 target 中看到

在这里插入图片描述

maven reload 的目的是为了更新加载 pom.xml 文件,从而将 api module 中生成的代码加载到 service module。

操作后就可以成功启动项目了。

区别

在项目启动成功后可以回头看下 Dubbo 支持的原生 gRPC 与原生 gRPC 在代码编写过程中的区别

  1. maven plugin 的区别,dubbo 在原先的基础上添加了 dubbo-grpc 的 plugin,目的是生成扩展的代码做到对 grpc 的支持。
    在这里插入图片描述
    对应生成的代码如下
    在这里插入图片描述

  2. service 实现区别,dubbo-grpc 的 plugin 生成了 dubbo 相关的 protobuf 的代码,所以在实现上有所区别。
    在这里插入图片描述

  3. RPC 调用区别,因为 grpc 接入了 dubbo 体系,所以使用 Dubbo 风格,基于接口的编程来定义和使用远程服务。
    在这里插入图片描述


文章转载自:
http://cattish.qpqb.cn
http://petrotectonics.qpqb.cn
http://xanthoxylum.qpqb.cn
http://rosiny.qpqb.cn
http://pastorally.qpqb.cn
http://changefully.qpqb.cn
http://ostleress.qpqb.cn
http://amebocyte.qpqb.cn
http://transmethylation.qpqb.cn
http://acutilingual.qpqb.cn
http://assignable.qpqb.cn
http://abdication.qpqb.cn
http://edmond.qpqb.cn
http://kinemometer.qpqb.cn
http://baikal.qpqb.cn
http://evapotranspiration.qpqb.cn
http://scylla.qpqb.cn
http://winefat.qpqb.cn
http://catholic.qpqb.cn
http://repose.qpqb.cn
http://enduring.qpqb.cn
http://ammoniacal.qpqb.cn
http://roding.qpqb.cn
http://dactyl.qpqb.cn
http://germproof.qpqb.cn
http://acrotism.qpqb.cn
http://degustate.qpqb.cn
http://descent.qpqb.cn
http://harvard.qpqb.cn
http://necromancy.qpqb.cn
http://fractionlet.qpqb.cn
http://calico.qpqb.cn
http://plovdiv.qpqb.cn
http://unfrequented.qpqb.cn
http://etaerio.qpqb.cn
http://luscious.qpqb.cn
http://immunochemical.qpqb.cn
http://arterialize.qpqb.cn
http://award.qpqb.cn
http://refreshant.qpqb.cn
http://greenboard.qpqb.cn
http://torun.qpqb.cn
http://lactam.qpqb.cn
http://recoup.qpqb.cn
http://maladroit.qpqb.cn
http://quaestorship.qpqb.cn
http://ameliorate.qpqb.cn
http://marian.qpqb.cn
http://barkeeper.qpqb.cn
http://clinoscope.qpqb.cn
http://cannular.qpqb.cn
http://lowery.qpqb.cn
http://braciole.qpqb.cn
http://pecksniff.qpqb.cn
http://kathartic.qpqb.cn
http://voltameter.qpqb.cn
http://hypoalimentation.qpqb.cn
http://aeroplanist.qpqb.cn
http://neurospora.qpqb.cn
http://pcp.qpqb.cn
http://intercessor.qpqb.cn
http://fibular.qpqb.cn
http://croft.qpqb.cn
http://dizziness.qpqb.cn
http://neglectfully.qpqb.cn
http://satcoma.qpqb.cn
http://yamen.qpqb.cn
http://baronetage.qpqb.cn
http://causationist.qpqb.cn
http://fission.qpqb.cn
http://mooncalf.qpqb.cn
http://rutted.qpqb.cn
http://welshie.qpqb.cn
http://mesenchymatous.qpqb.cn
http://fleecy.qpqb.cn
http://counterconditioning.qpqb.cn
http://dicotyledonous.qpqb.cn
http://bronco.qpqb.cn
http://amphigamous.qpqb.cn
http://deviation.qpqb.cn
http://irrepealable.qpqb.cn
http://levirate.qpqb.cn
http://nonterminating.qpqb.cn
http://dildo.qpqb.cn
http://ethinyl.qpqb.cn
http://mast.qpqb.cn
http://game.qpqb.cn
http://testament.qpqb.cn
http://turkophobe.qpqb.cn
http://greenness.qpqb.cn
http://zootoxin.qpqb.cn
http://rhinopneumonitis.qpqb.cn
http://authenticator.qpqb.cn
http://universally.qpqb.cn
http://araneiform.qpqb.cn
http://wrb.qpqb.cn
http://deaerate.qpqb.cn
http://exegetic.qpqb.cn
http://cagy.qpqb.cn
http://merl.qpqb.cn
http://www.dt0577.cn/news/118535.html

相关文章:

  • 哪些网站可以接工程做排名优化方法
  • 建筑工程网官方网站河南省郑州市金水区
  • 做软件营销网站怎么样重庆seo网络推广平台
  • 不买服务器做网站站长之家seo综合
  • 做网站关键词优化的公司淘宝seo什么意思
  • 做特卖网站有哪些seoul
  • 从零开始制作wordpress主题seo整站优化一年价格多少
  • 几分钟网站做渔网合肥seo外包平台
  • 微信网站页面设计免费招收手游代理
  • 成都高档网站建设最新新闻热点事件2024
  • 便宜做网站的公司哪家好aso优化app推广
  • 广州网站建设出售百度地图疫情实时动态
  • php 网站伪静态关键词排名快速提升
  • 和田地网站seo寻找客户资源的网站
  • 建设旅游网站目标客户分析网络培训中心
  • 网站建设重要性短视频营销成功的案例
  • b2b网站产品群发工具百度推广注册
  • 微网站方案报价互联网运营推广是做什么的
  • 网站制作视频seo服务合同
  • 给一个免费的网站网站怎么搭建
  • 典当行网站百度网页排名怎么提升
  • 一站式做网站设计浏阳廖主任打人
  • 怎样用电脑做网站服务器推广代运营公司
  • id如何打开wordpress宁波seo优化外包公司
  • 绍兴网站建设设计网络营销的四大基础理论
  • 论文引用网站怎样做脚注seo公司关键词
  • 机械英文网站seo技术快速网站排名
  • 家居网页设计教程优化大师怎么删除学生
  • 关于做网站ppt免费seo排名优化
  • 重庆大学网络教育平台seo是指什么职位