pom文件,注意依赖的版本。
11 11 5.0.6.RELEASE
war
org.springframework spring-webmvc ${spring.version} org.springframework spring-core ${spring.version} org.springframework spring-beans ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring-context-support ${spring.version} org.springframework spring-tx ${spring.version} com.alibaba dubbo 2.5.7 org.apache.zookeeper zookeeper 3.4.6 com.github.sgroschupf zkclient 0.1 javassist javassist 3.11.0.GA
org.apache.tomcat.maven tomcat7-maven-plugin 8002 / package run
服务方提供接口
public interface HelloService {String sayHello(String name);
}
服务方接口实现类 :一定要特别注意这个Service注解,这是dubbo的Service注解
@com.alibaba.dubbo.config.annotation.Service
public class HelloServiceImpl implements HelloService {@Overridepublic String sayHello(String name) {return "Hello," + name + "!!!";}
}
服务方的配置文件Spring.xml
服务方的web.xml
org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:spring/spring.xml
消费方的pom文件和服务方的一致,但是需要修改tomcat的端口为8002
消费方的Controller
@RestController
public class HelloAction {// 注意这个注解,这也是 dubbo提供的注解@com.alibaba.dubbo.config.annotation.Referenceprivate HelloService helloService;@RequestMapping("hello")@ResponseBodypublic String hello(String name) {return helloService.sayHello(name);}
}
消费方的接口:controller中要依赖HelloService,所以我们创建一个接口,但是消费方不需要实现这个接口,因为让服务方会提供这个接口的服务!
public interface HelloService {String sayHello(String name);
}
消费方的web.xml
springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring/spring.xml springmvc /
先启动服务方,再启动消费方,访问http://localhost:8002/hello?name=james
,注意看服务方提供的接口:
@com.alibaba.dubbo.config.annotation.Service
public class HelloServiceImpl implements HelloService {@Overridepublic String sayHello(String name) {return "Hello," + name + "!!!";}
}
分别修改dubbo-server和dubbo-consumer的spring.xml,
由于网络或服务端不可靠,会导致调用过程中出现不确定的阻塞状态(超时)
为了避免超时导致客户端资源(线程)挂起耗尽,必须设置超时时间
在服务提供方的spring中添加如下配置:
模拟的网络延迟进行测试:
@com.alibaba.dubbo.config.annotation.Service
public class HelloServiceImpl implements HelloService {@Overridepublic String sayHello(String name) {try {Thread.sleep(3000);}catch (Exception e){e.printStackTrace();}return "Hello," + name + "!!!";}
}
因为超时设置2秒,而模拟的网络延迟有3秒,超出时限,报错。
PS:配置原则:
当出现失败,自动切换并重试其它服务器,dubbo重试的缺省值是2次,可以自行设置
在provider提供方配置:
@com.alibaba.dubbo.config.annotation.Service
public class HelloServiceImpl implements HelloService {@Overridepublic String sayHello(String name) {System.out.println("=============被调用 1 次===============");try {Thread.sleep(3000);}catch (Exception e){e.printStackTrace();}return "Hello," + name + "!!!";}
}
不是所有的方法都适合设置重试次数
提供方接口添加sayNo()方法并实现
public interface HelloService {String sayHello( String name );String sayNo();
}
@com.alibaba.dubbo.config.annotation.Service
public class HelloServiceImpl implements HelloService {@Overridepublic String sayHello(String name) {System.out.println("=============被调用 1 次===============");try {Thread.sleep(3000);}catch (Exception e){e.printStackTrace();}return "Hello," + name + "!!!";}public String sayNo() {System.out.println("-------no被调用一次-------");return "no!";}
}
消费方接口添加sayNo()方法声明
public interface HelloService {String sayHello( String name );String sayNo();
}
消费方controller
@RestController
public class HelloAction {@com.alibaba.dubbo.config.annotation.Referenceprivate HelloService helloService;@RequestMapping("hello")@ResponseBodypublic String hello(String name) {return helloService.sayHello(name);}@GetMapping("no")@ResponseBodypublic String no() {return helloService.sayNo();}
}
消费方配置方法重试次数
一个接口,多个(版本的)实现类,可以使用定义版本的方式引入
为HelloService接口定义两个实现类,提供者修改配置:
消费者就可以根据version的版本,选择具体的服务版本
version="*"
,那么就会随机调用服务提供者的版本目前的通信案例所有的操作全都是 消费者发起,由服务提供者执行,这样会让提供者承担所有压力,一些功能消费者完全能够胜,把合法的参数再发送给提供者执行,效率高了,提供者也没那么累了,这就是本地存根
简单来讲,先在消费者处理一些业务逻辑,再调用提供者的过程,就是“本地存根”
本地存根”代码实现肯定在 消费者,创建一个HelloServiceStub类并且实现HelloService接口,并且必须使用构造方法的方式注入
public class HelloServiceStub implements HelloService {private HelloService helloService;// 注入HelloServicepublic HelloServiceStub(HelloService helloService) {this.helloService = helloService;}public String sayHello(String name) {System.out.println("本地存根数据验证。。。");if(!StringUtils.isEmpty(name)){return helloService.sayHello(name);}return "i am sorry!";}public String sayNo() {return helloService.sayNo();}
}
修改消费者配置:
可以使用管理端修改权重
为什么要服务降级?