赞
踩
节点 | 角色说明 |
---|---|
Provider | 暴露服务的服务提供方 |
Consumer | 调用远程服务的消费方 |
Registry | 服务注册与发现的注册中心 |
Monitor | 统计服务的调用次数和调用时间的监控中心 |
Container | 服务运行容器 |
新建一个父级springboot 项目
新建两个子集springboot项目 (一个消费者、一个服务提供者)
在pom 文件中添加 maven 依赖包(消费者和服务提供者都需要)
</dependencies>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
先写服务提供服务、后写消费者
修改服务提供的yaml 文件,
## 服务提供的
server:
# 服务端口
port: 8089
spring:
application:
name: dubbo-api ## dubbo 服务名称
dubbo:
server: true ## 表示为dubbo 服务
registry: N/A # 没有注册中心
## 消费者
server:
# 服务端口
port: 8088
spring:
application:
name: dubbo-comsumer ## dubbo 服务名称application:
dubbo:
registry: N/A # 没有注册中心
package com.wootop.web.service;
/**
* TODO
* 服务提供者的接口代码
* @author AZHE_SUN
* @date 2021-4-27 11:44
**/
public interface ServiceMqtt {
String getMessage(String s);
}
package com.wootop.web.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.wootop.web.service.ServiceMqtt; import org.springframework.stereotype.Component; /** * TODO * 服务提供者的接口实现 * @author AZHE_SUN * @date 2021-4-27 11:45 **/ @Component @Service(path="com.wootop.web.service.ServiceMqtt",interfaceClass = ServiceMqtt.class,group = "test") public class ServiceMqttImpl implements ServiceMqtt { @Override public String getMessage(String msg) { return "provider-message:"+msg; } }
服务提供者启动类
package com.wootop; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 启动程序 * * */ @SpringBootApplication @EnableDubboConfiguration # dubbo启动类 public class WooTopApplication { public static void main(String[] args) { // System.setProperty("spring.devtools.restart.enabled", "true"); SpringApplication.run(WooTopApplication.class, args); System.out.println("*** 系统启动成功 ***\n" + " ___ __ ________ \n" + " __ | / /______________ __/____________ \n" + " __ | /| / /_ __ \\ __ \\_ / _ __ \\__ __ \\ \n" + " __ |/ |/ / / /_/ / /_/ / / / /_/ /_ /_/ / \n" + " ____/|__/ \\____/\\____//_/ \\____/_ .___/ \n" + " /_/ \n" + " \n" ); } }
package com.wootop.web.service;
/**
* TODO
* 消费者接口调用
* @author AZHE_SUN
* @date 2021-4-27 13:38
**/
public interface ServiceMqtt {
String getMessage(String s);
}
package com.wootop.web.service; import com.alibaba.dubbo.config.annotation.Reference; import org.springframework.stereotype.Component; /** * TODO * 消费者的服务实现 * @author AZHE_SUN * @date 2021-4-27 13:36 **/ @Component public class ConsumerImpl { /*通过url 找到服务提供者 ,并且分组 */ @Reference(url = "dubbo://localhost:20880",group = "test") ServiceMqtt serviceAPI; public String getMessage(String msg){ // 调用对应服务方法 return serviceAPI.getMessage(msg); } }
package com.wootop; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import com.wootop.web.service.ConsumerImpl; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.context.ConfigurableApplicationContext; /** * 启动程序 * * @author ruoyi */ @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @EnableDubboConfiguration public class WooTopApplication { public static void main(String[] args) { /* 通过bean 对象直接调用 服务 这个包名、方法名 和服务对应的包名和方法名对应*/ ConfigurableApplicationContext run = SpringApplication.run(WooTopApplication.class, args); ConsumerImpl consumer=(ConsumerImpl)run.getBean("consumerImpl"); String str = consumer.getMessage("我是consumer端"); System.out.println(str); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。