GraphQL和微服务架构(GraphQL and Microservice Architecture)
我试图了解GraphQL最适合在Microservice架构中使用的位置。
有一些争论,只有一个GraphQL模式作为API网关代理对目标微服务的请求并强制他们的响应。 微服务器仍然会使用REST / Thrift协议进行通信思想。
相反,另一种方法是每个微服务器具有多个GraphQL模式。 拥有一个更小的API网关服务器,将请求的路由请求到目标微服务,并附有请求+图形查询的所有信息。
第一种方法
拥有1个GraphQL架构作为API网关将会有一个缺点,每次更改您的微服务合同输入/输出时,我们必须在API网关侧相应更改GraphQL架构。
第二种方法
如果每个微服务使用多个GraphQL架构,则以某种方式有意义,因为GraphQL强制执行模式定义,并且消费者将需要遵守从微服务器给出的输入/输出。
问题
您在哪里可以找到GraphQL适合设计微服务架构?
您将如何使用可能的GraphQL实现来设计API网关?
I'm trying to understand where GraphQL is most suitable to use within a Microservice architecture.
There is some debate about having only 1 GraphQL schema that works as API Gateway proxying the request to the targeted microservices and coercing their response. Microservices still would use REST / Thrift protocol for communication thought.
Another approach is instead to have multiple GraphQL schemas one per microservice. Having a smaller API Gateway server that route the request to the targeted microservice with all the information of the request + the GraphQL query.
1st Approach
Having 1 GraphQL Schema as an API Gateway will have a downside where every time you change your microservice contract input/output, we have to change the GraphQL Schema accordingly on the API Gateway Side.
2nd Approach
If using Multiple GraphQL Schema per microservices, make sense in a way because GraphQL enforces a schema definition, and the consumer will need to respect input/output given from the microservice.
Questions
Where do you find GraphQL the right fit for designing microservice architecture?
How would you design an API Gateway with a possible GraphQL implementation?
原文:https://stackoverflow.com/questions/38071714
最满意答案
仅当您的catch语句抛出到另一个try / catch时,例如:
try{ ... try{ ... } catch(ExceptionA a) { throw; } catch(Exception e) { //will not not catch ExceptionA (rethrow or not) } } catch(ExceptionA a) { //this would catch the re-throw } catch( Exception e) { }
相反,为什么不捕获一般的
Exception
,然后判断异常类型?try{ ... } catch(Exception e) { if (e is ExceptionA){ ... } }
或者把逻辑放在
finally
System.Exception thrownException = null; try{ ... } catch( ExceptionA a) { thrownException = a; ... // do special handling... } catch( ExceptionB b) { thrownException = b; ... // do special handling... } catch(Exception e) { ... } finally{ if ( thrownException != null ) { ... //case the type here or use some other way to identify.. } }
Only if your catch statement throws to another try/catch, for example:
try{ ... try{ ... } catch(ExceptionA a) { throw; } catch(Exception e) { //will not not catch ExceptionA (rethrow or not) } } catch(ExceptionA a) { //this would catch the re-throw } catch( Exception e) { }
Instead why don't you catch the general
Exception
and then case the exception types?try{ ... } catch(Exception e) { if (e is ExceptionA){ ... } }
Or put logic on the
finally
System.Exception thrownException = null; try{ ... } catch( ExceptionA a) { thrownException = a; ... // do special handling... } catch( ExceptionB b) { thrownException = b; ... // do special handling... } catch(Exception e) { ... } finally{ if ( thrownException != null ) { ... //case the type here or use some other way to identify.. } }
相关问答
更多-
我如何从PowerShell中的catch块中获取异常?(How can I Rethrow an exception from catch block in Powershell?)[2019-10-26]
throw关键字在PowerShell中的行为不同于.net实现:在.net中,您只能抛出System.Exceptions本身或其后继者,但在PowerShell中,您可以抛出任何内容并自动将其包装到System.Management.Automation.RuntimeException 。 在这里看到片段。 如果你想重新抛出原始异常,你可以使用throw (最常见),或者throw $_ ,或throw $_.Exception ps:内部catch变量$_本身不是例外,但包含Exception的Sy ... -
第二种方式绝对是更清晰的分析,这是我最看到的。 特定捕获首先发生并且不会触发通用捕获,但如果您没有实现特定捕获,则仍然会有后备。 此外,为了处理多个特定的异常,您还需要更多!(ex is SpecificException)检查。 The second way is definitely cleaner to analyse and it's what I see the most. The specific catch happens first and doesn't trigger the gener ...
-
仅当您的catch语句抛出到另一个try / catch时,例如: try{ ... try{ ... } catch(ExceptionA a) { throw; } catch(Exception e) { //will not not catch ExceptionA (rethrow or not) } } catch(ExceptionA a) { //this would catch t ...
-
这有点主观,但我宁愿忍受在我的代码中处理catch (Exception)的丑陋而不是担心如果有人将错误消息更改为No Data Returned之外的其他内容会发生什么。 This is a little subjective, but I would rather live with the ugliness of handling a catch (Exception) in my code than worry about what happens if someone changes the er ...
-
正确的方法来重新抛出异常(Proper way to rethrow an exception)[2023-02-26]
重新抛出异常的另一种方法(使用throw;如其他答案中所述)是将异常包装在内部异常中。 如MSDN中所述 ,所有自定义异常应该至少有四个构造函数,其中一个是 public InvalidDepartmentException(string message, System.Exception inner) : base(message, inner) { } 因此,如果所有自定义异常都是这样的,您可以将Method3的异常Method3为内部异常: void Method2() { if(error ... -
如果你遇到异常并从中恢复(使用IncrementToNonDuplicate ......),则没有理由再抛出异常。 catch和end try之间的代码应该只是清理资源,如关闭文件或datareader,如果你将重新抛出它。 您可能更愿意返回包含NonDuplicate值的结构以及有关函数中错误的必需信息。 其他方法是抛出一个自定义异常,其中包含“无效数字:它应该......”等信息 If you caught an exception and recovered from it (with your I ...
-
Boost rethrow_exception抛出不同类型的异常(Boost rethrow_exception throws exception of different type)[2022-07-14]
这是设计的。 请参阅http://www.boost.org/doc/libs/1_55_0/libs/exception/doc/frequently_asked_questions.html ,“为什么boost :: exception摘要?” boost :: exception的类型是抽象的,以防止用户“重新抛出”它。 如果用户想要重新抛出,她就会被迫使用像 catch( boost::exception & e ) { e << foo_info(foo); throw; //O ... -
return后的throw永远不会执行。 调用者不会thrown 。 这是一个无法访问的代码 。 它没有任何意义,抛出异常的方法不会向调用者返回任何内容(异常本身除外)。 所以就扔吧。 try { // return something valid } catch(Exception) { throw; } 至于为什么它没有被Visual Studio标记为无法访问,我认为这是一个bug。 编辑: 如果在finally块中抛出异常,则异常会在堆栈中冒泡以查找catch并且不返回该值。 ...
-
编写自定义SQLExceptionTranslator 。 这样,您将直接从数据库直接转换异常。 我不确定具体细节,但以下内容应该可以让你开始: public class CustomSQLExceptionTranslator implements SQLExceptionTranslator{ @Override protected DataAccessException translate(String task, String sql, SQLException ex) { ...
-
你可以做@radoh所说的并且包装成一个RuntimeException ,但是这样做的一个缺点是你的堆栈跟踪现在被污染了,并且会将违规行显示在你声明的地方throw new RuntimeException(ex) 。 另一种方法是使用Lomboks SneakyThrows机制,如下所示: public static void main(String[] args) { methodWithException(); } private static void methodWithExcepti ...