• Dubbo常用功能03rest协议


    1、服务生产者的pom文件中修改如下:

    <?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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.5.6</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.yas</groupId>
        <artifactId>serviceprovider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>serviceprovider</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.example</groupId>
                <artifactId>Interface</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.3</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-registry-zookeeper</artifactId>
                <version>2.7.3</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>javax.ws.rs</groupId>
                <artifactId>javax.ws.rs-api</artifactId>
                <version>2.1</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jaxrs</artifactId>
                <version>3.6.0.Final</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-client</artifactId>
                <version>3.6.0.Final</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jaxb-provider</artifactId>
                <version>3.6.0.Final</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jackson-provider</artifactId>
                <version>3.6.0.Final</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-multipart-provider</artifactId>
                <version>3.6.0.Final</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>jaxrs-api</artifactId>
                <version>3.0.10.Final</version>
            </dependency>
            <dependency>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>7.4.4.v20110707</version>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <excludes>
                            <exclude>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                            </exclude>
                        </excludes>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.mortbay.jetty</groupId>
                    <artifactId>jetty-maven-plugin</artifactId>
                    <version>7.4.4.v20110707</version>
                    <configuration>
                        <scanIntervalSeconds>5</scanIntervalSeconds>
                        <connectors>
                            <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                                <port>80</port>
                            </connector>
                        </connectors>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    2、服务生产者配置修改application.yml:

    server:
      port: 8001
    dubbo:
      application:
        name: site-service-boot-provider
      registry:
        address: zookeeper://ubu:2181
      scan:
        base-packages: com.yas.serviceprovider
    #  #指定某一种协议
    #  protocol:
    #    name: dubbo
    #    port: 20882
      protocol:
        name: rest
        port: 8787

    3、服务生产者提供rest服务--接受POST请求:

     1 package com.yas.serviceprovider.protocol;
     2 
     3 import com.yas.api.SiteService;
     4 import org.apache.dubbo.config.annotation.Service;
     5 import org.apache.dubbo.rpc.protocol.rest.support.ContentType;
     6 
     7 import javax.ws.rs.GET;
     8 import javax.ws.rs.POST;
     9 import javax.ws.rs.Path;
    10 import javax.ws.rs.Produces;
    11 
    12 @Service(version = "rest")
    13 @Path("site")
    14 public class RestImpl implements SiteService {
    15     @Override
    16     @POST
    17     @Path("name")
    18     @Produces({ContentType.APPLICATION_JSON_UTF_8,ContentType.TEXT_PLAIN_UTF_8})
    19     public String getName(String name) {
    20         System.out.println("/site/name接收到请求:"+name);
    21         return "rest:" + name;
    22     }
    23 }

    4、测试POST请求:

    使用postman请求地址:http://localhost:8787/site/name

    请求方式为:post,请求参数为:body中的 name=zhangsan

    由图可见,得到响应为:rest:name=zhangsan


    5、服务生产者提供rest服务--接受GET请求:

     1 package com.yas.serviceprovider.protocol;
     2 
     3 import com.yas.api.SiteService;
     4 import org.apache.dubbo.config.annotation.Service;
     5 import org.apache.dubbo.rpc.protocol.rest.support.ContentType;
     6 
     7 import javax.ws.rs.*;
     8 
     9 @Service(version = "rest")
    10 @Path("site")
    11 public class RestImpl implements SiteService {
    12     @Override
    13     @GET
    14     @Path("name")
    15     @Produces({ContentType.APPLICATION_JSON_UTF_8,ContentType.TEXT_PLAIN_UTF_8})
    16     public String getName(@QueryParam("name") String name) {
    17         System.out.println("/site/name接收到请求:"+name);
    18         return "rest:" + name;
    19     }
    20 }

    6、测试GET请求:

    使用postman请求地址:http://localhost:8787/site/name?name=zhangsan

    请求方式为:get,请求参数为:name=zhangsan

     由图可见,得到响应为:rest:zhangsan

  • 相关阅读:
    Python 单向循环链表
    python 单向链表实现
    Python实现栈
    单进程-非阻塞服务器
    Nginx开发从入门到精通
    Spring入门第十五课
    Spring入门第十六课
    Spring入门第十四课
    Spring入门第十三课
    Spring入门第十二课
  • 原文地址:https://www.cnblogs.com/asenyang/p/15506951.html
Copyright © 2020-2023  润新知