springboot集成dubbo-spring-boot-starter 实现dubbo RPC远程调用

dubbo-spring-boot-starter   Git源码地址: https://github.com/alibaba/dubbo-spring-boot-starter.git

创建一个SpringBoot Maven 项目(blog),且包含多个子项目(blog-api,blog-service,blog-web),在pom 文件中进行相关依赖:

blog父项目pom.xml

    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">
    4.0.0
    com.zzg
    blog
    0.0.1-SNAPSHOT
    pom
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.6.RELEASE
    

    
        blog-api
        blog-service
        blog-web
    

    
    
        UTF-8
        UTF-8
        1.8
        0.1.0
        1.5.8.RELEASE
        1.2.31
        0.2
        1.4.3
   

    
   
       
           
                com.alibaba.boot
                dubbo-spring-boot-starter
                ${dubbo-spring-boot-starter.version}
           

           
                org.springframework.boot
                spring-boot-starter-logging
                ${springboot.version}
           

           
                com.101tec
                zkclient
                ${zk-client.version}
           

       
   

创建一个子项目blog-api:注意用于提供接口。(pom.xml 和 相关代码如下)


  4.0.0
 
    com.zzg
    blog
    0.0.1-SNAPSHOT
 

  blog-api

接口定义如下:

package com.springboot.blog.api;

public interface RpcService {
    public String rpc(String parame);
}
 

创建一个子项目blog-service:服务提供者。(注意涉及:dubbo和springboot的集成配置)

pom.xml

    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">
    4.0.0
    
        com.zzg
        blog
        0.0.1-SNAPSHOT
    

    blog-service
    
        
            org.springframework.boot
            spring-boot-starter-web
       

       
            org.springframework.boot
            spring-boot-actuator
       

       
            org.springframework.boot
            spring-boot-starter-logging
       

       
            org.springframework.boot
            spring-boot-starter-test
       

       
            com.alibaba.boot
            dubbo-spring-boot-starter
           
               
                    zookeeper
                    org.apache.zookeeper
               

           

       

       
            com.101tec
            zkclient
       

       
            org.projectlombok
            lombok
       

        
       
            com.zzg
            blog-api
            0.0.1-SNAPSHOT
        

    

application.properties (dubbo配置文件)

   spring.application.name=blog-service
   server.port=9090
   #dubbo\u914D\u7F6E
   dubbo.application.id=blog-service
   dubbo.application.name=blog-service
   #\u534F\u8BAE\u914D\u7F6E
   dubbo.protocol.id=dubbo
   dubbo.protocol.name=dubbo
   #\u628A\u9ED8\u8BA4\u768420880\u7AEF\u53E3\u6362\u621012345
   dubbo.protocol.port=12345
   #\u670D\u52A1\u6CE8\u518C\u914D\u7F6E
   dubbo.registry.id=my-registry
   dubbo.registry.address=zookeeper://localhost:2181
   #\u914D\u7F6Edubbo\u7684\u5305\u626B\u63CF\uFF0C\u9488\u5BF9dubbo\u7684@Service, @Reference\u6CE8\u89E3
   dubbo.scan.base-packages=com.springboot.blog.support
   #dubbo\u5065\u5EB7\u76D1\u63A7
   endpoints.dubbo.enabled=true
   management.health.dubbo.status.defaults=memory
   management.health.dubbo.status.extras=load,threadpool
   management.port=9091

服务实现类和项目入口

package com.springboot.blog.support;

import com.alibaba.dubbo.config.annotation.Service;
import com.springboot.blog.api.RpcService;

@Service(version = "1.0.0", application = "${dubbo.application.id}", protocol = "${dubbo.protocol.id}", registry = "${dubbo.registry.id}")
public class RpcServiceImpl implements RpcService {

    @Override
    public String rpc(String parame) {
        // TODO Auto-generated method stub
        return "this params is :" + parame;
    }

}
 

package com.springboot.blog;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootService {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringApplication.run(SpringBootService.class, args);
        System.out.println("============= SpringBoot Service Start Success =============");
    }

}
 

创建一个子项目blog-web:服务消费者。(注意涉及:dubbo和springboot的集成配置)

pom.xml

    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">
    4.0.0
    
        com.zzg
        blog
        0.0.1-SNAPSHOT
    

    blog-web
    
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-actuator
        

        
            org.springframework.boot
            spring-boot-starter-test
        

        
            org.springframework.boot
            spring-boot-starter-logging
        

        
            com.alibaba.boot
            dubbo-spring-boot-starter
        

        
            com.101tec
            zkclient
        

        
            org.projectlombok
            lombok
        

        
            com.zzg
            blog-api
            0.0.1-SNAPSHOT
        

    

application.properties(dubbo配置文件)

spring.application.name=blog-web
server.port=8081
#dubbo\u914D\u7F6E
dubbo.application.id=blog-web
dubbo.application.name=blog-web
#\u670D\u52A1\u6CE8\u518C\u914D\u7F6E
dubbo.registry.id=my-registry
dubbo.registry.address=zookeeper://localhost:2181
management.port=8082

远程服务消费者和SpringBoot入口

package com.springboot.blog.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.springboot.blog.api.RpcService;

@RestController
@RequestMapping("/dubbo")
public class RpcController {
    @Reference(version = "1.0.0")
    private RpcService service;
    
    @RequestMapping("/rpc")
    public void rpc() {      
        System.out.println(service.rpc("rpc 远程调用"));
    }
}
 

package com.springboot.blog.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootWeb {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringApplication.run(SpringBootWeb.class, args);
        System.out.println("============= SpringBoot Web Start Success =============");
    }

}
 

 

 

 

你可能感兴趣的:(深蓝计划,微服务springboot)