服务注册

初始化项目

使用 jetbrain idea 等工具初始化一个 maven 项目

引入依赖

在上一步初始化好一个 maven 项目之后,我们在 pom.xml 中引入 Spring Boot Polaris 相关依赖。

  • 引入 spring-boot-polaris-dependencies 进行管理 Spring Boot Polaris 相关组件的依赖版本。
  • 引入 spring-boot-polaris-discovery-starter 实现 Spring Boot 服务注册到北极星中。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.4.3</version>
  10. <relativePath/> <!-- lookup parent from repository -->
  11. </parent>
  12. ...
  13. <dependencyManagement>
  14. <dependencies>
  15. <dependency>
  16. <groupId>com.tencent.polaris</groupId>
  17. <artifactId>spring-boot-polaris-dependencies</artifactId>
  18. <version>1.1.0</version>
  19. <type>pom</type>
  20. <scope>import</scope>
  21. </dependency>
  22. </dependencies>
  23. </dependencyManagement>
  24. <dependencies>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <!-- 引入 Spring Boot Polaris Discovery 依赖用于实现服务注册 -->
  30. <dependency>
  31. <groupId>com.tencent.polaris</groupId>
  32. <artifactId>spring-boot-polaris-discovery-starter</artifactId>
  33. </dependency>
  34. </dependencies>
  35. ...
  36. </project>

配置 application.properties

在 resources 目录下创建 application.properties 文件,并按照如下进行配置

  1. .
  2. ├── java
  3. └── com
  4. └── example
  5. └── springbootpolarisprovider
  6. └── SpringbootProviderApplication.java
  7. └── resources
  8. └── application.properties
  1. server.port=28888
  2. spring.application.name=BootEchoServer
  3. polaris.address=grpc://127.0.0.1:8091
  4. polaris.discovery.register.enable=true

示例代码

  1. ```java
  2. @SpringBootApplication
  3. public class SpringbootProviderApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(SpringbootProviderApplication.class, args);
  6. }
  7. @RestController
  8. static class EchoController {
  9. private final PolarisDiscoveryProperties properties;
  10. EchoController(PolarisDiscoveryProperties properties) {
  11. this.properties = properties;
  12. }
  13. @GetMapping(value = "/echo/{string}")
  14. public String echo(@PathVariable String string) {
  15. return "Hello PolarisMesh " + string + ", I'm " + properties.getApplicationName();
  16. }
  17. }
  18. }

验证

启动 Spring Boot 应用

在 Spring Boot 启动日志中,找到如下日志信息, 则表示 Spring Boot 应用已经成功注册到北极星中。

  1. [Polaris] success to register instance 127.0.0.1:28888, service is BootEchoServer, namespace is default
  2. [Polaris] success to schedule heartbeat instance 127.0.0.1:28888, service is BootEchoServer, namespace is default

可以通过 curl 命令查询服务端是否有该实例。

  1. curl --location --request POST '127.0.0.1:8090/v1/Discover' \
  2. --header 'Content-Type: application/json' \
  3. --data-raw '{
  4. "type": 1,
  5. "service": {
  6. "name": "BootEchoServer",
  7. "namespace": "default"
  8. }
  9. }'