• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

dubbo(六)——Dubbo配置

武飞扬头像
⊙ω⊙ 在学习的路上越走越远~~~
帮助1

一、配置原则

服务提供这配置访问参数。因为服务提供者更了解服务的各种参数。

例如:配置timeout参数
学新通

二、关闭检查

dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发现问题,默认check=true。通过check="false" 关闭检查,比如,测试时,有些服务不关心,或者出现了循环依赖,必须有一方先启动。一般开发环境上不关闭检查,生产环境(线上环境)关闭检查

  1. 关闭某个服务的启动时检查
<dubbo:reference interface="com.foo.BarService" check="false"/>
  1. 关闭注册中心启动时检查
<dubbo:registry check="false"/>

默认启动服务时检查注册中心存在并已运行,注册中心不启动会报错。

三、重试次数

消费者访问提供者,如果访问失败,则切换重试访问其它服务器,但重试会带来更长延迟。访问时间变长,用户的体验较差。多次重新访问服务有可能访问成功。可通过retries="2"来设置重试次数(不含第一次)。

重试次数配置如下:

<dubbo:service retries="2"/>
或
<dubbo:reference retries="2"/>

四、超时时间

由于网络或服务端不可靠,会导致调用出现一种不确定的中间状态(超时)。为了避免超时导致客户端资源(线程)挂起耗尽,必须设置超时时间。
timeout: 调用远程服务超时时间(毫秒)

  1. dubbo消费端
    指定接口超时配置
<dubbo:reference interface="com.foo.BarService" timeout="2000"/>
  1. dubbo服务端
    指定接口超时配置
<dubbo:service interface="com.foo.BarService" timeout="2000"/>

五、版本号

每个接口都应定义版本号,为后续不兼容升级提供可能。当一个接口有不同的实现,项目早起使用的一个实现类,之后创建接口的新的实现类。区分不同的接口实现使用version
复制zk-node-shop-userservicezk-node-shop-multi-userservice

六、版本号项目实战

学新通

  1. 接口

学新通

  • DubboService
public interface DubboService {
    public String sayDubbo();
}
  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cjw.dubbo</groupId>
  <artifactId>003-interface</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
</project>
  1. 服务提供者
    学新通
  • DubboServiceImpl
public class DubboServiceImpl implements DubboService {
    public String sayDubbo() {
        return "zkeeper---dubbo";
    }
}
  • newDubboServiceImpl
public class newDubboServiceImpl implements DubboService {
    public String sayDubbo() {
        return "newDubboServiceImpl";
    }
}
  • dubbo-zk-provider.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!--声明服务提供者名称-->
    <dubbo:application name="004-zk-provider"/>
    <!--指定协议,提供者名称-->
    <dubbo:registry  address="zookeeper://localhost:2181"/>
    <!--指定注册中心地址-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--暴露服务 版本号1.0-->
    <dubbo:service  interface="com.cjw.dubbo.service.DubboService" ref="dubboServiceImpl" version="1.0"/>
    <!--暴露服务 版本号2.0-->
    <dubbo:service  interface="com.cjw.dubbo.service.DubboService" ref="newdubboServiceImpl" version="2.0"/>

    <bean id="dubboServiceImpl" class="com.cjw.dubbo.impl.DubboServiceImpl"/>

    <bean id="newdubboServiceImpl" class="com.cjw.dubbo.impl.newDubboServiceImpl"/>
</beans>
学新通
  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:dubbo-zk-provider.xml</param-value>
  </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>
  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cjw.dubbo</groupId>
  <artifactId>004-zk-provider</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.2</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.16.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.16.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-framework</artifactId>
      <version>4.1.0</version>
    </dependency>
    <dependency>
      <groupId>com.cjw.dubbo</groupId>
      <artifactId>003-interface</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>
学新通
  1. 服务消费者

学新通

  • DubboZkController
@Controller
public class DubboZkController {
    @Autowired
    private DubboService dubboService;
    @Autowired
    private DubboService newdubboService;
    @RequestMapping("/zk")
    public String getZk(Model model) {
        String dubbo = dubboService.sayDubbo();
        model.addAttribute("dubbo", dubbo);
        String dubbo1 = newdubboService.sayDubbo();
        model.addAttribute("dubbo1", dubbo1);
        return "dubbo";
    }
}
  • dubbo-zk-consumer.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!--指定服务消费者名称 唯一-->
    <dubbo:application name="005-zk-consumer"/>
    <!--使用zookeeper-->
    <dubbo:registry  address="zookeeper://localhost:2181"/>
 <!--引用远程接口
    id:远程服务代理对象
    interface:接口的全限定类名
    version:dubbo 版本号
 -->
    <dubbo:reference id="dubboService" interface="com.cjw.dubbo.service.DubboService" version="1.0"/>
    <dubbo:reference id="newdubboService" interface="com.cjw.dubbo.service.DubboService" version="2.0"/>
</beans>
学新通
  • springmvc.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.cjw.dubbo.web"/>
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
  • web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml,classpath:dubbo-zk-consumer.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

学新通
  • dubbo.jsp
    <title>Title</title>
    <hr/>
    测试zookeeper连接:${dubbo}
    <hr/>
    测试dubbo版本号:${dubbo1}
    <hr/>
  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.cjw.dubbo</groupId>
  <artifactId>005-zk-consumer</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
<dependencies>
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.2</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.16.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.16.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.1.0</version>
  </dependency>
  <dependency>
    <groupId>com.cjw.dubbo</groupId>
    <artifactId>003-interface</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
</dependencies>
</project>
学新通

七、什么是监控中心

  1. 什么是监控中心
    Dubbo的使用,其实只需要有注册中心,消费者,提供者这三个就可以使用了,但是并不能看到有哪些消费者和提供者,为了更好的调试,发现问题,解决问题,因此引入dubbo-admin。通过dubbo-admin可以对消费者和提供者进行管理。可以在dubbo应用部署做动态的调整,服务的管理。
  • dubbo-admin
    图形化的服务管理页面:安装时需要指定注册中心地址,即可从注册中心中获取所有的提供者/消费者进行配置管理。
  • dubbo-monitor-simple
    简单的监控中心:
  1. 发布配置中心
  • 下载监控中心

链接:https://pan.百度.com/s/1G9Iz5xLd1lQ5HFGwCzG-jw
提取码:o6o5

  • 运行管理后台dubbo-admin
    启动方式1:
  1. dubbo-2.5.x目录执行 mvn clean package -Dmaven.test.skip=true
  2. 然后将war包放到tomcat中的webapps下,启动下tomcat后,停止tomcat
  3. 将解压好的dubbo-admin-2.5.10中内容复制到ROOT目录,然后在启动下tomcat,这时tomcat的端口最好不要是8080容易和zookeeper默认端口冲突
    tomcat 启动方式: tomcat/bin目录下执行 sh catalina.sh run(linux)/startup.bat(windows) 启动Tomcat
    学新通
    学新通

启动方式2:
导入Eclipse或者idea启动

  1. dubbo.properties配置信息

学新通
用户密码(注意:root用户的密码是rootguest用户的密码是guest
注册中心地址(这里用的是zookeeper
学新通

  1. 效果图
    用户admin 密码admin,用户guest 密码guest 上面配置的密码
    学新通
    dubbo-admin首页

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggfigk
系列文章
更多 icon
同类精品
更多 icon
继续加载