Dubbo (three) configuration instructions

Configuration instructions

Check at startup

At startup, it will check whether the dependent service is available in the registry, and throw an exception when it is not available.
Write the main method to initialize the container on the consumer side to start (tomcat startup mode, you must visit the action once to initialize spring)

/**
 * @Program: dubbo_test2
 * @Author: XiaoXing
 * @Create: 2021-01-16 13:35
 * @Description: 启动时检查
 **/
public class TestCheckException {
    
    

    public static void main(String[] args) {
    
    
        //初始化spring
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/spring.xml");

        try {
    
    
            System.in.read();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

}
	<!--
        关闭所有服务的启动时检查 (没有提供者时报错)
        默认是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

overtime time

Due to the unreliability of the network or server, it will cause an uncertain blocking state (timeout) during the call.
In order to avoid timeout causing client resources (threads) to hang and exhaust, the timeout period must be set
. Add the following configuration to the service provider:

<!--设置超时时间为2秒,默认为1秒-->
<dubbo:provider timeout="2000"/>

The simulated network delay can be added to the service implementation HelloServiceImpl.java for testing:

@Service
public class HelloServiceImpl implements HelloService {
    
    

    @Override
    public String sayHello(String name) {
    
    
        try {
    
    
            Thread.sleep(3000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        return "Hello " + name + " !!!";
    }

}

The timeout is set to 2 seconds, and the simulated network delay is 3 seconds. If the time limit is exceeded, an error will be reported!

Insert picture description here

Configuration principle:

  • dubbo recommends configuring as many Consumer attributes as possible on the Provider:
    • As the service provider, he knows the service performance parameters better than the service user, such as call timeout time, reasonable number of retries, etc.
    • After the Provider is configured, the Consumer will use the Provider's configuration value if it is not configured, that is, the Provider configuration can be the default value of the consumer.

number of retries

When a failure occurs, it will automatically switch and retry other servers. The default value of dubbo retry is 2 times. We can set it by ourselves. Configure
in the provider:

<!-- 消费方连接第1次不算,再来重试3次,总共重试4次 -->
<dubbo:provider timeout="2000" retries="3"/>
@Service
public class HelloServiceImpl implements HelloService {
    
    

    @Override
    public String sayHello(String name) {
    
    
        System.out.println("被调用1次");
        try {
    
    
            //模拟网络延迟
            Thread.sleep(3000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        return "Hello " + name + " !!!";
    }

}
  • Not all methods are suitable for setting the number of retries

    • Idempotent method: suitable (when the parameters are the same, no matter how many times it is executed, the result is the same, for example: query, modify)
    • Non-idempotent method: not suitable (when the parameters are the same, the execution result is different, for example: delete, add)
  • Set a method individually

    public interface HelloService {
          
          
    
        public String sayHello(String name);
    
        public String sayNo();
    
    }
    
    @Override
    public String sayNo() {
          
          
        System.out.println("------------no--------------");
        return "no";
    }
    

    Add the sayNo() method declaration to the consumer interface

    public interface HelloService {
          
          
    
        String sayHello( String name );
    
        public String sayNo();
    
    }
    
    @Controller
    public class HelloAction {
          
          
    
    //    @Reference  // 远程去服务方将service的实现类注入进来
        @Autowired
        private HelloService helloService;
    
        @RequestMapping("/hello")
        @ResponseBody
        public String sayHi(String name){
          
          
            System.out.println(name);
            return helloService.sayHello(name);
        }
    
        @RequestMapping("/no")
        @ResponseBody
        public String sayNo(){
          
          
            return helloService.sayNo();
        }
    
    }
    

    Consumer configuration method retry times

    <dubbo:reference interface="service.HelloService" id="helloService">
        <dubbo:method name="sayHello" retries="3"></dubbo:method>
        <dubbo:method name="sayNo" retries="0"></dubbo:method>
    </dubbo:reference>
    

Multi-version

One interface, multiple (versions) implementation classes, can be introduced by defining versions.
Define two implementation classes for the HelloService interface, and the provider can modify the configuration:

<!--配置多版本-->
<dubbo:service interface="service.HelloService" class="service.impl.HelloServiceImpl01" version="1.0.0"></dubbo:service>
<dubbo:service interface="service.HelloService" class="service.impl.HelloServiceImpl02" version="2.0.0"></dubbo:service>

Consumers can choose a specific service version according to the version

<dubbo:reference interface="service.HelloService" id="helloService" version="1.0.0">
    <dubbo:method name="sayHello" retries="3"></dubbo:method>
    <dubbo:method name="sayNo" retries="0"></dubbo:method>
</dubbo:reference>

Note: The consumer's control layer should be changed to automatic injection, because the @Reference annotation and dubbo:reference conflict here

@Controller
public class HelloAction {
    
    

//    @Reference  // 远程去服务方将service的实现类注入进来
    @Autowired
    private HelloService helloService;
}

When the consumer's version is changed to version="*", then the service provider's version will be randomly called

Local stub

At present, our distributed architecture has a serious problem, that is, all operations are initiated by consumers, and the service provider executes the
consumer's mouthful but does nothing. This will make the provider very tired, for example Simple parameter verification, consumers are fully competent, send the legal parameters to the provider for execution, the efficiency is high, and the provider is not so tired.
For example: go to the Real Estate Bureau to handle the house transfer, please bring your own certificates and materials, If you don't bring anything, it will be very troublesome to go through the transfer procedures. You must first investigate what kind of loan you have, whether there is a mortgage, whether the real estate certificate is yourself, and copy the information. It must be done in one day. Come tomorrow. If you can prepare these things in advance and handle the transfer, one hour is enough. This is "the reason for the high efficiency of the real estate agency".
Not much to say. First deal with some business logic in the consumer, and then call the provider. , Is the "local stub"
code implementation must be in the consumer, create a HelloServiceStub class and implement the HelloService interface
Note: must use the construction method to inject

public class HelloServiceStub implements 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();
    }
}

Modify consumer configuration

<dubbo:reference interface="service.HelloService" id="helloService" version="2.0.0" stub="stub.HelloServiceStub">
    <dubbo:method name="sayHello" retries="3"></dubbo:method>
    <dubbo:method name="sayNo" retries="0"></dubbo:method>
</dubbo:reference>

Guess you like

Origin blog.csdn.net/weixin_49741990/article/details/112710524