亲宝软件园·资讯

展开

springboot整合dubbo

孤依寒 人气:0

 Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看,Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。

1.服务提供者配置

//需要额外引入的jar包  提供者      
<dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

spring配置方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<dubbo:application name="user-service-provider"></dubbo:application>
    <!-- 指定注册中心的地址,将来服务提供者要向注册中心进行注册-->
    <dubbo:registry address="zookeeper://192.168.192.3:2181"></dubbo:registry>
    <!-- 指定将来服务消费者消费我的时候使用的rpc协议 -->
    <dubbo:protocol name="dubbo" port="11123"></dubbo:protocol>
    <!-- 服务发布 -->
    <bean id="userService" class="com.oracle.shop.user.service.impl.UserServiceImpl"></bean>
    <dubbo:service interface="com.oracle.shop.user.service.UserService" ref="userService"></dubbo:service>

</beans>

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.服务消费者订阅

 <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

spring配置方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<dubbo:application name="user-web-consumer"></dubbo:application>
    <!-- 指定注册中心的地址,将来服务提供者要向注册中心进行注册-->
    <dubbo:registry address="zookeeper://192.168.192.3:2181"></dubbo:registry>
    <!-- 订阅具体服务,让dubbo帮咱们生成动态代理客户端对象-->
    <dubbo:reference interface="com.oracle.shop.user.service.UserService" id="userService"></dubbo:reference>
    <dubbo:reference interface="com.oracle.shop.order.service.OrderService" id="orderService"></dubbo:reference>

</beans>

在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")
    public 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 );
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论