1.创建一个maven空项目
该项目包含api、consumer、provider三个子模块,均为springboot模块。
2.在api模块创建服务接口
package com.cz.czapi.service;
public interface OrderService {
Integer add(int cost);
}
该服务模拟提供订单服务。
3.在提供者服务里面实现服务。
(1)首先引入api模块以及dubbo
<!--引入api-->
<dependency>
<groupId>com.cz</groupId>
<artifactId>cz-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!--引入dubbo环境-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
(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
<!--引入api模块-->
<dependency>
<groupId>com.cz</groupId>
<artifactId>cz-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!--引入dubbo环境-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
(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.效果
出现该效果表示成功调用。