4.1
配置原则
在服务提供者配置访问参数。因为服务提供者更了解服务的各种参数。
4.2
关闭检查
dubbo
缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止
Spring
初始化完成,以便上线时,能及早发现问题,默认
check=true
。通过
check="false"
关闭检查,
比如,测试时,有些服务不关心,或者出现了循环依赖,必须有一方先启动。
例
1
:关闭某个服务的启动时检查
<dubbo:reference interface="com.foo.BarService" check="false" />
例
2
:关闭注册中心启动时检查
<dubbo:registry check="false" />
默认启动服务时检查注册中心存在并已运行。注册中心不启动会报错。
4.2
重试次数
消费者访问提供者,如果访问失败,则切换重试访问其它服务器,但重试会带来更长延迟。访问时间变长,用户的体验较差。多次重新访问服务器有可能访问成功。可通过
retries="2"
来设置重试次数
(
不含第一次
)
。
4.3 超时时间
由于网络或服务端不可靠,会导致调用出现一种不确定的中间状态(超时)。为了避免超时导致客户端资源(线程)挂起耗尽,必须设置超时时间。
timeout
:
调用远程服务超时时间
(
毫秒
)
4.3.1 dubbo
消费端
4.3.2 dubbo
服务端
4.4 版本号
每个接口都应定义版本号,为后续不兼容升级提供可能。
当一个接口有不同的实现,项目早期使用的一个实现类, 之后创建接口的新的实现类。区分不同的接口实现使用
version
。
特别是项目需要把早期接口的实现全部换位新的实现类,也需要使用
version.
可以用版本号从早期的接口实现过渡到新的接口实现,版本号不同的服务相互间不引用。
可以按照以下的步骤进行版本迁移:
在低压力时间段,先升级一半提供者为新版本
再将所有消费者升级为新版本
然后将剩下的一半提供者升级为新版本
例
:
以
zk-node-shop-userservice
为例。
功能实例
1 .使用maven的archety-webapp新建model
2.UserServiceImpl类
package com.it.dubbo.service.impl;
import com.it.dubbo.entity.User;
import com.it.dubbo.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public User queryUserById(Integer id, String name) {
User user = new User();
user.setId(id);
user.setName(name+"666");
return user;
}
}
3.UserServiceImpl2类
package com.it.dubbo.service.impl;
import com.it.dubbo.entity.User;
import com.it.dubbo.service.UserService;
public class UserServiceImpl2 implements UserService {
@Override
public User queryUserById(Integer id, String name) {
User user = new User();
user.setId(id);
user.setName(name+"888");
return user;
}
}
4.dubbo-userservice-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服务提供者:保证唯一性-->
<dubbo:application name="009-zk-userservice-multi-provider"/>
<!--声明dubbo的协议和端口号-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--使用注册中心-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--暴露服务-->
<!--不管接口有几个实现类,只要服务提供者服务接口的时候指定了版本号,那作为消费者引用远程接口服务的时候就必须指定版本号-->
<dubbo:service interface="com.it.dubbo.service.UserService" ref="userServiceImpl" version="1.0.0"/>
<dubbo:service interface="com.it.dubbo.service.UserService" ref="userServiceImpl2" version="2.0.0"/>
<bean id="userServiceImpl" class="com.it.dubbo.service.impl.UserServiceImpl"/>
<bean id="userServiceImpl2" class="com.it.dubbo.service.impl.UserServiceImpl2"/>
</beans>
5.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-userservice-provider.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
6.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.it.dubbo</groupId>
<artifactId>009-zk-userservice-multi-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!--spring依赖-->
<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>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!--接口工程-->
<dependency>
<groupId>com.it.dubbo</groupId>
<artifactId>006-zk-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--zookeeper依赖-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
1 .使用maven的archety-webapp新建model
2.UserController
package com.it.dubbo.web;
import com.it.dubbo.entity.User;
import com.it.dubbo.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private UserService userservice2;
@RequestMapping(value = "/userDetail")
public String userDetail(Model model,Integer id,String name){
User user = userService.queryUserById(id, name);
User user2 = userservice2.queryUserById(id, name);
model.addAttribute("user",user);
model.addAttribute("user2",user2);
return "userDetail";
}
}
3.dubbo-multi-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服务消费者名称:保证唯一性-->
<dubbo:application name="010-zk-multi-consumer"/>
<!--指定注册中心-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--引用远程接口服务-->
<dubbo:reference id="userService" interface="com.it.dubbo.service.UserService" version="1.0.0"/>
<dubbo:reference id="userService2" interface="com.it.dubbo.service.UserService" version="2.0.0"/>
</beans>
4.applicationContext.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.it.dubbo.web"/>
<!--配置注解驱动-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
5.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>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,classpath:dubbo-multi-consumer.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6.jsp
<%--
Created by IntelliJ IDEA.
User: lijianglin
Date: 2022/8/13
Time: 10:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<div>用户1</div>
<div>用户姓名:${user.name}</div>
<div>用户编号:${user.id}</div>
<div>用户2</div>
<div>用户姓名:${user2.name}</div>
<div>用户编号:${user2.id}</div>
</body>
</html>
1.
先启动
zookeeper
2.
启动
tomcat