赞
踩
4.1.1 启动时检查
- /*
- * 启动时检查
- * */
- public class testCheckException {
- public static void main(String[] args) throws IOException {
- // 初始化spring
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring.xml");
- System.in.read();
- }
- }
- <!--默认是true:抛异常;false:不抛异常-->
- <dubbo:consumer check="false" />
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.Target=System.out
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m%n
- log4j.appender.file=org.apache.log4j.FileAppender
- log4j.appender.file.File=dubbo.log
- log4j.appender.file.layout=org.apache.log4j.PatternLayout
- log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %l %m%n
- log4j.rootLogger=error, stdout,file
4.1.2 超时时间
- <!--设置超时时间为2秒,默认为1秒-->
- <dubbo:provider timeout="2000"/>
- import com.alibaba.dubbo.config.annotation.Service;
- import service.HelloService;
- /*
- * 服务实现类
- * */
- @Service
- public class HelloServiceImpl01 implements HelloService {
- public String sayHello(String name) {
- try {
- Thread.sleep(3000);
- }catch (Exception e){
- e.printStackTrace();
- }
- return "Hello,"+name+"!!!!";
- }
- }
4.1.3 重试次数
- <!-- 消费方连接第1次不算,再来重试3次,总共重试4次 -->
- <dubbo:provider timeout="2000" retries="3"/>
- import com.alibaba.dubbo.config.annotation.Service;
- import service.HelloService;
- /*
- * 服务实现类
- * */
- @Service
- public class HelloServiceImpl01 implements HelloService {
- public String sayHello(String name) {
- System.out.println("被调用了一次");
- try {
- Thread.sleep(3000);
- }catch (Exception e){
- e.printStackTrace();
- }
- return "Hello,"+name+"!!!!";
- }
- }
1. 提供方接口添加sayNo()方法并实现
- /*
- * 服务方接口
- * */
- public interface HelloService {
- String sayHello(String name);
- String sayNo();
- }
- import com.alibaba.dubbo.config.annotation.Service;
- import service.HelloService;
- /*
- * 服务实现类
- * */
- @Service
- public class HelloServiceImpl01 implements HelloService {
- public String sayHello(String name) {
- System.out.println("被调用了一次");
- try {
- Thread.sleep(3000);
- }catch (Exception e){
- e.printStackTrace();
- }
- return "Hello,"+name+"!!!!";
- }
-
- public String sayNo() {
- System.out.println("no被调用了1次");
- return "no";
- }
- }
2. 消费方接口添加sayNo()方法声明
- /*
- * 服务方接口(声明而已,具体实现会远程调用dubb-service的实现类)
- * */
- public interface HelloService {
- String sayHello(String name);
- String sayNo();
- }
3. 消费方controller
- package controller;
-
- import com.alibaba.dubbo.config.annotation.Reference;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import service.HelloService;
-
- /*
- * 控制层
- * */
- @Controller
- public class HelloAction {
-
- //@Reference // 此注解已经在xml文件中被<dubbo:reference>顶替,所以自动注入 即可
- @Autowired
- private HelloService helloService;
-
-
- @GetMapping("hello")
- @ResponseBody
- public String sayHi(String name){
- return helloService.sayHello(name);
- }
-
- @GetMapping("no")
- @ResponseBody
- public String no(){
- return helloService.sayNo();
- }
- }
4. 消费方配置方法重试次数
- <dubbo:reference interface="service.HelloService" id="HelloService">
- <dubbo:method name="sayHello" retries="3"/>
- <dubbo:method name="sayNo" retries="0"/> <!-- 不重试 -->
- </dubbo:reference>
4.1.4 多版本
- <dubbo:service interface="service.HelloService" class="service.impl.HelloServiceImpl01" version="1.0.0" />
- <dubbo:service interface="service.HelloService" class="service.impl.HelloServiceImpl02" version="2.0.0" />
- <dubbo:reference interface="service.HelloService" id="HelloService" version="2.0.0">
- <dubbo:method name="sayHello" retries="3"/>
- <dubbo:method name="sayNo" retries="0"/>
- </dubbo:reference>
- /*
- * 控制层
- * */
- @Controller
- public class HelloAction {
-
- @Autowired
- private HelloService helloService;
- }
4.1.5 本地存根
- package stub;
-
- import org.springframework.util.StringUtils;
- import service.HelloService;
-
- /*
- * 本地存根
- * */
- public class HelloServiceStub implements HelloService {
-
- // helloService的代理对象
- private HelloService helloService;
-
- /*
- * 本地存根必须以构造方法的形式注入
- * */
- public HelloServiceStub(HelloService helloService) {
- this.helloService = helloService;
- }
-
- @Override
- public String sayHello(String name) {
- if (!StringUtils.isEmpty(name)){
- return helloService.sayHello(name);
- }
- return "i am sorry!";
- }
-
- @Override
- public String sayNo() {
- return helloService.sayNo();
- }
- }
- <dubbo:reference interface="service.HelloService" id="HelloService" version="2.0.0" stub="stub.HelloServiceStub">
- <dubbo:method name="sayHello" retries="3"/>
- <dubbo:method name="sayNo" retries="0"/>
- </dubbo:reference>
修改提供者配置并启动3个提供者,让消费者对其进行访问
<dubbo:provider timeout="2000" retries="3" port="20881"/>
- public String sayNo() {
- System.out.println("1.0no被调用了1次");
- return "no";
- }
- public String sayNo() {
- System.out.println("2.0no被调用了1次");
- return "no";
- }
- <!--loadbalance="roundrobin" 轮循-->
- <dubbo:reference loadbalance="roundrobin" interface="service.HelloService" id="HelloService" version="2.0.0" stub="stub.HelloServiceStub">
- <dubbo:method name="sayHello" retries="3"/>
- <dubbo:method name="sayNo" retries="0"/>
- </dubbo:reference>
4.3.1 zookeeper宕机
4.4.1 为什么要服务降级
4.4.2 服务降级实现方式
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。