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的服务端。

相关推荐
汤米粥1 分钟前
通过PHP创建AWS的CloudFront并绑定证书添加备用域名
服务器·云计算·aws
Aiden_SHU15 分钟前
Wireshark中的length栏位
服务器·网络·wireshark
清风fu杨柳25 分钟前
麒麟服务器工作站SP1 arm环境qt5.6.3源码编译
服务器·arm开发·qt
叫我龙翔1 小时前
【计网】实现reactor反应堆模型 --- 多线程方案优化 ,OTOL方案
linux·运维·网络
mit6.8241 小时前
[Docker#9] 存储卷 | Volume、Bind、Tmpfs | -v/mount | MySQL 灾难恢复 | 问题
linux·运维·docker·容器·架构
Z1eaf_complete1 小时前
Docker的基础使用
运维·docker·容器·云计算
群联云防护小杜1 小时前
服务器被挂马怎么办?——解决服务器被挂马的方法和步骤
运维·服务器·网络协议·tcp/ip·安全·ddos
明志致远淡泊宁静2 小时前
记录一次服务器redis被入侵
运维·服务器·redis
Jtti2 小时前
Jtti:服务器总是自动重启怎么办?
运维·服务器
我是黄大仙2 小时前
利用飞书多维表格自动发布版本
运维·服务器·数据库·飞书