温故知新02-springboot集成dubbo

1.创建一个maven空项目


maven空项目.png

该项目包含api、consumer、provider三个子模块,均为springboot模块。
2.在api模块创建服务接口

package com.cz.czapi.service;

public interface OrderService {
    Integer add(int cost);
}

该服务模拟提供订单服务。
3.在提供者服务里面实现服务。
(1)首先引入api模块以及dubbo


        
            com.cz
            cz-api
            0.0.1-SNAPSHOT
            compile
        
        
        
            com.alibaba.boot
            dubbo-spring-boot-starter
            0.2.0
        

(2)配置dubbo

dubbo:
  application:
    name: dubbo-provider
  registry:
    address: 192.168.100.250:2181
    protocol: zookeeper
    check: false
  protocol:
    name: dubbo
    port: 30003
  monitor:
    protocol: register
  consumer:
    check: false
    timeout: 3000

server:
  port: 8061

dubbo.registry.address修改为自己的zk地址
(3)启动类启用dubbo(@EnableDubbo注解类)

package com.cz.czprovider;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class CzProviderApplication {

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

}

(4)实现服务

package com.cz.czprovider.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cz.czapi.service.OrderService;

/**
 * @program: cz-parent
 * @description:
 * @author: shuonar
 * @create: 2020-12-23 17:16
 **/
@Service
public class OrderServiceImpl implements OrderService {

    private final Integer totalCost = 500;


    @Override
    public Integer add(int cost) {
        return totalCost + cost;
    }
}

注意:实现一定引入dubbo的Service注解,引入Spring的Service的将会导致服务提供者不存在

3.消费者准备
(1)引入dubbo和api


        
            com.cz
            cz-api
            0.0.1-SNAPSHOT
            compile
        

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

(2)配置yaml

dubbo:
  application:
    name:  dubbo-consumer
  registry:
    address: 192.168.100.250:2181
    protocol: zookeeper
    check: false
  monitor:
    protocol: register
  consumer:
    check:  false
    timeout: 3000

server:
  port: 8062

(3)消费服务

package com.cz.czconsumer.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.cz.czapi.service.OrderService;
import com.cz.czconsumer.service.WorkService;

/**
 * @program: cz-parent
 * @description:
 * @author: shuonar
 * @create: 2020-12-23 17:05
 **/
@Service
public class WorkServiceImpl implements WorkService {
    @Reference
    private OrderService orderService;
    //日志服务...
    //优惠券服务
    @Override
    public Integer getCost(int a) {
        Integer result = null;
        try {
            result = orderService.add(a);
        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }
}

package com.cz.czconsumer.service;

public interface WorkService {
Integer getCost(int a);
}


(4)controller类

package com.cz.czconsumer.controller;

import com.cz.czconsumer.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: cz-parent
 * @description:
 * @author: shuonar
 * @create: 2020-12-23 17:11
 **/
@RestController
public class WorkController {
    @Autowired
    private WorkService workService;


    @RequestMapping("/getCost")
    public String getCost(int a){
        return "该工单总共消费 :" + workService.getCost(a);
    }
}

4.效果


image.png

出现该效果表示成功调用。

源码地址:
https://gitee.com/qijiapo/cz-parent

你可能感兴趣的:(温故知新02-springboot集成dubbo)