dubbo的初级使用
发布日期:2021-05-07 14:38:27 浏览次数:18 分类:原创文章

本文共 3857 字,大约阅读时间需要 12 分钟。

1.导入dubbo坐标,这个坐标每个项目都是固定的
<!--Dubbo的起步依赖,版本2.7之后统一为rg.apache.dubbo -->        <!--<dubbo.version>2.7.4.1</dubbo.version>-->        <!--<zookeeper.version>4.0.0</zookeeper.version>-->        <dependency>            <groupId>org.apache.dubbo</groupId>            <artifactId>dubbo</artifactId>            <version>${dubbo.version}</version>        </dependency>        <!--ZooKeeper客户端实现 -->        <dependency>            <groupId>org.apache.curator</groupId>            <artifactId>curator-framework</artifactId>            <version>${zookeeper.version}</version>        </dependency>        <!--ZooKeeper客户端实现 -->        <dependency>            <groupId>org.apache.curator</groupId>            <artifactId>curator-recipes</artifactId>            <version>${zookeeper.version}</version>        </dependency>
2.配置servive层的applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"	   xmlns:context="http://www.springframework.org/schema/context"	   xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"	   xsi:schemaLocation="        http://www.springframework.org/schema/beans        https://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        https://www.springframework.org/schema/context/spring-context.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd        ">	<!--dubbo配置-->	<!--配置项目名称-->	<dubbo:application name="dubbo-service"/>	<!--配置注册中心地址-->	<dubbo:registry address="zookeeper://192.168.31.81:2181"/>	<!--注解扫描-->	<dubbo:annotation package="com.lzb.service.impl"/></beans>
3.给资源加dubbo包下的@service注解
package com.lzb.service.impl;import com.lzb.UserService;import org.apache.dubbo.config.annotation.Service;@Service //dubbbo的bean注入,提供ip,端口,路径public class UserServiceImpl implements UserService {       @Override    public String hello() {           return "hello";    }}
4.配置controller层的spring-mvc.xml
<?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://dubbo.apache.org/schema/dubbo"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd         http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">    <mvc:annotation-driven/>    <context:component-scan base-package="com.lzb.controller"/>    <!--dubbo配置-->    <dubbo:application name="dubbo-web"/>    <dubbo:registry address="zookeeper://192.168.31.81:2181"/>    <dubbo:annotation package="com.lzb.controller"/></beans>
5.给需要获取资源的对象变量加@Reference
package com.lzb.controller;import com.lzb.UserService;import org.apache.dubbo.config.annotation.Reference;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/user")public class UserController {       /**     * 1.从zookeeper获取UserService的url     * 2.远程调用并赋给变量     */    @Reference //远程注入    UserService userService;    @RequestMapping("/hello")    public String hello(){           return userService.hello();    }}
地址缓存,缓存后不再访问注册中心pojo序列化超时和重试和多版本@Service(timeout = 3000,retries = 2,version = "v1.0")@Reference(version = "v1.0")负载均衡/权重@Service(weight = 100)服务降级@Service(mock = "force:return null")集群容错@Reference(cluster = "failover")
上一篇:spring自定义组件扫描器
下一篇:zookeeper安装

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年03月31日 13时41分14秒