最后
文章中涉及到的知识点我都已经整理成了资料,录制了视频供大家下载学习,诚意满满,希望可以帮助在这个行业发展的朋友,在论坛博客等地方少花些时间找资料,把有限的时间,真正花在学习上,所以我把这些资料,分享出来。相信对于已经工作和遇到技术瓶颈的朋友们,在这份资料中一定都有你需要的内容。
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
dubbopractice
com.bolingcavalry
1.0-SNAPSHOT
4.0.0
com.bolingcavalry
springbootmulticastprovider
1.0-SNAPSHOT
springbootmulticastprovider
Demo project for dubbo service provider from Spring Boot, multicast mode
org.springframework.boot
spring-boot-dependencies
${springboot.version}
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
com.bolingcavalry
practiceinterface
${project.version}
org.projectlombok
lombok
org.apache.dubbo
dubbo-spring-boot-starter
org.springframework.boot
spring-boot-maven-plugin
${springboot.version}
- 配置文件application.yml,要注意的是registry.address的配置是广播模式:
dubbo:
application:
#application-name 本模块名字
name: springboot-multicast-provider
id: springboot-multicast-provider
registry:
address: multicast://224.5.6.7:1234
id: registry
protocol:
name: dubbo
port: 20880
- 编写服务实现类DemoServiceImpl.java,注意@Service注解将当前类的实例作为远程服务对外暴露:
package com.bolingcavalry.springbootmulticastprovider;
import com.bolingcavalry.dubbopractice.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.RpcContext;
@Slf4j
@Service
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
log.info("I’m springboot-multicast-provider, Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "I’m springboot-multicast-provider, Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
}
- 编写SpringBoot启动类SpringBootMulticastProviderApplication.java,注意要添加@EnableDubbo注解:
package com.bolingcavalry.springbootmulticastprovider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class SpringBootMulticastProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMulticastProviderApplication.class, args);
}
}
- 至此服务提供方编码完成,直接在IDEA上运行SpringBootMulticastProviderApplication类即可启动服务,启动成功后的日志输出如下图:
编码(服务消费方)
- 现在网络上已经有了服务,咱们再来编写服用消费方的代码,一共要创建6个文件,创建顺序和功能如下表:
| 创建顺序 | 文件名 | 作用 |
| — | — | — |
| 1 | pom.xml | 工程的pom文件 |
| 2 | src/main/resources/application.yml | 配置文件 |
| 3 | RemoteInvokeServiceImpl.java | service层,在这里远程调用服务提供方的服务 |
| 4 | DemoController.java | web接口类,对外提供web服务 |
| 5 | SwaggerConfig.java | swagger配置类,便于通过页面测试接口 |
| 6 | SpringBootMulticastConsumerApplication.java | 启动类 |
- 完整的文件位置如下图:
- 接下来逐个创建上述文件;
- 创建名为springbootmulticastconsumer的子工程,pom.xml内容如下,同样需要依赖dubbo-spring-boot-starter:
<project xmlns=“http://maven.apache.org/POM/4.0.0”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
dubbopractice
com.bolingcavalry
1.0-SNAPSHOT
4.0.0
com.bolingcavalry
springbootmulticastconsumer
1.0-SNAPSHOT
springbootmulticastconsumer
Demo project for dubbo service consumer from Spring Boot, multicast mode
org.springframework.boot
spring-boot-dependencies
${springboot.version}
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
io.springfox
springfox-swagger2
io.springfox
springfox-swagger-ui
com.bolingcavalry
practiceinterface
${project.version}
org.projectlombok
lombok
org.apache.dubbo
dubbo-spring-boot-starter
org.springframework.boot
spring-boot-maven-plugin
${springboot.version}
- 编写配置文件application.yml,注意dubbo.registry.address的值,除了是广播模式,还要添加unicast=false,这样才能保证多个消费者进程都能收到广播:
dubbo:
application:
name: springboot-multicast-consumer
id: springboot-multicast-consumer
qosEnable: false
registry:
address: multicast://224.5.6.7:1234?unicast=false
id: registry
protocol:
name: dubbo
port: 20880
server:
port: 8081
- 编写调用远程服务的代码,如下,可见如果想调用远程服务,只要对接口做@Reference注释即可,另外还通过timeout属性增加了超时配置:
package com.bolingcavalry.springbootmulticastconsumer.service;
import com.bolingcavalry.dubbopractice.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
@Service
public class RemoteInvokeServiceImpl {
@Reference(timeout = 2000)
private DemoService demoService;
public String sayHello(String name) {
return "from dubbo remote (multicast mode) : " + demoService.sayHello(name);
}
}
读者福利
由于篇幅过长,就不展示所有面试题了,感兴趣的小伙伴
更多笔记分享
(String name) {
return "from dubbo remote (multicast mode) : " + demoService.sayHello(name);
}
}
读者福利
由于篇幅过长,就不展示所有面试题了,感兴趣的小伙伴
[外链图片转存中…(img-mzWjfkPd-1715625938838)]
[外链图片转存中…(img-0mw4QZJY-1715625938839)]
[外链图片转存中…(img-eQuUQMTI-1715625938839)]
更多笔记分享
[外链图片转存中…(img-dOmnKHKS-1715625938839)]