springboot2.3.0集成apache dubbo2.7.7發佈rest,dubbo服務

本文將介紹使用springboot2.3.0集成apache dubbo2.7.7發佈rest,dubbo服務,具體可參見apache dubbo官方示例,本文相關內容已在本地測試無誤,如有問題歡迎留言拍磚。
pom依賴:

<properties>
    <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
    <dubbo.version>2.7.7</dubbo.version>
</properties>
<dependencyManagement>
    <dependencies>
        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- Apache Dubbo  -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-bom</artifactId>
            <version>${dubbo.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>springboot-dubbo</groupId>
        <artifactId>api</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>compile</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</artifactId>
        <version>${dubbo.version}</version>
        <exclusions>
            <exclusion>
                <artifactId>slf4j-log4j12</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>${dubbo.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-rpc-rest</artifactId>
        <version>${dubbo.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>8.5.31</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-annotations-api</artifactId>
        <version>8.5.31</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
</dependencies>

屬性文件application.yaml

spring:
  application:
    name: provider
dubbo:
  application:
    name: privider
  registry:
    address: zookeeper://127.0.0.1:2181
    timeout: 20000
  protocols:
    rest:
      name: rest
      server: tomcat
      port: 8080
    dubbo:
      name: dubbo
      port: 20880
debug: true

接口api:

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("hello")
@Consumes({MediaType.APPLICATION_JSON,MediaType.TEXT_XML})
@Produces({"application/json;charset=UTF-8"})
public interface IHelloService {

    @GET
    @Path("say")
    String hello(@QueryParam("name") String name);
}

服務provider:

import com.star.api.IHelloService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService(interfaceClass = IHelloService.class,protocol = {"rest","dubbo"},version = "1.0")
public class HelloServiceImpl implements IHelloService {
    @Override
    public String hello(String name) {
        return "hello " + name;
    }
}

啓動類:

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo(scanBasePackages = {"com.star.provider.service"})
public class ProviderApp {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApp.class, args);
    }
}

測試:

public static void main(String[] args) {
        ReferenceConfig<IHelloService> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setApplication(new ApplicationConfig("dubbo-demo-consumer"));
        RegistryConfig registryConfig = new RegistryConfig("zookeeper://127.0.0.1:2181");
        registryConfig.setTimeout(10000);
        referenceConfig.setRegistry(registryConfig);
        referenceConfig.setInterface(IHelloService.class);
        referenceConfig.setTimeout(10000);
        referenceConfig.setVersion("1.0");
        //referenceConfig.setProtocol("rest");
        referenceConfig.setProtocol("dubbo");
        IHelloService helloService = referenceConfig.get();
        System.out.println(helloService.hello("dubbo latest"));
    }

完成,覺得有用,點個關注,【碼農小麥】公衆號同

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章