0%

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
import okhttp3.*;
public class TestHttpClient {
@Test
public void test(){
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(Duration.ofSeconds(100))
.readTimeout(Duration.ofSeconds(100))
.writeTimeout(Duration.ofSeconds(100))
.build();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "code=1");
Request request = new Request.Builder()
.url("https://127.0.0.1:1111/post")
.method("POST", body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
try {
Response response = client.newCall(request).execute();
ResponseBody responseBody = response.body();
if (response.code() != 200 || responseBody == null) {
System.out.println("nothing " + startNum);
return;
}
String string = responseBody.string();
} catch (IOException e) {

}
}
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.security.MessageDigest;
import static org.apache.commons.codec.digest.MessageDigestAlgorithms.MD5;
public class TestFileClient {
@Test
public void test(){
InputStream is = new FileInputStream(new File("/data/source.txt"));
MessageDigest messageDigest = MessageDigest.getInstance(MD5);
/*输入流转输出流*/
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
messageDigest.update(buffer, 0, len);
}
String md5 = (new String(Hex.encodeHex(messageDigest.digest())));
}
}

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

使用Selenium 可以操作 浏览器的行为,并修改界面元素。

官方文档比较齐全
官方示例需要跑通
文档地址: 入门指南
options 参考地址:chromium-command-line-switches
chrome 功能特性:chromedriver

下载

示例

maven 依赖

1
2
3
4
5
6
<!--操作浏览器-->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.9.1</version>
</dependency>

管理浏览器驱动的第三方组件,下面的示例没有使用

1
2
3
4
5
6
<!--管理浏览器驱动-->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.3.3</version>
</dependency>

官方 chrome 示例

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
import cn.hutool.core.lang.Assert;
import io.github.bonigarcia.wdm.WebDriverManager;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;

import java.time.Duration;
import java.util.*;
import java.util.NoSuchElementException;

public class SeleniumTest {
@Test
public void chrome() {
/*管理驱动*/
System.setProperty("webdriver.chrome.driver", "D:\\develop\\selenium-server\\chromedriver-113.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--remote-allow-origins=*");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.selenium.dev/selenium/web/web-form.html");

String title = driver.getTitle();
Assert.equals("Web form", title);
driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));

WebElement textBox = driver.findElement(By.name("my-text"));
WebElement submitButton = driver.findElement(By.cssSelector("button"));

textBox.sendKeys("Selenium");
submitButton.click();

WebElement message = driver.findElement(By.id("message"));
String value = message.getText();
Assert.equals("Received!", value);
driver.quit();
}
}

edge 启动

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
import cn.hutool.core.lang.Assert;
import io.github.bonigarcia.wdm.WebDriverManager;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;

import java.time.Duration;
import java.util.*;
import java.util.NoSuchElementException;

public class SeleniumTest {
@Test
public void edge() {
/*管理驱动*/
System.setProperty("webdriver.edge.driver", "D:\\develop\\selenium-server\\msedgedriver-113.0.1774.3.exe");
EdgeOptions edgeOptions = new EdgeOptions();
/*如果不配这里,启动会报错*/
edgeOptions.addArguments("--remote-allow-origins=*");
/*配置默认浏览的大小*/
edgeOptions.addArguments("--window-size=200,200");
/*配置浏览器的属性*/
Map<String, Object> prefs = new HashMap<>();
prefs.put("profile.default_content_settings.popups", 0);
/*浏览器默认允许下载多个*/
prefs.put("profile.default_content_setting_values.automatic_downloads", 1);
/*浏览器默认下载路径*/
prefs.put("download.default_directory", "E:\\Downloads");
edgeOptions.setExperimentalOption("prefs", prefs);
EdgeDriver driver = new EdgeDriver(edgeOptions);
/*打开页面*/
driver.get("https://www.selenium.dev/selenium/web/web-form.html");
/*获取当前tab页面的key*/
String thisWindowHandle = driver.getWindowHandle();
/*打开新标签页并切换到新标签页*/
driver.switchTo().newWindow(WindowType.TAB);
/*关闭标签页或窗口*/
driver.close();
/*打开一个新窗口并切换到新窗口*/
driver.switchTo().newWindow(WindowType.WINDOW);
/*获取所有tab*/
for (String windowHandle : driver.getWindowHandles()) {
/*切换tab*/
driver.switchTo().window(windowHandle);
}
/*重新打开url*/
driver.get("https://www.selenium.dev/selenium/web/web-form.html");
/*执行脚本*/
driver.executeScript("arguments[0].click();", button);
/*执行脚本2*/
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", button);
/*退出浏览*/
driver.quit();
}
}

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

需要安装 net-tools

查看网络

1
ifconfig

我这里的网络为 ens33

编辑网络

1
sudo vi /etc/sysconfig/network-scripts/ifcfg-ens33

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static #开机协议,有dhcp及static;
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=ens33
UUID=d8a65e65-884f-4a7e-b1c3-3bf192de75c0
DEVICE=ens33
ONBOOT=yes #设置为开机启动;
DNS1=114.114.114.114 #这个是国内的DNS地址,是固定的;
IPADDR=192.168.2.2 #你想要设置的固定IP,理论上192.168.2.2-255之间都可以,请自行验证;
NETMASK=255.255.255.0 #子网掩码,不需要修改;
GATEWAY=192.168.2.254 #网关

如果使用虚拟机:这里应该和你“2.配置虚拟机的NAT模式具体地址参数”中的(2)选择VMnet8–取消勾选使用本地DHCP–设置子网IP–网关IP设置 一样才行。

如果无法联网,请查看网关,如果是虚拟机,则查看有没有网关。

重启网略

1
sudo systemctl restart NetworkManager

测试网络

1
ping www.baidu.com

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

官网

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/

数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"position": "122_269_154_298",
"value": [
[
"A",
0.6103852987289429
],
[
"B",
0.5880608558654785
],
[
"C",
0.5793392658233643
]
]
}

结果

1
2
3
4
5
6
7
8
9
{
"position" : "122_269_154_298",
"result0" : "A",
"score0" : 0.6103852987289429,
"result1" : "B",
"score1" : 0.5880608558654785,
"result2" : "C",
"score2" : 0.5793392658233643
}

jolt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[
{
"operation": "shift",
"spec": {
"position": "position",
"value": {
"*": {
"0": "result&1",
"1": "score&1"
}
}
}
}
]

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

测试网站:jolt-test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[
{
"operation": "shift",
"spec": {
"*": {
// #2 means go three levels up the tree (count from 0),
// and ask the "ratings" node, how many of it's
// children have been matched.
//
// This allows us to put the Name and the Value into
// the same object in the Ratings array.
"$": "[#2].key",
"@": "[#2].value"
}
}
}
]

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