#Spring Cloud using zookeeper as a service registration center and configuration center
Install zookeeper
-Download
-Unzip
tar -xvf zookeeper-3.4.10.tar.gz
-Start zookeeper
cd zookeeper-3.4.10
cd conf
cp zoo_sample.cfg zoo.cfg
cd ../bin
sh zkServer.sh start
Use zookeeper as the service registry
maven dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
Add annotation @EnableDiscoveryClient to the startup class
package com.garlic.springcloudzookeeperclientapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* zookeeper作为服务注册中心,应用启动类
*/
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudZookeeperClientAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudZookeeperClientAppApplication.class, args);
}
}
Add configuration file application.properties
1 配置应用名称
spring.application.name=spring-cloud-zookeeper-client-app
2 配置服务端口
server.port=8080
3 关闭安全控制
management.security.enabled=false
配置zookeeper地址
spring.cloud.zookeeper.connect-string=localhost:2181
Use DiscoveryClient to get a list of registered services
package com.garlic.springcloudzookeeperclientapp.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* 提供Rest Api,根据实例名称获取注册服务列表
*/
@RestController
@RequestMapping("/zookeeper")
public class ZookeeperController {
@Value("${spring.application.name}")
private String instanceName
private final DiscoveryClient discoveryClient;
@Autowired
public ZookeeperController(DiscoveryClient discoveryClient) {
this.discoveryClient = discoveryClient;
}
@GetMapping
public String hello() {
return "Hello,Zookeeper.";
}
@GetMapping("/services")
public List<String> serviceUrl() {
List<ServiceInstance> list = discoveryClient.getInstances(instanceName);
List<String> services = new ArrayList<>();
if (list != null && list.size() > 0 ) {
list.forEach(serviceInstance -> {
services.add(serviceInstance.getUri().toString());
});
}
return services;
}
}
Note: You can start different instances. Here I started two instances of port 8080 and 8081, and then use the endpoint to query the list of registered services
You can also query the list of registered services through zookeeper related commands
sh zkCli.sh
[services, zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /
[services, zookeeper]
[zk: localhost:2181(CONNECTED) 2] ls /services
[spring-cloud-zookeeper-client-app]
[zk: localhost:2181(CONNECTED) 3] ls /services/spring-cloud-zookeeper-client-app
[be61af3d-ffc2-4ffc-932c-26bc0f94971c, bcf21ece-e9e1-4a91-b985-8828688370b8]
[zk: localhost:2181(CONNECTED) 4]
Use zookeeper as the configuration center
- Use zkCli to create configuration information
[zk: localhost:2181(CONNECTED) 27] create /config ""
Created /config
[zk: localhost:2181(CONNECTED) 28] create /config ""
Created /config/garlic
[zk: localhost:2181(CONNECTED) 29] create /config/garlic/name "default"
Created /config/garlic/name
[zk: localhost:2181(CONNECTED) 30] set /config/garlic-dev/name "dev"
Node does not exist: /config/garlic-dev/name
[zk: localhost:2181(CONNECTED) 31] create /config/garlic-dev/name "dev"
Created /config/garlic-dev/name
[zk: localhost:2181(CONNECTED) 32] create /config/garlic-test/name "test"
Created /config/garlic-test/name
[zk: localhost:2181(CONNECTED) 33] create /config/garlic-prod/name "prod"
Add maven dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-config</artifactId>
</dependency>
bootstrap.properties
启用zookeeper作为配置中心
spring.cloud.zookeeper.config.enabled = true
配置根路径
spring.cloud.zookeeper.config.root = config
配置默认上下文
spring.cloud.zookeeper.config.defaultContext = garlic
配置profile分隔符
spring.cloud.zookeeper.config.profileSeparator = -
application.properties
配置应用名称
spring.application.name=spring-cloud-zookeeper-config-app
配置服务端口
server.port=10000
关闭安全控制
management.security.enabled=false
spring.profiles.active=dev
Write Controller to dynamically obtain the data of the zookeeper configuration center
package com.garlic.springcloudzookeeperconfigapp.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 提供Rest Api,获取配置在zookeeper中的配置信息
*/
@RestController
@RequestMapping("/zookeeper")
@RefreshScope // 必须添加,否则不会自动刷新name的值
public class ZookeeperController {
@Autowired
private Environment environment;
@Value("${name}")
private String name;
@GetMapping
public String hello() {
return "Hello, " + name;
}
@GetMapping("/env")
public String test() {
String name = environment.getProperty("name");
System.out.println(name);
return "Hello," + name;
}
}
K8s cluster solution
-
Single machine-above 16G
One master, two working nodes
-
Single machine-above 8G
One master + work base point, one work node
-
Multiple hosts-4G
A host starts a virtual machine and bridges the network
Configure the cluster environment
-
Clone centos-7-1908: k1
-
Set cpu and memory
cpu - 2
RAM-2G
The third option is to set the network as a bridged network
-
Set ip
./ip-static ip: 192.168.64.191 第三个方案,用自动获取ip ./ip-dhcp
-
upload files
- Upload the two files easzup and images.gz to /root/
- upload the ansible folder to /etc/
-
Execute the following command
# 对easzup文件设置执行权限 chmod +x ./easzup # 下载离线安装文件,并安装配置docker, # 如果离线文件已经存在则不会重复下载, # 离线安装文件存放路径: /etc/ansible ./easzup -D # 启动kubeasz工具使用的临时容器 ./easzup -S # 进入该容器 docker exec -it kubeasz sh # 下面命令在容器内执行 # 配置离线安装 cd /etc/ansible sed -i 's/^INSTALL_SOURCE.*$/INSTALL_SOURCE: "offline"/g' roles/chrony/defaults/main.yml sed -i 's/^INSTALL_SOURCE.*$/INSTALL_SOURCE: "offline"/g' roles/ex-lb/defaults/main.yml sed -i 's/^INSTALL_SOURCE.*$/INSTALL_SOURCE: "offline"/g' roles/kube-node/defaults/main.yml sed -i 's/^INSTALL_SOURCE.*$/INSTALL_SOURCE: "offline"/g' roles/prepare/defaults/main.yml exit
-
Import image
docker load -i images.gz