依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.4.0</version> <!-- 请根据最新版本进行调整 -->
</dependency>
暴露接口
@DubboService
package com.example.provider;
import com.example.api.HelloService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
}
调用服务
@DubboReference
package com.example.consumer;
import com.example.api.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
@Component
public class HelloClient {
@DubboReference
private HelloService helloService;
public void greet(String name) {
String message = helloService.sayHello(name);
System.out.println(message);
}
}
序列化
implements Serializable
package com.example.api;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
}
provider.yml
server:
port: 18081
spring:
datasource:
url: jdbc:mysql://localhost:3306/dubbo-demo?useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
application:
name: user-provider
logging:
level:
cn.itcast: debug
pattern:
dateformat: HH:mm:ss:SSS
dubbo:
protocol:
name: dubbo
port: 20881
registry:
address: nacos://127.0.0.1:8848
scan:
base-packages: cn.itcast.user.service
consumer.yml
server:
port: 18080
spring:
application:
name: user-consumer
logging:
level:
cn.itcast: debug
pattern:
dateformat: HH:mm:ss:SSS
dubbo:
registry:
address: nacos://127.0.0.1:8848
最佳实践
将公共接口抽取成一个模块
消费者和服务提供者在maven坐标中引入这个模块
所以就不需要定义service的实现类了
启动检查
dubbo:
registry:
address: nacos://127.0.0.1:8848
consumer:
check: false
多版本
provider指定版本
@DubboService(version = “2.0.0”)
public class UserServiceImpl2 implements UserService {
…………
}
消费者调用指定版本
@RestController
@RequestMapping("/user")
public class UserController {
//引用远程服务
@DubboReference(version = "2.0.0")
private UserService userService;
………
}
整合SpringCLoudAlibaba
依赖
<!-- Nacos Discovery 用于服务发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--springcloud alibaba dubbo依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
服务提供者接口暴露
@DubboService
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public User queryById(Long id) {
return userMapper.findById(id);
}
}
服务消费者远程调用
@DubboReference
private UserService userService;
provider.yml
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://192.168.80.40:3306/dubbo-demo?useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
cloud:
nacos:
discovery:
server-addr: localhost:8848
application:
name: user-service
logging:
level:
cn.itcast: debug
pattern:
dateformat: HH:mm:ss:SSS
dubbo:
protocol:
name: dubbo
port: 20881
registry:
address: spring-cloud://localhost #使用springCloud注册中心的地址
scan:
base-packages: cn.itcast.user.service
Consumer.yml
server:
port: 8082
spring:
datasource:
url: jdbc:mysql://192.168.80.40:3306/dubbo-demo?useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
cloud:
nacos:
discovery:
server-addr: localhost:8848
application:
name: order-service
dubbo:
registry:
address: spring-cloud://localhost #使用springCloud注册中心的地址
consumer:
check: false
retries: 0
logging:
pattern:
dateformat: HH:mm:ss:SSS
level:
cn.itcast: debug