spring cloud 中使用zookeeper作为服务注册中心与配置中心的使用配置

#Spring Cloud 中使用zookeeper作为服务注册中心与配置中心

安装zookeeper

-下载
-解压

tar -xvf zookeeper-3.4.10.tar.gz

-启动zookeeper

cd zookeeper-3.4.10
cd conf
cp zoo_sample.cfg zoo.cfg
cd ../bin
sh zkServer.sh start

使用zookeeper作为服务注册中心

maven依赖

      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
      </dependency>

启动类上添加注解@EnableDiscoveryClient

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);
   }
}

添加配置文件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

使用DiscoveryClient获取注册服务列表

 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;
   }
}

注:可以启动不同的实例,此处我启动了端口8080与8081两个实例,然后使用端点可以查询到所注册的服务列表

同样可以通过zookeeper相关命令查询到说注册的服务列表

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]

使用zookeeper作为配置中心

  • 使用zkCli创建配置信息
[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"

添加 maven依赖

       <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

编写Controller来动态获取zookeeper配置中心的数据

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 集群方案

  1. 单机 - 16G以上

    一个主控,两个工作节点

  2. 单机 - 8G以上

    一个主控+工作基点,一个工作节点

  3. 多台主机 - 4G

    一台主机启动一个虚拟机,用桥接网络

配置集群环境

  1. 克隆 centos-7-1908: k1

  2. 设置 cpu 和 内存

    cpu - 2

    内存 - 2G

    第三个方案,把网络设置成桥接网络

  3. 设置ip

    ./ip-static
    ip: 192.168.64.191
    
    第三个方案,用自动获取ip
    ./ip-dhcp
    
  4. 上传文件

    • easzup、images.gz 两个文件上传到 /root/
    • ansible 文件夹上传到 /etc/
  5. 执行以下命令

    # 对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
    
  6. 导入镜像

    docker load -i images.gz
    

猜你喜欢

转载自blog.csdn.net/qq_41536934/article/details/110940515