Spring中的aware接口详情
301
2022-09-12
springboot分布式整合dubbo的方式
Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。
1.服务提供者配置
//需要额外引入的jar包 提供者
spring配置方式
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
springboot配置方式
#dubbo的配置
#服务名
dubbo.application.name=user-provider
#注册中心地址
dubbo.registry.address=zookeeper://192.168.192.3:2181
#使用的协议以及端口号
dubbo.protocol.name=dubbo
dubbo.protocol.port=11111
发布服务的方式
在相应的实现类上添加注解
import com.alibaba.dubbo.config.annotation.Service;
import com.oracle.shopping.user.mapper.MenuMapper;
import com.oracle.shopping.user.po.Menu;
import com.oracle.shopping.user.service.MenuService;
import javax.annotation.Resource;
@Service //dubbo的注解表示发布该类
@org.springframework.stereotype.Service//spring的注解表示会在启动时实例化该类
public class MenuServiceImpl implements MenuService {
@Resource
private MenuMapper menuMapper;
@Override
public int deleteByPrimaryKey(String id) {
return menuMapper.deleteByPrimaryKey(id);
}
@Override
public int insert(Menu record) {
return menuMapper.insert(record);
}
@Override
public int insertSelective(Menu record) {
return menuMapper.insertSelective(record);
}
@Override
public Menu selectByPrimaryKey(String id) {
return menuMapper.selectByPrimaryKey(id);
}
@Override
public int updateByPrimaryKeySelective(Menu record) {
return menuMapper.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(Menu record) {
return menuMapper.updateByPrimaryKey(record);
}
}
启动类中添加dubbo的扫包器,设置扫描路径
@SpringBootApplication
@MapperScan("com.oracle.shopping.user.mapper")
@EnableTransactionManagement
@DubboComponentScan("com.oracle.shopping.user.service.impl")
public class UserProviderApp {
public static void main(String[] args) {
SpringApplication.run(UserProviderApp.class, args);
}
}
2.服务消费者订阅
spring配置方式
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://springframework.org/schema/beans
http://springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
在controller中进行依赖注入和调用
@Controller
@RequestMapping("/user")
public class UserController {
//通过动态代理的方式生成的动态代理类
@Autowired(required = false)
private UserService userService;
//不进行检查,防止报错无法运行
@Autowired(required = false)
private OrderService orderService;
@RequestMapping("/a")
public @ResponseBody
User detail(){
return userService.findUserById(1);
}
@RequestMapping("/b")
public @ResponseBody
Orders details(){
return orderService.findUserById(1);
}
}
springboot配置方式
#指定自己的名称
dubbo.application.name=user-consumer
#指定注册中心的地址
dubbo.registry.address=zookeeper://192.168.192.3:2181
在controller当中使用注解声明要使用的服务(实现类)
@RestController
@RequestMapping("/user")
public class UserController {
//表示要使用的服务
@Reference(interfaceName = "com.oracle.shopping.user.service.UserService")
private UserService userService;
@Autowired
private RedisTemplate redisTemplate;
@RequestMapping("/detail")
psDXfhublic User detail(String id){
return userService.selectByPrimaryKey(id);
}
}
启动类中设置dobbo注解的扫描路径
@SpringBootApplication
@DubboComponentScan("com.oracle.shopping.user.controller")
public class UserConsumerApp {
public static void main(String[] args) {
SpringApplication.run(UserConsumerApp.class );
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~