dubbo复习:(11)使用grpc客户端访问tripple协议的dubbo 服务器

一、服务器端依赖:

复制代码
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>dubbotrippleserver</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <dubbo.version>3.1.8</dubbo.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
            <type>pom</type>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.19.4</version>
        </dependency>
    </dependencies>

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.1</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.19.4:exe:${os.detected.classifier}</protocArtifact>
                    <protocPlugins>
                        <protocPlugin>
                            <id>dubbo</id>
                            <groupId>org.apache.dubbo</groupId>
                            <artifactId>dubbo-compiler</artifactId>
                            <version>${dubbo.version}</version>
                            <mainClass>org.apache.dubbo.gen.tri.Dubbo3TripleGenerator</mainClass>
                        </protocPlugin>
                    </protocPlugins>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

二、服务器端hello.proto文件,位于src/main/proto

复制代码
syntax = "proto3";

option java_multiple_files = true;
option java_package = "cn.edu.tju.hello";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "TEST";

package helloworld;

message HelloRequest {
    string name = 1;
}

message HelloReply {
    string message = 1;
}
service Greeter{
    rpc greet(HelloRequest) returns (HelloReply);
}

三、maven compile生成接口定义文件(点击compile)

执行后生成文件

将其剪切到src/main/java(也可以配置maven直接生成到指定位置)

四、新建一个接口实现类:

复制代码
package cn.edu.tju.hello;



import cn.edu.tju.hello.DubboGreeterTriple;
import cn.edu.tju.hello.HelloReply;
import cn.edu.tju.hello.HelloRequest;

public class GreeterImpl extends DubboGreeterTriple.GreeterImplBase {
    @Override
    public HelloReply greet(HelloRequest request) {
        return HelloReply.newBuilder()
                .setMessage("[Dubbo] " + request.getName() + "!")
                .build();
    }
}

五、编写dubbo服务端主类,然后运行(其中注册中心的地址为zookeeper 3.6.2)

复制代码
package cn.edu.tju.test;


import cn.edu.tju.hello.Greeter;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.*;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;

import java.io.IOException;

public class MyDubboServer {

    public static void main(String[] args) throws IOException {
        ServiceConfig<Greeter> service = new ServiceConfig<>();
        service.setInterface(Greeter.class);
        service.setRef(new GreeterImpl());

        DubboBootstrap bootstrap = DubboBootstrap.getInstance();
        bootstrap.application(new ApplicationConfig("myServer"))
                .registry(new RegistryConfig("zookeeper://xx.xx.xx.xx:2181"))
                .protocol(new ProtocolConfig(CommonConstants.TRIPLE, 50092))
                .service(service)
                .start();
        System.out.println("Dubbo triple stub server started");
        System.in.read();
    }
}

六、grpc客户端依赖:

复制代码
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>grpcclient</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <grpc-version>1.54.0</grpc-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-core</artifactId>
            <version>${grpc-version}</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
            <version>${grpc-version}</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>${grpc-version}</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>${grpc-version}</version>
        </dependency>

        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.22.2</version>
        </dependency>
    </dependencies>


    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.7.1</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.21.7:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.54.0:exe:${os.detected.classifier}</pluginArtifact>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

七、客户端proto(scr/main/proto/hello.proto)和服务端的相同

复制代码
syntax = "proto3";

option java_multiple_files = true;
option java_package = "cn.edu.tju.hello";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "TEST";

package helloworld;

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}
service Greeter{
  rpc greet(HelloRequest) returns (HelloReply);
}

执行maven compile,生成代码

根据包名,将生成的代码剪切到scr/main/java下。

八、编写grpc客户端主类,来访问dubbo服务端

复制代码
package cn.edu.tju.hello;




import io.grpc.Channel;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;


public class MyHelloClient {
    private static final Logger logger = Logger.getLogger(MyHelloClient.class.getName());

    private final GreeterGrpc.GreeterBlockingStub blockingStub;

    public MyHelloClient(Channel channel) {
        blockingStub = GreeterGrpc.newBlockingStub(channel);
    }


    public void greet(String name) {
        logger.info("Will try to greet " + name +" ...");
        HelloRequest request = HelloRequest.newBuilder().setName(name).build();
        HelloReply response;
        try {
            response = blockingStub.greet(request);
        } catch (Exception ex) {
            logger.info(ex.getMessage());
            return;
        }
        logger.info("来自服务器的响应: " + response.getMessage());
    }


    public static void main(String[] args) throws Exception {
        String user = "【爱因斯坦】";
        String target = "localhost:50097";


        ManagedChannel channel = ManagedChannelBuilder.forTarget(target)
                .usePlaintext()
                .build();
        try {
            MyHelloClient client = new MyHelloClient(channel);
            client.greet(user);
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        finally {
            channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
        }
    }
}

九、客户端运行结果

总结:

dubbo3 服务端,在使用tripple协议时,底层可以使用grpc协议,因此其它的客户但可以通过grpc通信的方式来访问dubbo 3的服务端。

相关推荐
mklpo1477 分钟前
为不支持sse4.2命令集的老旧设备制作Win11 26H2系统镜像,2026-10-11。
运维·windows·python
碧海银沙音频科技研究院31 分钟前
炬芯ATS308X的扩音器:麦克风到扬声器直通功能实现
java·linux·运维
.Hypocritical.34 分钟前
SpringBoot+Vue 前后端分离项目部署到云服务器
服务器·vue.js·spring boot
吴声子夜歌1 小时前
Docker入门与实战——实战案例(编程开发)(一)
运维·docker·容器
空空dream1 小时前
04 | 用 Jenkins 打通 CI自动构建镜像并推送到本地 Harbor
运维·ci/cd·jenkins
海宇AI1 小时前
零信任架构实战:基于海宇运营商近3个月平均账单构建自动化免押租赁授信网关
运维·人工智能·架构·自动化
此生决int2 小时前
Linux系列(06)——进程概念(上)
linux·运维·服务器
海宇服务2 小时前
零信任架构实战:基于海宇股权穿透构建自动化招投标供应商关联排查网关
运维·人工智能·架构·自动化
房开民2 小时前
通俗总结 Octree(八叉树)
java·服务器·数据库
wdfk_prog2 小时前
LWIP教程 13:从 `dhcp_start()` 到 `DHCP_STATE_BOUND`——DHCPv4、ACD、Timer 与真实抓包
运维·网络协议