Dubbo frame
Introduction to the Dubbo framework
Dubbo (pronounced [ˈdʌbəʊ]) is a high-performance and excellent service framework (SOA) open sourced by Alibaba. It enables applications to implement service output and input functions through high-performance RPC, and can be seamlessly integrated with the Spring framework.
Dubbo is a high-performance, lightweight open source Java RPC framework that provides three core capabilities: interface-oriented remote method invocation, intelligent fault tolerance and load balancing, and automatic service registration and discovery.
How to set up Dubbo
1-Dubbo's dependency
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
2-How it works
In theory, the two servers that do not meet the same-origin test communicate through cross-domain, but this method leads to insecurity, but the microservice Dubbo framework remotely calls the local method without barriers to realize the communication between the two servers. It is also extremely safe
Yml file configuration
#关于Dubbo配置
dubbo:
scan:
basePackages: com.jt #指定dubbo的包路径
application: #应用名称
name: provider-item #一个接口对应一个服务名称
registry:
address: zookeeper://192.168.126.129:2181?backup=192.168.126.129:2182,192.168.126.129:2183
protocol: #指定协议
name: dubbo #使用dubbo协议(tcp-ip) web-controller直接调用sso-Service
port: 20880 #每一个服务都有自己特定的端口 不能重复.
zookeeper ---- is the registration center (see related content)
Project structure
1. The service interface and pojo can be placed in the public module
2. Put serviceImpl into the service provider (implementation class of service)
3. Put the controller control layer into the consumer
interface被消费者/生产者(提供者)依赖
Code structure
Common module
provider
Mapper (Dao) and service implementation class (Impl)
Mapper (Dao)
使用mybatis-puls的情况下
public interface UserMapper extends BaseMapper<User>{
}
serviceImpl (implementation class of service)
@Service(timeout=3000) //3秒超时 内部实现了rpc
//@org.springframework.stereotype.Service//将对象交给spring容器管理
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAll() {
System.out.println("我是提供者");
return userMapper.selectList(null);
}
consumer
@RestController
public class UserController {
利用dubbo的方式为接口创建代理对象 利用rpc调用
@Reference(loadbalance="roundrobin")
private UserService userService;
@RequestMapping("/findAll")
public List<User> findAll(){
//远程调用时传递的对象数据必须序列化.
return userService.findAll();
}
Code changes
Code changes after using Dubbo
消费者中
@Autowired ———— @Reference(loadbalance=“roundrobin”) dubbo的注解
提供者中
@Service ———— @Service(timeout=3000) dubbo's comment