北极星使用文档

官网

GitHub

注册中心对比

服务网格对比

注意:实际使用的版本号,请参考下文

解决的问题

  • 易用:尽量不改变原代码风格或者使用习惯,如feign实现rpc调用等,易于集成到原项目中。

  • 减少重复开发:之前都是手工编码或接第三方依赖如ribbon、sentinel实现负载均衡、熔断限流,代码显得比较臃肿、维护成本高。

  • 可视化配置:业务有时候需要动态放量/减量的流控需求或者调整路由规则需求。

  • 多语言:由于并非所有服务都为java语言,需要支持其他语言如python、go等接入。

安装

下载列表

1
2
3
4
5
6
7
mkdir ~/tools && cd ~/tools
wget https://github.com/polarismesh/polaris/releases/download/v1.12.1/polaris-standalone-release_v1.12.1.darwin.amd64.zip
unzip polaris-standalone-release_v1.12.1.darwin.amd64.zip
polaris-standalone-release_v1.12.1.linux.amd64 ../polaris-standalone
cd ../polaris-standalone/
chmod u+x *install.sh
./install.sh

查看需要暴露的端口

1
cat port.properties

端口

1
2
3
4
5
6
7
8
9
10
polaris_eureka_port=8761
polaris_open_api_port=8090
polaris_service_grpc_port=8091
polaris_config_grpc_port=8093
polaris_prometheus_sd_port=9000
polaris_xdsv3_port=15010
polaris_console_port=8080
polaris_limiter_http_port=8100
polaris_limiter_grpc_port=8101
prometheus_port=9090

防火墙

1
2
firewall-cmd --zone=public --add-port=8090/tcp --add-port=8091/tcp --add-port=8093/tcp --add-port=8080/tcp --add-port=8100/tcp --add-port=8101/tcp --add-port=9090/tcp  --permanent
firewall-cmd --reload

快速入门

SpringCloud 应用接入

版本列表

参考SpringCloudTencent版本列表

Spring Cloud Tencent 版本 Spring Cloud 版本 Spring Boot 版本 Spring Framework 版本 版本发布说明
1.8.0-2021.0.3 (推荐) 2021.0.3 2.6.12 5.3.23 Release Note
1.7.0-2021.0.3 2021.0.3 2.6.12 5.3.23 Release Note

服务注册

maven 依赖

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>polaris</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>polaris-cloud-provider</artifactId>
<properties>
<encoding>utf-8</encoding>
<maven-compiler-plugin.target>8</maven-compiler-plugin.target>
<maven-compiler-plugin.source>8</maven-compiler-plugin.source>
<!-- <spring-cloud-tencent.version>1.8.0-2021.0.3</spring-cloud-tencent.version>-->
<spring-cloud-tencent.version>1.7.0-2021.0.3</spring-cloud-tencent.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
<spring-boot.version>2.6.12</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-dependencies</artifactId>
<version>${spring-cloud-tencent.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>${encoding}</encoding>
<source>${maven-compiler-plugin.source}</source>
<target>${maven-compiler-plugin.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

配置

文件名:application.yml

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
server:
port: 28888
spring:
application:
name: EchoServer
cloud:
polaris:
# 多个用英文','分隔
address: grpc://192.168.14.124:8091
# 本机地址:${spring.cloud.client.ip-address} ≈ 没什么用
#local-ip-address:
# 公共配置:建议单独配置 ≈ 没什么用
namespace: seed
# 公共配置:建议单独配置 ≈ 没什么用
service: EchoServer
discovery:
enabled: true
# 命名空间:可以读取 spring.cloud.polaris.namespace
namespace: seed
# 服务名:以读取 spring.cloud.polaris.service、spring.application.name
service: EchoServer
# polaris 认证
#token:
# 负载
weight: 100
# 版本
#version:
# 协议
protocol: http
# 实例端口:默认server.port、8080
port: ${server.port}
# 仅作为客户端可以设置为false
registerEnabled: true
heartbeat-enabled: true
# 心跳间隔(秒0~60)
heartbeat-interval: 5
# 服务列表刷新时间间隔(毫秒)
service-list-refresh-interval: 60000
# 采集程序
stat:
enabled: true
# Local host for prometheus to pull
# host: 28082
# Port for prometheus to pull.
port: 28082
# Path for prometheus to pull.
path: /metrics

示例代码

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
package com;

import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
* polaris tencent cloud
*
* @author maxzhao
* @since 2022-10-21 11:21
*/
@SpringBootApplication
public class SpringCloudProviderApplication {

@RestController
static class EchoController {

private final PolarisDiscoveryProperties properties;

EchoController(PolarisDiscoveryProperties properties) {
this.properties = properties;
}

@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return "Hello PolarisMesh " + string + ", I'm " + properties.getService();
}
}

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

校验

启动成功后会展示

1
polaris registry, seed EchoServer 192.168.222.1:28888 {} register finished

服务发现

maven依赖

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>polaris</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>polaris-cloud-consumer</artifactId>
<properties>
<encoding>utf-8</encoding>
<maven-compiler-plugin.target>8</maven-compiler-plugin.target>
<maven-compiler-plugin.source>8</maven-compiler-plugin.source>
<!-- <spring-cloud-tencent.version>1.8.0-2021.0.3</spring-cloud-tencent.version>-->
<spring-cloud-tencent.version>1.7.0-2021.0.3</spring-cloud-tencent.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
<spring-boot.version>2.6.12</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-dependencies</artifactId>
<version>${spring-cloud-tencent.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>${encoding}</encoding>
<source>${maven-compiler-plugin.source}</source>
<target>${maven-compiler-plugin.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

配置

文件名:application.yml

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
server:
port: 38888
spring:
application:
name: EchoConsumer
cloud:
polaris:
address: grpc://192.168.14.124:8091
# 本机地址:${spring.cloud.client.ip-address} ≈ 没什么用
#local-ip-address:
# 公共配置:建议单独配置 ≈ 没什么用
namespace: seed
# 公共配置:建议单独配置 ≈ 没什么用
service: EchoConsumer
discovery:
enabled: true
# 命名空间:可以读取 spring.cloud.polaris.namespace
namespace: seed
# 服务名:以读取 spring.cloud.polaris.service、spring.application.name
service: EchoConsumer
# polaris 认证
#token:
# 负载
weight: 100
# 版本
#version:
# 协议
protocol: http
# 实例端口:默认server.port、8080
port: ${server.port}
# 仅作为客户端可以设置为false
registerEnabled: false
heartbeat-enabled: true
# 心跳间隔(秒0~60)
heartbeat-interval: 5
# 服务列表刷新时间间隔(毫秒)
service-list-refresh-interval: 60000

# 采集程序
stat:
enabled: true
# Local host for prometheus to pull
# host: 38082
# Port for prometheus to pull.
port: 38082
# Path for prometheus to pull.
path: /metrics

示例代码

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
60
61
62
63
64
65
66
package com;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
* polaris tencent cloud
*
* @author maxzhao
* @since 2022-10-21 11:21
*/
@EnableFeignClients
@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;
private final EchoServerClient client;

EchoController(RestTemplate template,
EchoServerClient client) {
this.template = template;
this.client = client;
}

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

/**
* 基于 Feign 的调用
*/
@GetMapping(value = "/echo")
public String echo2(@RequestParam(name = "value") String val) {
return client.echo(val);
}
}

/**
* 基于 Feign 的调用
*/
@FeignClient(name = "EchoServer")
public interface EchoServerClient {

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

校验

http://localhost:38888/echo/echo

1
Hello PolarisMesh echo, I'm EchoServer

http://localhost:38888/echo?value=echo2

1
Hello PolarisMesh echo2, I'm EchoServer

动态配置

会默认加载

  • application-default.properties
  • application-default.yml
  • application.properties
  • application.yml
  • bootstrap-default.properties
  • bootstrap-default.yml
  • bootstrap.properties
  • bootstrap.yml

defaultspring.profiles.active 的默认值

maven依赖

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>polaris</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>polaris-cloud-config</artifactId>
<properties>
<encoding>utf-8</encoding>
<maven-compiler-plugin.target>8</maven-compiler-plugin.target>
<maven-compiler-plugin.source>8</maven-compiler-plugin.source>
<!-- <spring-cloud-tencent.version>1.8.0-2021.0.3</spring-cloud-tencent.version>-->
<spring-cloud-tencent.version>1.7.0-2021.0.3</spring-cloud-tencent.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
<spring-boot.version>2.6.12</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-dependencies</artifactId>
<version>${spring-cloud-tencent.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</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-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>${encoding}</encoding>
<source>${maven-compiler-plugin.source}</source>
<target>${maven-compiler-plugin.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

配置

文件名:bootstrap.yml

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
server:
port: 48084
spring:
application:
name: ConfigExample
cloud:
polaris:
# 多个用英文','分隔
address: grpc://192.168.14.124:8091
# 本机地址:${spring.cloud.client.ip-address} ≈ 没什么用
#local-ip-address:
# 命名空间
namespace: seed
# 服务名,也是分组名,决定全局配置的分组 ${spring.application.name}
service: ConfigExample2
config:
# 配置中心的地址列表,配置这个会导致加载速度变慢
#address: ${spring.cloud.polaris.address}
# 配置中心端口
port: 8093
# auto refresh when config file changed
auto-refresh: true
# 与配置中心断连后抛出 IllegalArgumentException 异常
shutdown-if-connect-to-config-server-failed: true
# 本地与远程配置一致时,是否有限加载远程配置
preference: true
# 刷新类型:reflect:通过反射刷新bean,refresh_context:刷新spring context
refresh-type: reflect
groups:
- name: ${spring.application.name} # group name,仅获取当前文件
files: [ "config/user.properties" ]

示例代码

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
package com;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* polaris tencent cloud
*
* @author maxzhao
* @since 2022-10-21 11:21
*/
@SpringBootApplication
public class SpringCloudTencentConfigDemoApplication {

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

@RestController
@RefreshScope
public static class UserController {

@Value("${name}")
private String name;

@GetMapping(value = "/name")
public String name() {
return name;
}
}
}

创建配置

配置分组 菜单下,创建命名空间为 seed的分组 ConfigExample,点击名称进入配置文件管理。

创建名空间为 seed、分组为 **ConfigExample**的文件 config/user.properties,文件格式为 properties

编辑文件 config/user.properties 并写入

1
name=seed

保存并发布。

校验

http://localhost:48084/name

1
seed

配置修改

编辑配置为

1
name=seed@2022

保存并发布。

校验

http://localhost:48084/name

1
seed@2022

本文地址: https://github.com/maxzhao-it/blog/post/eeb07be8/