官网
GitHub
注册中心对比
服务网格对比
注意:实际使用的版本号,请参考下文
解决的问题
易用 :尽量不改变原代码风格或者使用习惯,如feign实现rpc调用等,易于集成到原项目中。
减少重复开发 :之前都是手工编码或接第三方依赖如ribbon、sentinel实现负载均衡、熔断限流,代码显得比较臃肿、维护成本高。
可视化配置 :业务有时候需要动态放量/减量的流控需求或者调整路由规则需求。
多语言 :由于并非所有服务都为java语言,需要支持其他语言如python、go等接入。
安装 下载列表
1 2 3 4 5 6 7 mkdir ~/tools && cd ~/toolswget 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 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.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 > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <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 namespace: seed service: EchoServer discovery: enabled: true namespace: seed service: EchoServer weight: 100 protocol: http port: ${server.port} registerEnabled: true heartbeat-enabled: true heartbeat-interval: 5 service-list-refresh-interval: 60000 stat: enabled: true port: 28082 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;@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.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 > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > com.tencent.cloud</groupId > <artifactId > spring-cloud-starter-tencent-polaris-discovery</artifactId > </dependency > <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 namespace: seed service: EchoConsumer discovery: enabled: true namespace: seed service: EchoConsumer weight: 100 protocol: http port: ${server.port} registerEnabled: false heartbeat-enabled: true heartbeat-interval: 5 service-list-refresh-interval: 60000 stat: enabled: true port: 38082 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;@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); } @GetMapping(value = "/echo") public String echo2 (@RequestParam(name = "value") String val) { return client.echo(val); } } @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
default
为 spring.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.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 > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <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 namespace: seed service: ConfigExample2 config: port: 8093 auto-refresh: true shutdown-if-connect-to-config-server-failed: true preference: true refresh-type: reflect groups: - name: ${spring.application.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;@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
并写入
保存并发布。
校验 http://localhost:48084/name
配置修改 编辑配置为
保存并发布。
校验 http://localhost:48084/name
本文地址: https://github.com/maxzhao-it/blog/post/eeb07be8/