赞
踩
Springboot 2.3.9
Dubbo :3.0.0
一、服务端:
Maven引入:
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!--不引人下面包时会抛异常 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.13.0</version>
</dependency>
Application.properties配置:
#dubbo服务名称
dubbo.application.name=boot-user-service-provider
#注册中心位置(N/A表示无注册中心)
dubbo.registry.address=N/A
#通信规则协议
dubbo.protocol.name=dubbo
#通信端口
dubbo.protocol.port=20880
#扫描位置
#dubbo.scan.base-packages=com.xuanfeng.code.service
定义接口:
public interface DubboDemoService {
public String sayHello(String param1);
}
接口实现类:
import org.apache.dubbo.config.annotation.DubboService;
//dubbo服务注解
@DubboService
public class DubboDemoServiceImpl implements DubboDemoService{
@Override
public String sayHello(String param1) {
System.out.println("aaaaaa----------------");
System.out.println(param1);
return "haha";
}
}
启动类代码:
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
//扫描dubbo服务
@EnableDubbo
@EnableConfigurationProperties(Configs.class)
public class Application {
public static void main(String[] args) {
try {
SpringApplication.run(Application.class, args);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
二、客户端
Maven引入:
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Application.properties配置:
dubbo.application.name=dubbo-consumer
#不验证服务是否存在
dubbo.consumer.check=false
定义接口:
public interface DubboDemoService {
public String sayHello(String param1);
}
注:接口路径必须与服务端的保持一致
接口调用(以下写个自动作业测试):
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class AutoJob{
//配置url可直连服务,绕过注册中心(旧版用@Reference)
@DubboReference(url = "dubbo://127.0.0.1:20880")
private DubboDemoService dubboDemoService;
@Scheduled(cron = "0/10 * * * * ?")
public void test() {
dubboDemoService.sayHello("哈啊哈ssssssss");
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。