赞
踩
案例 : 分作三个模块 1 dubbo-common, 2 dubbo-provider, 4 dubbo-consumer
<!--
持有 dubbo 依赖,
目的是为了方便管理, 之后provider和consumer只需要持有这个模块的依赖, 就不需要各自添加dubbo依赖
-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.8</version>
</dependency>
package com;
public interface DosomeService {
Object dosome(String str);
}
<dependency>
<groupId>org.example</groupId>
<artifactId>dubbo-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
server:
port: 8081 #Tomcat端口, 和provider模块当然不能重复
# 和项目名称相同
dubbo.application.name=dubbo-Serv
# 查阅官方文档可知, registry.address 属性是必须的, 即使不使用注册中心, 也得有这个属性, 随便给一个;
dubbo.registry.address=N/A
# 设置 registry.register=false, 消除 registry.address 的作用;
dubbo.registry.register=false
# 固定的
dubbo.protocol.name=dubbo
# 暴露服务的端口
dubbo.protocol.port=20880
package com.example.dubboserv.service.impl; import com.DosomeService; import org.apache.dubbo.config.annotation.DubboService; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Component @DubboService (version = "1.0.0", timeout = 3000) public class DosomeServiceImpl implements DosomeService { @Override public Object dosome(String str) { System.out.println("服务被调用"); List<String> x = new ArrayList<>(); x.add("a"); x.add("b"); x.add("c"); return x; } }
// 表明这是一个配置类
@Configuration
// 开启dubbo, 作为服务端, 在括号内指定服务的包
@EnableDubbo(scanBasePackages = "com.example.dubboserv.service.impl")
// 扫描dubbo配置文件
@PropertySource("classpath:/dubbo-provider.properties")
public class DubboServConfig {
// 啥也不用干
}
@SpringBootApplication
public class DubboServApplication {
public static void main(String[] args) {
SpringApplication.run(DubboServApplication.class, args);
}
}
<dependency>
<groupId>org.example</groupId>
<artifactId>dubbo-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
server:
port: 8081 #Tomcat端口, 和provider模块当然不能重复
# 和项目名称相同
dubbo.application.name=dubbo-consu
# 固定的
dubbo.protocol.name=dubbo
# 暴露服务的端口
dubbo.protocol.port=20880
# 启动时不检查是否有服务, 默认是true
dubbo.consumer.check=false
// 表明这是一个配置类
@Configuration
// 开启dubbo
@EnableDubbo
// 扫描dubbo配置文件
@PropertySource("classpath:/dubbo-consumer.properties")
public class DubboServConfig {
// 啥也不用干
}
@EnableScheduling
@SpringBootApplication
public class DubboConsuApplication {
public static void main(String[] args) {
SpringApplication.run(DubboServApplication.class, args);
}
}
@Component public class Test { // 通过 url 属性指定 ip 和 port, 直接访问服务提供者 @DubboReference( id="dosomeService", interfaceName = "com.DosomeService", version = "1.0.0", url="dubbo://10.41.25.2:20880") private DosomeService dosomeService; @Scheduled(cron = "0/5 * * * * ?") public void m() { Object ans = dosomeService.dosome("发起调用"); System.out.println(ans); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。