CSE提供了RPC和RestTemplate两种方式访问服务端,提供了丰富一致的开发治理能力, 如果能够访问第三方就更加方便了。 CSE也提供了访问第三方的机制。 由于CSE访问服务端,需要事先知道契约, 而第三方没有契约; 另外一个问题是第三方的地址不是通过服务中心发现的。因此访问第三方需要解决如下几个问题:
声明契约:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | @Path ( "/rest/it-producer/v1/dataTypeJaxrs" ) interface DataTypeJaxrsSchemaIntf { @Path ( "intPath/{input}" ) @GET int intPath( @PathParam ( "input" ) int input); @Path ( "intQuery" ) @GET int intQuery( @QueryParam ( "input" ) int input); @Path ( "intHeader" ) @GET int intHeader( @HeaderParam ( "input" ) int input); @Path ( "intCookie" ) @GET int intCookie( @CookieParam ( "input" ) int input); @Path ( "intForm" ) @POST int intForm( @FormParam ( "input" ) int input); @Path ( "intBody" ) @POST int intBody( int input); @Path ( "intAdd" ) @GET int intAdd( @QueryParam ( "num1" ) int num1, @QueryParam ( "num2" ) int num2); //strinnum1 @Path ( "stringPath/{input}" ) @GET String stringPath( @PathParam ( "input" ) String input); @Path ( "stringQuery" ) @GET String stringQuery( @QueryParam ( "input" ) String input); @Path ( "stringHeader" ) @GET String stringHeader( @HeaderParam ( "input" ) String input); @Path ( "stringCookie" ) @GET String stringCookie( @CookieParam ( "input" ) String input); @Path ( "stringForm" ) @POST String stringForm( @FormParam ( "input" ) String input); @Path ( "stringBody" ) @POST String stringBody(String input); @Path ( "stringConcat" ) @GET String stringConcat( @QueryParam ( "str1" ) String str1, @QueryParam ( "str2" ) String str2); } |
声明/注册访问地址和协议
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | String endpoint = "rest:23.3.3.3:8080?sslEnabled=false" ; RegistryUtils.getServiceRegistry() .registerMicroserviceMappingByEndpoints( THIRD_PARTY_MICROSERVICE_NAME, "1.2.1" , Collections.singletonList(endpoint), DataTypeJaxrsSchemaIntf. class ); MicroserviceInstance instance = new MicroserviceInstance(); instance.setEndpoints(Collections.singletonList(endpoint)); RegistryUtils.getServiceRegistry() .registerMicroserviceMapping( ASYNC_THIRD_PARTY_MICROSERVICE_NAME, "1.1.1" , Collections.singletonList(instance), DataTypeJaxrsSchemaAsyncIntf. class ); |
这样就可以了。
详细代码参考:
https://github.com/apache/servicecomb-java-chassis/blob/master/integration-tests/it-consumer/src/main/java/org/apache/servicecomb/it/testcase/thirdparty/Test3rdPartyInvocation.java
使用这个功能,推荐使用CSE 2.3.62版本,详细参考Rlease Notes: https://support.huaweicloud.com/productdesc-cse/cse_productdesc_0008.html
开发指南参考: https://docs.servicecomb.io/java-chassis/zh_CN/build-consumer/3rd-party-service-invoke.html