目录
一、依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
二、Server服务端
2.1 启动类
@EnableDubbo
开启基于注解的Dubbo功能
package com.lsh;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author :LiuShihao
* @date :Created in 2021/3/8 10:26 上午
* @desc :
*/
//开启基于注解的Dubbo功能
@EnableDubbo
@SpringBootApplication
public class DubboServerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboServerApplication.class);
}
}
2.2 yml配置文件
server:
port: 8081
spring:
application:
name: Dubbo_server
datasource:
url: jdbc:mysql://localhost:3306/day27?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
hikari:
connection-timeout: 60000
maximum-pool-size: 5
jpa:
show-sql: true
dubbo:
application:
#服务应用名称
name: provider-8081
protocol:
name: dubbo
#手动配置服务端口
port: 20880
#表明这是一个server
status: server
#扫描哪些包下的服务要被注册
scan:
base-packages: com.lsh.service
# 连接监控中心
monitor:
protocol: registry
# 2.6写法 ubbo.registry.address: zookeeper://192.168.103.10:2181 使用com.alibaba.boot的包也需要使用这个写法
# 2:7的配置 dubbo.config.center: 推荐用法,填写配置中心的地址,并且在配置中心的相应目录下配置注册中心和元数据中心的地址:
# config-center:
# address: zookeeper://192.168.103.10:2181
registry:
address: zookeeper://192.168.103.10:2181
2.3 Controller
@RestController
public class ProviderController {
@Autowired
UserService userService;
@GetMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
}
}
2.4 Server
public interface UserService {
List<User> findAll();
}
2.5 ServerImpl
package com.lsh.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.lsh.entity.User;
import com.lsh.repository.UserRepository;
import com.lsh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author :LiuShihao
* @date :Created in 2021/3/8 9:23 上午
* @desc :
* @Service注解需要使用com.alibaba.dubbo.config.annotation.Service;包下的注解
*
*/
//注意!这里的@Service注解是dubbo包下的,不是Spring包下的,千万不要导错,这里是将此服务放到dubbo服务运行容器
@Service
@Component("UserService")
public class UserServiceImpl implements UserService {
@Autowired
UserRepository userRepository;
@Override
public List<User> findAll() {
return userRepository.findAll();
}
}
三、Client调用端
3.1 启动类
启动类同上
3.2 yml配置文件
server:
port: 8082
spring:
application:
name: Dubbo_client
datasource:
url: jdbc:mysql://localhost:3306/day27?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
hikari:
connection-timeout: 60000
maximum-pool-size: 5
jpa:
show-sql: true
dubbo:
application:
#指定当前服务/应用的名字(不要和别的服务名字相同)
name: consumer-8082
#指定注册中心的地址
# registry:
# address: zookeeper://192.168.103.10:2181
protocol:
# 指定通信规则(通信协议、通信端口)
name: dubbo
port: 20880
# 包扫描
# 指定监控中心
monitor:
protocol: monitor
# config-center:
# address: zookeeper://192.168.103.10:2181
scan:
base-packages: com.lsh.service
registry:
address: zookeeper://192.168.103.10:2181
3.3 Controller
package com.lsh.controller;
import com.lsh.entity.User;
import com.lsh.service.ClientServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author :LiuShihao
* @date :Created in 2021/3/8 1:35 下午
* @desc :服务调用端
*/
@RestController
public class UserController {
@Autowired
ClientServer clientServer;
@GetMapping("/findAll")
public List<User> findAll(){
return clientServer.findAll();
}
}
3.4 Server
ClientServer
package com.lsh.service;
import com.lsh.entity.User;
import java.util.List;
/**
* @author :LiuShihao
* @date :Created in 2021/3/8 1:51 下午
* @desc :
*/
public interface ClientServer {
List<User> findAll();
}
UserServer
package com.lsh.service;
import com.lsh.entity.User;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author :LiuShihao
* @date :Created in 2021/3/8 9:20 上午
* @desc : 服务端像消费端提供接口,因此我们需要编写与服务端包名,接口名完全相同的接口
*/
@Component("UserService")
public interface UserService {
List<User> findAll();
}
3.5 ServerImpl
package com.lsh.service.impl;
import com.alibaba.dubbo.config.annotation.Reference;
import com.lsh.entity.User;
import com.lsh.service.ClientServer;
import com.lsh.service.UserService;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author :LiuShihao
* @date :Created in 2021/3/8 1:51 下午
* @desc :@Reference 是com.alibaba.dubbo.config.annotation.Reference包下的注解
*/
@Component("ClientServer")
public class ClientServerImpl implements ClientServer {
//这个注解是dubbo下的,这里的userService是在zookeeper注册中心取到的
@Reference
UserService userService;
@Override
public List<User> findAll() {
return userService.findAll();
}
}
四、测试
启动服务端和调用端,调用调用端Client
的findAll
接口,有返回数据就说明服务调用成功。
总结
@Service
注解暴露服务
@Reference
注解引用服务,声明需要调用的远程服务的接口,生成远程服务代理
注意:这两个注解都是Dubbo
包下的