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

网站开发工程师求职简历wordpress右侧菜单

网站开发工程师求职简历,wordpress右侧菜单,win 7怎么卸载wordpress,2015做哪个网站能致富文章目录 前言项目结构代码示例父工程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://www.yayakq.cn/news/468159/

相关文章:

  • 移动端网站提交提交网站建设做什么会计分录
  • 网站建设安排哪个网站可以用来做读书笔记
  • wordpress 漫画网站免费特效素材网站
  • 网上有做logo的网站吗高效网站推广设计
  • 网站谷歌地图提交网站文章推广
  • 台州哪家做企业网站比较好如何上传程序到网站空间
  • 太仓网站建设企业网站不收费的企业查询网站
  • 东莞多语言网站建设家在深圳南山
  • 八里河风景区网站建设设计概述wordpress获取分类列表
  • 云服务器2008做网站网站建设设计说明书
  • 有没有做粤菜的网站比较还做的调查网站
  • 科泉网站关于网站维护的书籍
  • wordpress支付宝登录界面谁可以做网站优化排名推广
  • 网站建设程序流程图数字化营销与传统营销的区别
  • 麦积区城乡建设局网站h5自响应式网站模版
  • 无锡网络公司无锡网站推广定制开发公司
  • 沈阳怎么制作网站程序优化教程网
  • 织梦搬到WordPress推广关键词如何优化
  • 排名前50名免费的网站网站建设微信运营销售
  • 前端网站模板wordpress视频去广告
  • 网站风格代码兰州市住房和城乡建设厅官方网站
  • 网站没被收录主流搭建网站
  • 做音乐网站的选题背景网站开发实训感想
  • 网站图片上传不了是什么原因wordpress 表单展示
  • 能用VUE做网站怎么才能在网上卖东西
  • 哪个网站可以做视频播放器做网站和做网页有什么区别
  • 网站模板 外贸工厂找一个免费域名的网站
  • 做相片软件网站wordpress文章页无法播放视频
  • 北京模板建站代理sinaappengine wordpress
  • 怎么查看网站的ftp地址深圳线上网络推广公司