1、创建mave项目:

dubbo入门之springboot+dubbo_第1张图片

2、修改pom.xml文件:



    4.0.0

    com.ym
    springbootdubbo
    pom
    1.0-SNAPSHOT
    
        service
        serviceImpl
        testweb
    

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
    
    
        UTF-8
        1.8
        1.8
    
    
        
            junit
            junit
            3.8.2
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.alibaba.boot
            dubbo-spring-boot-starter
            0.1.0
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            ch.qos.logback
            logback-core
        
        
            ch.qos.logback
            logback-classic
        
        
            com.101tec
            zkclient
            0.10
        
        
            org.eclipse.jetty
            jetty-maven-plugin
            9.4.6.v20170531
        

    

    
        
            
                
                    maven-clean-plugin
                    3.0.0
                
                
                
                    maven-resources-plugin
                    3.0.2
                
                
                    maven-compiler-plugin
                    3.7.0
                
                
                    maven-surefire-plugin
                    2.20.1
                
                
                    maven-jar-plugin
                    3.0.2
                
                
                    maven-install-plugin
                    2.5.2
                
                
                    maven-deploy-plugin
                    2.8.2
                
            
        
    

3、创建远程接口定义模块:

dubbo入门之springboot+dubbo_第2张图片

4、创建接口:

dubbo入门之springboot+dubbo_第3张图片

5、创建接口实现模块:

dubbo入门之springboot+dubbo_第4张图片

6、springboot整合application.yml:

spring:
  application:
    name: dubbo-provider-app
server:
  port: 9090
dubbo:
  scan:
    base-packages: com.ym.service
  application:
    id: dubbo-provider
    name: dubbo-provider
  protocol:
    id: duboo
    name: dubbo
    port: 12345
    status: server #标明是一个server
  registry:
    id: my-reg
    address: zookeeper://192.168.1.224:2181
endpoint:
  dubbo:
    enabled: true
management:
  port: 9091
  health:
    dubbo:
      status:
        extras: load,threadpool
        defaults: memory

7、实现接口:

dubbo入门之springboot+dubbo_第5张图片

package com.ym.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ym.service.TestService;

/**
 * Created with IntelliJ IDEA.
 * User: Dony
 * Date: 2018/11/17
 * Time: 16:04
 * Description:
 */
//@Service此时不再使用这个注解
@Service(version = "1.0",application = "${dubbo.application.id}",protocol = "${dubbo.protocol.id}",registry = "${dubbo.registry.id}") //这个注解时dubbo提供的,其作用是创建此类型的对象,然后作为服务提供者发布
public class TestServiceImpl implements TestService {
    @Override
    public String getData(String name) {
        return "result=" + name;
    }
}

8、创建启动程序:

package ym;

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

/**
 * Created with IntelliJ IDEA.
 * User: Dony
 * Date: 2018/11/17
 * Time: 20:34
 * Description:
 */
@SpringBootApplication
public class StartSpringBootMain {
    public static void main(String[] args) {
        SpringApplication.run(StartSpringBootMain.class);
    }
}

观察dubbo控制台:

dubbo入门之springboot+dubbo_第6张图片

dubbo入门之springboot+dubbo_第7张图片


9、创建消费端:

dubbo入门之springboot+dubbo_第8张图片


10、整合springboot和dubbo(application.yml):

spring:
  application:
    name: dubbo-consumer-app
server:
  port: 8080
dubbo:
  application:
    id: dubbo-consumer
    name: dubbo-consumer
  protocol:
    id: duboo
    name: dubbo
    port: 54321
  registry:
    id: my-reg
    address: zookeeper://192.168.1.224:2181

11、创建controller程序,引用远程接口:

package com.ym.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ym.service.TestService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * User: Dony
 * Date: 2018/11/17
 * Time: 20:58
 * Description:
 */
@RestController
@RequestMapping("/test")
public class TestController {

    //@Autowired 不是使用这个注解,使用dubbo注解引用远程服务
    @Reference(version = "1.0", application = "${dubbo.application.id}")
    private TestService testService;

    @RequestMapping("/getdata/{name}")
    public String getData(@PathVariable("name") String name) {
        return testService.getData(name);
    }
}

12、创建启动程序,并启动项目:

package com.ym;

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

/**
 * Created with IntelliJ IDEA.
 * User: Dony
 * Date: 2018/11/17
 * Time: 21:14
 * Description:
 */
@SpringBootApplication(scanBasePackages = "com.ym")
public class StartSpringBootMain {
    public static void main(String[] args) {
        SpringApplication.run(StartSpringBootMain.class);
    }
}

13、观察dubbo控制台:

dubbo入门之springboot+dubbo_第9张图片

dubbo入门之springboot+dubbo_第10张图片