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

相关推荐
快乐非自愿10 分钟前
Nginx负载配置
运维·nginx
u01090535931 分钟前
巴比达内网穿透:重塑企业级数据通信的高效与安全边界
服务器·网络·安全
henan程序媛41 分钟前
LVS+Keepalived群集
linux·服务器·lvs·keepalived·双机热备份
会讲英语的码农1 小时前
【计算机网络】第一章 概要
运维·服务器·网络协议
故事讲予风听1 小时前
iptables与firewalld
linux·服务器·网络·网络安全
掘根1 小时前
【Linux】touch
java·linux·服务器
podoor2 小时前
在WordPress中获取10天之内的文章更新数
运维·wordpress
九河云2 小时前
AWS云服务器的竞争优势
服务器·云计算·aws
文盲老顾2 小时前
阿里云 ECS 服务器的安全组设置
服务器·安全·阿里云
2301_784912692 小时前
服务器被劫持
服务器