dubbo service degradation dubbo service degradations

 

turn:

1. In the Management Console to configure service degradation dubbo

Configuring the meaning of the figure are: consumer calls com.zhang.HelloService method, the direct return null, not initiate a remote call.

Actual operations are: Added override node zk of the /dubbo/com.zhang.HelloService/configurators.

override://0.0.0.0/com.zhang.HelloService?category=configurators&dynamic=false&group=a&mock=force:return+null

 

2 may be performed by a service degradation Code: (dubbo given document snippet)

Service degradation function can temporarily block a non-critical service error, and define the return policy after the downgrade.
Write rules covering the dynamic configuration to the registry:

RegistryFactory registryFactory = 
    ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
Registry registry = registryFactory.getRegistry(URL.valueOf("zookeeper://10.20.153.10:2181"));
registry.register(URL.valueOf("override://0.0.0.0/com.foo.BarService?category=configurators&dynamic=false&application=consumer_app&mock=force:return+null"));

mock = force: return + null
represents consumer directly call the method returns a null value for the service, not to initiate a remote call. Used to shield important impact on the caller's service is not available.
mock = fail: return + null
represents consumer calls to this method of service after a failure, and then return a null value, do not throw an exception. To tolerate impact on the caller's service is not important when instability.

Dubbo the true meaning of service degradation: on the provider does not operate, but told the consumer, what action do you call service.

 

3. Now analyze consumer end static configuration mock, consumer how to call mock service

The user can: mock configuration attributes <dubbo reference> in. How to configure a custom mock, yet they get to know. To mock = "force: return + null" for example, we first analyze dubbo default MockInvoker.

MockClusterInvoker logic:

Copy the code
// MockClusterInvoker
public Result invoke(Invocation invocation) throws RpcException {
    Result result = null; // 获取<dubbo:reference>的mock属性。 String value = directory.getUrl().getMethodParameter(invocation.getMethodName(), Constants.MOCK_KEY, Boolean.FALSE.toString()).trim(); if (value.length() == 0 || value.equalsIgnoreCase("false")){ //mock="" 或者 mock="false" result = this.invoker.invoke(invocation); } else if (value.startsWith("force")) { //强制使用mock,如mock="force:return+null" if (logger.isWarnEnabled()) { logger.info("force-mock: " + invocation.getMethodName() + " force-mock enabled , url : " + directory.getUrl()); } result = doMockInvoke(invocation, null); } else { //mock="fail:return+null" 或 mock="MyMock" try { result = this.invoker.invoke(invocation); }catch (RpcException e) { if (e.isBiz()) { throw e; } else { if (logger.isWarnEnabled()) { logger.info("fail-mock: " + invocation.getMethodName() + " fail-mock enabled , url : " + directory.getUrl(), e); } //出现超时异常 result = doMockInvoke(invocation, e); } } } return result; } private Result doMockInvoke(Invocation invocation, RpcException e) { Result result = null; Invoker<T> minvoker; List<Invoker<T>> mockInvokers = selectMockInvoker(invocation); if (mockInvokers == null || mockInvokers.size() == 0){ //如果没有配置自定义Mock,则使用默认MockInvoker minvoker = (Invoker<T>) new MockInvoker(directory.getUrl()); } else { minvoker = mockInvokers.get(0); } try { //调用 result = minvoker.invoke(invocation); } catch (RpcException me) { if (me.isBiz()) { result = new RpcResult(me.getCause()); } else { throw new RpcException(me.getCode(), getMockExceptionMessage(e, me), me.getCause()); } } catch (Throwable me) { throw new RpcException(getMockExceptionMessage(e, me), me.getCause()); } return result; }
Copy the code

The default MockInvoker:

Copy the code
// MockInvoker
public Result invoke(Invocation invocation) throws RpcException {
    //获取url中的sayHello.mock参数值 String mock = getUrl().getParameter(invocation.getMethodName() + "." + Constants.MOCK_KEY); if (invocation instanceof RpcInvocation) { ((RpcInvocation) invocation).setInvoker(this); } if (StringUtils.isBlank(mock)){ //获取mock属性值 mock = getUrl().getParameter(Constants.MOCK_KEY); } if (StringUtils.isBlank(mock)){ throw new RpcException(new IllegalAccessException("mock can not be null. url :" + url)); } //假定mock="force:return+null",处理后mock="return+null" mock = normallizeMock(URL.decode(mock)); if (Constants.RETURN_PREFIX.trim().equalsIgnoreCase(mock.trim())){ RpcResult result = new RpcResult(); result.setValue(null); return result; } else if (mock.startsWith(Constants.RETURN_PREFIX)) { //构造返回值 mock = mock.substring(Constants.RETURN_PREFIX.length()).trim(); mock = mock.replace('`', '"'); try { Type[] returnTypes = RpcUtils.getReturnTypes(invocation); Object value = parseMockValue(mock, returnTypes); return new RpcResult(value); } catch (Exception ew) { throw new RpcException("mock return invoke error. method:" + invocation.getMethodName() + ", mock:" + mock + ", url: "+ url , ew); } } else if (mock.startsWith(Constants.THROW_PREFIX)) { mock = mock.substring(Constants.THROW_PREFIX.length()).trim(); mock = mock.replace('`', '"'); if (StringUtils.isBlank(mock)){ throw new RpcException(" mocked exception for Service degradation. "); } else { //用户自定义类 Throwable t = getThrowable(mock); throw new RpcException(RpcException.BIZ_EXCEPTION, t); } } else { //impl mock try { Invoker<T> invoker = getInvoker(mock); return invoker.invoke(invocation); } catch (Throwable t) { throw new RpcException("Failed to create mock implemention class " + mock , t); } } } //mock=fail:throw //mock=fail:return //mock=xx.Service private String normallizeMock(String mock) { if (mock == null || mock.trim().length() ==0){ return mock; } else if (ConfigUtils.isDefault(mock) || "fail".equalsIgnoreCase(mock.trim()) || "force".equalsIgnoreCase(mock.trim())){ mock = url.getServiceInterface()+"Mock"; } if (mock.startsWith(Constants.FAIL_PREFIX)) { mock = mock.substring(Constants.FAIL_PREFIX.length()).trim(); } else if (mock.startsWith(Constants.FORCE_PREFIX)) { mock = mock.substring(Constants.FORCE_PREFIX.length()).trim(); } return mock; }
Copy the code

 

Tags: Dubbo

1. In the Management Console to configure service degradation dubbo

Configuring the meaning of the figure are: consumer calls com.zhang.HelloService method, the direct return null, not initiate a remote call.

Actual operations are: Added override node zk of the /dubbo/com.zhang.HelloService/configurators.

override://0.0.0.0/com.zhang.HelloService?category=configurators&dynamic=false&group=a&mock=force:return+null

 

2 may be performed by a service degradation Code: (dubbo given document snippet)

Service degradation function can temporarily block a non-critical service error, and define the return policy after the downgrade.
Write rules covering the dynamic configuration to the registry:

RegistryFactory registryFactory = 
    ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
Registry registry = registryFactory.getRegistry(URL.valueOf("zookeeper://10.20.153.10:2181"));
registry.register(URL.valueOf("override://0.0.0.0/com.foo.BarService?category=configurators&dynamic=false&application=consumer_app&mock=force:return+null"));

mock = force: return + null
represents consumer directly call the method returns a null value for the service, not to initiate a remote call. Used to shield important impact on the caller's service is not available.
mock = fail: return + null
represents consumer calls to this method of service after a failure, and then return a null value, do not throw an exception. To tolerate impact on the caller's service is not important when instability.

Dubbo the true meaning of service degradation: on the provider does not operate, but told the consumer, what action do you call service.

 

3. Now analyze consumer end static configuration mock, consumer how to call mock service

The user can: mock configuration attributes <dubbo reference> in. How to configure a custom mock, yet they get to know. To mock = "force: return + null" for example, we first analyze dubbo default MockInvoker.

MockClusterInvoker logic:

Copy the code
// MockClusterInvoker
public Result invoke(Invocation invocation) throws RpcException {
    Result result = null; // 获取<dubbo:reference>的mock属性。 String value = directory.getUrl().getMethodParameter(invocation.getMethodName(), Constants.MOCK_KEY, Boolean.FALSE.toString()).trim(); if (value.length() == 0 || value.equalsIgnoreCase("false")){ //mock="" 或者 mock="false" result = this.invoker.invoke(invocation); } else if (value.startsWith("force")) { //强制使用mock,如mock="force:return+null" if (logger.isWarnEnabled()) { logger.info("force-mock: " + invocation.getMethodName() + " force-mock enabled , url : " + directory.getUrl()); } result = doMockInvoke(invocation, null); } else { //mock="fail:return+null" 或 mock="MyMock" try { result = this.invoker.invoke(invocation); }catch (RpcException e) { if (e.isBiz()) { throw e; } else { if (logger.isWarnEnabled()) { logger.info("fail-mock: " + invocation.getMethodName() + " fail-mock enabled , url : " + directory.getUrl(), e); } //出现超时异常 result = doMockInvoke(invocation, e); } } } return result; } private Result doMockInvoke(Invocation invocation, RpcException e) { Result result = null; Invoker<T> minvoker; List<Invoker<T>> mockInvokers = selectMockInvoker(invocation); if (mockInvokers == null || mockInvokers.size() == 0){ //如果没有配置自定义Mock,则使用默认MockInvoker minvoker = (Invoker<T>) new MockInvoker(directory.getUrl()); } else { minvoker = mockInvokers.get(0); } try { //调用 result = minvoker.invoke(invocation); } catch (RpcException me) { if (me.isBiz()) { result = new RpcResult(me.getCause()); } else { throw new RpcException(me.getCode(), getMockExceptionMessage(e, me), me.getCause()); } } catch (Throwable me) { throw new RpcException(getMockExceptionMessage(e, me), me.getCause()); } return result; }
Copy the code

The default MockInvoker:

Copy the code
// MockInvoker
public Result invoke(Invocation invocation) throws RpcException {
    //获取url中的sayHello.mock参数值 String mock = getUrl().getParameter(invocation.getMethodName() + "." + Constants.MOCK_KEY); if (invocation instanceof RpcInvocation) { ((RpcInvocation) invocation).setInvoker(this); } if (StringUtils.isBlank(mock)){ //获取mock属性值 mock = getUrl().getParameter(Constants.MOCK_KEY); } if (StringUtils.isBlank(mock)){ throw new RpcException(new IllegalAccessException("mock can not be null. url :" + url)); } //假定mock="force:return+null",处理后mock="return+null" mock = normallizeMock(URL.decode(mock)); if (Constants.RETURN_PREFIX.trim().equalsIgnoreCase(mock.trim())){ RpcResult result = new RpcResult(); result.setValue(null); return result; } else if (mock.startsWith(Constants.RETURN_PREFIX)) { //构造返回值 mock = mock.substring(Constants.RETURN_PREFIX.length()).trim(); mock = mock.replace('`', '"'); try { Type[] returnTypes = RpcUtils.getReturnTypes(invocation); Object value = parseMockValue(mock, returnTypes); return new RpcResult(value); } catch (Exception ew) { throw new RpcException("mock return invoke error. method:" + invocation.getMethodName() + ", mock:" + mock + ", url: "+ url , ew); } } else if (mock.startsWith(Constants.THROW_PREFIX)) { mock = mock.substring(Constants.THROW_PREFIX.length()).trim(); mock = mock.replace('`', '"'); if (StringUtils.isBlank(mock)){ throw new RpcException(" mocked exception for Service degradation. "); } else { //用户自定义类 Throwable t = getThrowable(mock); throw new RpcException(RpcException.BIZ_EXCEPTION, t); } } else { //impl mock try { Invoker<T> invoker = getInvoker(mock); return invoker.invoke(invocation); } catch (Throwable t) { throw new RpcException("Failed to create mock implemention class " + mock , t); } } } //mock=fail:throw //mock=fail:return //mock=xx.Service private String normallizeMock(String mock) { if (mock == null || mock.trim().length() ==0){ return mock; } else if (ConfigUtils.isDefault(mock) || "fail".equalsIgnoreCase(mock.trim()) || "force".equalsIgnoreCase(mock.trim())){ mock = url.getServiceInterface()+"Mock"; } if (mock.startsWith(Constants.FAIL_PREFIX)) { mock = mock.substring(Constants.FAIL_PREFIX.length()).trim(); } else if (mock.startsWith(Constants.FORCE_PREFIX)) { mock = mock.substring(Constants.FORCE_PREFIX.length()).trim(); } return mock; }
Copy the code

 

Guess you like

Origin www.cnblogs.com/libin6505/p/11270147.html