HSF 服务依赖
使用 Spring Boot 编程模型结合 Pandora Boot开发HSF服务时,需要使用到的依赖(若使用XML配置文件方式,可以不添加),[backcolor=transparent]可选。
- [backcolor=transparent] [backcolor=transparent]<dependency>
- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework.cloud[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]spring-cloud-starter-hsf[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]</dependency>
RESTful 服务依赖
为 Spring Cloud 开发的 RESTful 应用在 EDAS 中添加服务注册与发现的支持,[backcolor=transparent]必选。 EDAS 中 Spring Cloud 服务注册与发现功能目前只支持自研的 VIPServer。
-
[backcolor=transparent]
[backcolor=transparent]
<dependency>
- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework.cloud[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]spring-cloud-starter-vipclient[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]</dependency>[backcolor=transparent]
为 Spring Cloud 开发的 RESTful 应用在 EDAS 中服务添加链路跟踪的依赖。[backcolor=transparent]可选,添加后才能使用 服务监控 功能。
-
[backcolor=transparent]
[backcolor=transparent]
<dependency>
- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework.cloud[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]spring-cloud-starter-eagleeye[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]</dependency>
为 Spring Cloud 开发的 RESTful 应用在 EDAS 中服务添加限流支持的依赖。[backcolor=transparent]可选,添加后才能使用 限流降级 功能。
-
[backcolor=transparent]
[backcolor=transparent]
<dependency>
- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework.cloud[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]spring-cloud-starter-sentinel[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]</dependency>
为 Spring Cloud 开发的 RESTful 应用在 EDAS 中服务添加服务调用鉴权的依赖,[backcolor=transparent]可选。
-
[backcolor=transparent]
[backcolor=transparent]
<dependency>
- [backcolor=transparent] [backcolor=transparent]<groupId>[backcolor=transparent]org.springframework.cloud[backcolor=transparent]</groupId>
- [backcolor=transparent] [backcolor=transparent]<artifactId>[backcolor=transparent]spring-cloud-starter-dauth[backcolor=transparent]</artifactId>
- [backcolor=transparent] [backcolor=transparent]</dependency>[backcolor=transparent]
[backcolor=transparent]注意:若应用没有依赖 spring-boot-starter-web ,将不会引入嵌入式 Tomcat ,导致 EDAS 页面上面的一些 Tomcat 相关参数及健康检查无法使用。
HSF 服务开发
全局配置
[backcolor=transparent]注意: 全局配置只针对于采用 Spring Boot 编程模型在 Pandora Boot 应用中使用 EDAS 服务时有用,针对 Spring XML 配置文件发布/订阅服务方式无效。
使用方式:在 application.properties 添加 EDAS 服务的版本信息和客户端超时信息, 样例配置如下([backcolor=transparent]建议都默认添加):
- [backcolor=transparent]spring[backcolor=transparent].[backcolor=transparent]hsf[backcolor=transparent].[backcolor=transparent]group[backcolor=transparent]=[backcolor=transparent]HSF
- [backcolor=transparent]spring[backcolor=transparent].[backcolor=transparent]hsf[backcolor=transparent].[backcolor=transparent]version[backcolor=transparent]=[backcolor=transparent]1.0[backcolor=transparent].[backcolor=transparent]0
- [backcolor=transparent]spring[backcolor=transparent].[backcolor=transparent]hsf[backcolor=transparent].[backcolor=transparent]timeout[backcolor=transparent]=[backcolor=transparent]3000
若未添加,则默认属性:group=HSF, version=1.0.0. DAILY, timeout=3000
启动代码
- [backcolor=transparent]@SpringBootApplication
- [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]HSFBootDemoProviderApplication[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] main[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent][][backcolor=transparent] args[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]// 启动Pandora Boot 用于加载 Pandora 容器
- [backcolor=transparent] [backcolor=transparent]PandoraBootstrap[backcolor=transparent].[backcolor=transparent]run[backcolor=transparent]([backcolor=transparent]args[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 启动Spring Boot
- [backcolor=transparent] [backcolor=transparent]SpringApplication[backcolor=transparent] springApplication[backcolor=transparent]=[backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]SpringApplication[backcolor=transparent]([backcolor=transparent]HSFBootDemoProviderApplication[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent]);
- [backcolor=transparent] springApplication[backcolor=transparent].[backcolor=transparent]run[backcolor=transparent]([backcolor=transparent]args[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 标记服务启动完成,并设置线程 wait。防止用户业务代码运行完毕退出后,导致容器退出。
- [backcolor=transparent] [backcolor=transparent]PandoraBootstrap[backcolor=transparent].[backcolor=transparent]markStartupAndWait[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent]}
说明:这里主要添加了 PandoraBootstrap 相关代码,主要用途是为了启动 Pandora 容器相关插件。
服务发布
配置发布
编写服务配置
-
[backcolor=transparent]
[backcolor=transparent]
<!-- 提供一个服务示例,interface 表示需要公布的服务接口,ref 是具体该接口实现的服务-->
- [backcolor=transparent] [backcolor=transparent]<hsf:provider[backcolor=transparent] [backcolor=transparent]id[backcolor=transparent]=[backcolor=transparent]"hSFBootDemoApi"
- [backcolor=transparent] [backcolor=transparent]interface[backcolor=transparent]=[backcolor=transparent]"com.aliware.edas.hsfboot.api.VersionInfoApi"[backcolor=transparent] [backcolor=transparent]ref[backcolor=transparent]=[backcolor=transparent]"versionInfoApiImpl"[backcolor=transparent] [backcolor=transparent]/>
导入配置(在 Spring Boot Application 的上添加 @ImportResource)
-
[backcolor=transparent]
[backcolor=transparent]
@SpringBootApplication
- [backcolor=transparent] [backcolor=transparent]@ImportResource[backcolor=transparent]([backcolor=transparent]locations [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]{[backcolor=transparent]"classpath:hsf-beans.xml"[backcolor=transparent]})
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]HSFBootDemoProviderApplication
注解发布
注解方式符合 Spring Boot 编程风格,只需要通过简单几个注解即可完成服务发布。
编写 HSF 服务, 在发布的服务实现上添加 @HSFProvider, 其中 serviceInterface 是要发布服务的接口,如下
-
[backcolor=transparent]
[backcolor=transparent]
@HSFProvider[backcolor=transparent]
([backcolor=transparent]
serviceInterface [backcolor=transparent]
=
- [backcolor=transparent] [backcolor=transparent]VersionInfoApi[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent],[backcolor=transparent]serviceGroup[backcolor=transparent]=[backcolor=transparent]"HSF"[backcolor=transparent],[backcolor=transparent]clientTimeout[backcolor=transparent]=[backcolor=transparent]3000[backcolor=transparent],[backcolor=transparent]serviceVersion[backcolor=transparent]=[backcolor=transparent]"1.0.0"[backcolor=transparent])
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]VersionInfoApiImpl[backcolor=transparent] [backcolor=transparent]implements[backcolor=transparent] [backcolor=transparent]VersionInfoApi
服务订阅
配置订阅
编写服务配置
-
[backcolor=transparent]
[backcolor=transparent]
<!-- 提供一个服务示例 interface 表示需要订阅的服务接口 -->
- [backcolor=transparent] [backcolor=transparent]<hsf:consumer[backcolor=transparent] [backcolor=transparent]id[backcolor=transparent]=[backcolor=transparent]"versionInfoApi"[backcolor=transparent] [backcolor=transparent]interface[backcolor=transparent]=[backcolor=transparent]"com.aliware.edas.hsfboot.api.VersionInfoApi"[backcolor=transparent]></hsf:consumer>
导入配置(在 Spring Boot Application 的上添加 @ImportResource)
-
[backcolor=transparent]
[backcolor=transparent]
@SpringBootApplication
- [backcolor=transparent] [backcolor=transparent]@ImportResource[backcolor=transparent]([backcolor=transparent]locations [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]{[backcolor=transparent]"classpath:hsf-beans.xml"[backcolor=transparent]})
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]HSFBootDemoConsumerApplication
使用
-
[backcolor=transparent]
[backcolor=transparent]
@Autowired
- [backcolor=transparent] [backcolor=transparent]private[backcolor=transparent] [backcolor=transparent]VersionInfoApi[backcolor=transparent] versionInfoApi[backcolor=transparent];
注解订阅
注解方式符合 Spring Boot 编程风格,只需要通过简单几个注解即可完成服务订阅。
通过 @HSFConsumer 注入需要消费的 interface,如下
-
[backcolor=transparent]
[backcolor=transparent]
@HSFConsumer[backcolor=transparent]
([backcolor=transparent]
serviceGroup[backcolor=transparent]
=[backcolor=transparent]
"HSF"[backcolor=transparent]
,[backcolor=transparent]
clientTimeout[backcolor=transparent]
=[backcolor=transparent]
3000[backcolor=transparent]
,[backcolor=transparent]
serviceVersion[backcolor=transparent]
=[backcolor=transparent]
"1.0.0"[backcolor=transparent]
)
- [backcolor=transparent] [backcolor=transparent]private[backcolor=transparent] [backcolor=transparent]VersionInfoApi[backcolor=transparent] versionInfoApi[backcolor=transparent];
[backcolor=transparent]最佳实践:在 Config 类里配置一次 @HSFConsumer ,然后在多处 @Autowired 注入使用通常一个 HSF Consumer 需要在多个地方使用,但并不需要在每次使用的地方都用@HSFConsumer 来标记。只需要写一个统一个 Config 类,然后在其它需要使用的地方,直接 @Autowired 注入即可。
@Configuration public class HsfConfig {
- [backcolor=transparent]/**
- [backcolor=transparent] * 版本接口,这里只需要一个注解,就完成了服务订阅,在使用的地方,直接@Autowired即可使用。
- [backcolor=transparent]* 注意:配置方式订阅服务方式不需要写该注解
- [backcolor=transparent] */
- [backcolor=transparent]@HSFConsumer[backcolor=transparent]([backcolor=transparent]serviceGroup[backcolor=transparent]=[backcolor=transparent]"HSF"[backcolor=transparent],[backcolor=transparent]clientTimeout[backcolor=transparent]=[backcolor=transparent]3000[backcolor=transparent],[backcolor=transparent]serviceVersion[backcolor=transparent]=[backcolor=transparent]"1.0.0"[backcolor=transparent])
- [backcolor=transparent]private[backcolor=transparent] [backcolor=transparent]VersionInfoApi[backcolor=transparent] versionInfoApi[backcolor=transparent];
- [backcolor=transparent]}
[backcolor=transparent]说明:
- 如果在 application.proeprteis 中定义了全局配置, 那么 serviceGroup 和 serviceVersion 是不需要指定的。
@HSFProvider 、@HSFConsumer 中的 serviceGroup 和 serviceVersion 以及其他 String 类型的配置都可以支持 Spring 的 property placeholder, 如:
-
[backcolor=transparent]
[backcolor=transparent]
@HSFProvider[backcolor=transparent]
([backcolor=transparent]
serviceInterface [backcolor=transparent]
=[backcolor=transparent]
[backcolor=transparent]
VersionInfoApi[backcolor=transparent]
.[backcolor=transparent]
class[backcolor=transparent]
,[backcolor=transparent]
serviceGroup [backcolor=transparent]
=[backcolor=transparent]
[backcolor=transparent]
"${service.group.name}"[backcolor=transparent]
)
- [backcolor=transparent] [backcolor=transparent]@HSFConsumer[backcolor=transparent]([backcolor=transparent]serviceGroup[backcolor=transparent]=[backcolor=transparent]"${service.group.name}"[backcolor=transparent],[backcolor=transparent]clientTimeout[backcolor=transparent]=[backcolor=transparent]3000[backcolor=transparent])
单元测试
Pandora Boot 应用的单元测试可以通过 PandoraBootRunner 启动,并与 SpringJUnit4ClassRunner 无缝集成
代码编写
-
[backcolor=transparent]
[backcolor=transparent]
@RunWith[backcolor=transparent]
([backcolor=transparent]
PandoraBootRunner[backcolor=transparent]
.[backcolor=transparent]
class[backcolor=transparent]
)
- [backcolor=transparent] [backcolor=transparent]@DelegateTo[backcolor=transparent]([backcolor=transparent]SpringJUnit4ClassRunner[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent])
- [backcolor=transparent] [backcolor=transparent]// 加载测试需要的类,一定要加入Spring Boot的启动类,其次需要加入本类
- [backcolor=transparent] [backcolor=transparent]@SpringBootTest[backcolor=transparent]([backcolor=transparent]classes [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]{[backcolor=transparent] [backcolor=transparent]HSFBootDemoProviderApplication[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]VersionInfoTest[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]})
- [backcolor=transparent] [backcolor=transparent]@Component
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]VersionInfoTest[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]//当这里使用 @HSFConsumer 时,一定要在 @SpringBootTest 类加载中,加载本类,通过本类来注入对象,否则当做泛化时,会报类转换异常
- [backcolor=transparent] [backcolor=transparent]@HSFConsumer[backcolor=transparent]([backcolor=transparent]generic[backcolor=transparent] [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]true[backcolor=transparent])
- [backcolor=transparent] [backcolor=transparent]VersionInfoApi[backcolor=transparent] versionApi[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]@Test
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] testInvoke[backcolor=transparent]()[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"#####:"[backcolor=transparent]+[backcolor=transparent]versionApi[backcolor=transparent].[backcolor=transparent]getVersionInfo[backcolor=transparent]());
- [backcolor=transparent] [backcolor=transparent]TestCase[backcolor=transparent].[backcolor=transparent]assertEquals[backcolor=transparent]([backcolor=transparent]"1.0.0"[backcolor=transparent],[backcolor=transparent] versionApi[backcolor=transparent].[backcolor=transparent]getVersionInfo[backcolor=transparent]());
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]@Test
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] testGenericInvoke[backcolor=transparent]()[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]GenericService[backcolor=transparent] service [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]GenericService[backcolor=transparent])[backcolor=transparent] versionApi[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]Object[backcolor=transparent] result [backcolor=transparent]=[backcolor=transparent] service[backcolor=transparent].[backcolor=transparent]$invoke[backcolor=transparent]([backcolor=transparent]"getVersionInfo"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent][][backcolor=transparent] [backcolor=transparent]{},[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]Object[backcolor=transparent][][backcolor=transparent] [backcolor=transparent]{});
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"####:"[backcolor=transparent]+[backcolor=transparent]result[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]TestCase[backcolor=transparent].[backcolor=transparent]assertEquals[backcolor=transparent]([backcolor=transparent]"1.0.0"[backcolor=transparent],[backcolor=transparent] result[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]@Test
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] testMock[backcolor=transparent]()[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]VersionInfoApi[backcolor=transparent] mock [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]Mockito[backcolor=transparent].[backcolor=transparent]mock[backcolor=transparent]([backcolor=transparent]VersionInfoApi[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]AdditionalAnswers[backcolor=transparent].[backcolor=transparent]delegatesTo[backcolor=transparent]([backcolor=transparent]versionApi[backcolor=transparent]));
- [backcolor=transparent] [backcolor=transparent]Mockito[backcolor=transparent].[backcolor=transparent]when[backcolor=transparent]([backcolor=transparent]mock[backcolor=transparent].[backcolor=transparent]getVersionInfo[backcolor=transparent]()).[backcolor=transparent]thenReturn[backcolor=transparent]([backcolor=transparent]"V1.9.beta"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]TestCase[backcolor=transparent].[backcolor=transparent]assertEquals[backcolor=transparent]([backcolor=transparent]"V1.9.beta"[backcolor=transparent],[backcolor=transparent] mock[backcolor=transparent].[backcolor=transparent]getVersionInfo[backcolor=transparent]());
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]}