服务发现

项目初始化

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

引入依赖

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

  • 引入 spring-cloud-tencent-dependencies 进行管理 Spring Cloud Tencent 相关组件的依赖版本。
  • 引入 spring-cloud-starter-tencent-polaris-discovery 实现从北极星中发现服务。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.9</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    ...

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.tencent.cloud</groupId>
                <artifactId>spring-cloud-tencent-dependencies</artifactId>
                <version>1.7.0-2021.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- 简单的 Spring Cloud Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 引入 Spring Cloud Tencent 的服务注册发现依赖 -->
        <dependency>
            <groupId>com.tencent.cloud</groupId>
            <artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
        </dependency>
        <!-- 引入 Feign 依赖实现 Feign 调用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

    ...

</project>

配置 application.yml

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

.
├── java
│   └── com
│       └── example
│           └── spingcloudpolarisconsumer
│               └── SpringCloudConsumerApplication.java
└── resources
    └── application.yml

配置 application.yml

server:
  port: 38888
spring:
  application:
    name: EchoConsumer

  cloud:
    polaris:
      namespace: default # 配置注册命名空间
      address: grpc://127.0.0.1:8091
      discovery:
        enabled: true
      stat:
        enabled: true
        port: 38082

示例代码

基于 RestTemplate 的服务发现调用

@SpringBootApplication
public class SpringCloudConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConsumerApplication.class, args);
    }

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RestController
    class EchoController {

        private final RestTemplate template;

        EchoController(RestTemplate template) {
            this.template = template;
        }

        @RequestMapping(value = "/echo", method = RequestMethod.GET)
        public String echo(@RequestParam(name = "value") String val) {
            return template.getForObject("http://EchoServer/echo/" + val, String.class);
        }
    }
}

基于 Feign 的服务发现调用

@SpringBootApplication
@EnableFeignClients
public class SpringCloudPolarisConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudPolarisConsumerApplication.class, args);
    }

    @RestController
    static class EchoController {

        private final EchoServerClient client;

        EchoController(EchoServerClient client) {
            this.client = client;
        }

        @GetMapping(value = "/echo")
        public String echo(@RequestParam(name = "value") String val) {
            return client.echo(val);
        }
    }

    @FeignClient(name = "EchoServer")
    public interface EchoServerClient {

        @GetMapping("/echo/{value}")
        String echo(@PathVariable("value") String value);
    }
}

验证

通过 curl 命令对服务消费者发起调用。

curl --location --request GET '127.0.0.1:38888/echo?value=SCT'

预期的结果如下

Hello PolarisMesh SCT, I'm EchoServer