栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Spring cloud config配置中心

Spring cloud config配置中心

文章目录
    • Spring cloud config配置中心
      • 步骤
      • 三个业务模块的配置,传到git仓库
      • 搭建配置中心
      • 配置中心的客户端
    • Rabbitmq消息中间件
      • 搭建Rabbitmq服务器(需要VMware)
      • VMware
  • # 安装 Docker 虚拟机
  • # Rabbitmq 消息中间件
    • 搭建 Rabbitmq 服务器
    • Rabbitmq 服务的六种模式:
        • 1. pom文件:
        • 2.生产者发送消息
        • 3.消费者接收消息
        • 生产者发送消息
        • 消费者接收消息
        • 生产者发送消息
        • 消费者接收消息
    • RabbitMQ 使用场景

Spring cloud config配置中心

默认用git仓库进行存储数据

步骤
  1. 创建远程仓库
      1.在gitee中,右上角点加号
      2.设置仓库名
      3.设置成开源项目
  2. 创建本地仓库
      VCS----create git repository
      选择springcloud1工程文件夹作为本地仓库目录
      点击对勾即可或ctrl+k
      选中全部文件、必写提交信息、执行提交
  3. 把本地仓库的数据上传到远程仓库
      ctrl+shift+k 或VCS----git----push 执行推送
      点击define remote
      粘贴远程仓库的地址
三个业务模块的配置,传到git仓库
  1. 在springcloud1创建一个目录
  2. 复制三个业务的application.yml进去这目录
  3. 三个文件中添加override-none=true
spring:
  application:
    name: order-service
  cloud:
    config:
      override-none: true

防止下载的配置,覆盖本地参数设置
4. 提交到本地仓库
5. 上传到远程仓库

搭建配置中心
  1. 新建sp09-config模块
  2. 添加config server 、eureka client依赖

            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client

  1. yml配置
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/han-jia-ajie/springcloud1
server:
  port: 6001
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
  1. 启动类注解:@EnableConfigServer
  2. 检查确认:http://localhost:6001/item-service/dev
          http://localhost:6001/user-service/dev
          http://localhost:6001/order-service/dev
配置中心的客户端
  1. 修改2,3,4的application.yml的代码全部注释
  2. 修改03用户,添加配置中心客户端设置
      添加config client依赖

            org.springframework.cloud
            spring-cloud-starter-config

  新建bootstrap.yml(在引导配置阶段,从配置中心下载)
  添加三条配置:
      连接eureka
      指定配置中心的服务ID
      从配置中心下载user-service-dev.yml

#连接eureka
eureka:
  client:
    service-url:
      #默认地点,如果使用的是云服务,可以通过云服务商购买不同地点的注册中心服务器
      #自己搭建的注册中心只能使用defaultZone
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
#配置中心的服务id
spring:
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config-server
      #user-service-dev.yml
      name: order-service
      profile: dev
#下载配置文件
Rabbitmq消息中间件

在分布式系统中,用来在模块之间传递数据的工具,是重要的组件,RabbitMQ是一种消息中间件,用于处理来自客户端的异步消息。
常见的消息服务器:Rabbitmq、Acticemq、Kafka、Rocketmq(阿里)、Tubemq(腾讯)

搭建Rabbitmq服务器(需要VMware) VMware

版本要在16或16+
NatW网络网段
使用192.168.64.0网段
(编辑—>虚拟网络编辑–上面选择vmnet8—左下角修改成192.168.64.0)

# 安装 Docker 虚拟机
  1. 关闭 centos-8-2105
  2. 克隆 centos-8-2105: docker-base
  3. 用 mobaxterm 工具上传文件到 /root/
    – 课前资料DevOps课前资料dockerdocker-install 文件夹(可以用上个老师的软件)
  4. 参考 Docker离线安装笔记,从第3步开始执行(前两步下载文件不用执行)
# Rabbitmq 消息中间件

在分布式系统中,用来在模块之间传递数据的工具

消息服务在分布式系统中,是非常重要的组件

常用的消息服务器:

  • Rabbitmq
  • Activemq
  • Kafka
  • Rocketmq
  • Tubemq
搭建 Rabbitmq 服务器
  1. 准备好docker环境
  2. 从 docker base 克隆: rabbitmq
  3. 设置ip地址
    ./ip-static
    ip: 192.168.64.140
    
    ifconfig   #如果有问题,参考 VMware 下面的 解决网络设置问题
    
  4. 上传镜像压缩文件到/root/
    –DevOps课前资料dockerrabbitmq-image.gz`
  5. 执行导入 docker load -i rabbitmq-image.gz
    具体看这个链接
Rabbitmq 服务的六种模式:
  • 简单模式
1. pom文件:
 
        
            com.rabbitmq
            amqp-client
            5.4.3
        
    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.0
                
                    1.8
                    1.8
                
            
        
    
2.生产者发送消息
package m1;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //1.连接服务器
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("wht6.cn");//要和自己的虚拟机ip匹配
        factory.setPort(5672);//5672是客户端收发信息的端口,15672是管理控制台的访问端口
        factory.setUsername("admin");
        factory.setPassword("admin");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();//通信的通道
        //2.创建队列 helloworld,如果已经存在就不会重复创建
        //queueDeclare:队列声明
        
        channel.queueDeclare("helloworld-ren",false,false,false,null);
        //3.向holloworld
        
        channel.basicPublish("", "helloworld-ren", null, "helloworld".getBytes());
        System.out.println("消息发送完成");
    }
}

3.消费者接收消息
package m1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //建立连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("wht6.cn");//要和自己的虚拟机ip匹配
        factory.setPort(5672);//5672是客户端收发信息的端口,15672是管理控制台的访问端口
        factory.setUsername("admin");
        factory.setPassword("admin");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //创建队列
        channel.queueDeclare("helloworld-ren",false,false,false,null);
        //创建回调对象
        DeliverCallback deliverCallback= (consumerTag,message)->{
            byte[] a = message.getBody();
            String s = new String(a);
            System.out.println("收到:"+s);
        };
        CancelCallback cancelCallback=consumerTag -> {};
        //从helloworld接收消息,传递回调对象
        
        channel.basicConsume("helloworld-ren", true,deliverCallback,cancelCallback);
    }
}
  • 工作模式
生产者发送消息
package m2;

import com.rabbitmq.client.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;


import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("wht6.cn");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin" );
        //创建队列,叫helloworld-ren
        Channel channel = connectionFactory.newConnection().createChannel();
        channel.queueDeclare("task_queue", true,false,false , null);
        //发送消息
        while (true){
            System.out.println("输入消息:");
            String s = new Scanner(System.in).next();
            channel.basicPublish("", "task_queue", MessageProperties.PERSISTENT_BASIC, s.getBytes());
        }
    }
}
消费者接收消息
package m2;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("wht6.cn");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin" );
        //创建队列
        Channel channel = connectionFactory.newConnection().createChannel();
        //创建回调对象
        DeliverCallback deliverCallback= (consumerTag,message)->{
            String s = new String(message.getBody());
            System.out.println("收到:"+s);
            //遍历字符串找‘.’字符
            for (int i = 0; i  {};
        //每次收一条,处理完之前不受下一条
        channel.basicQos(1);
        //开始接收数据
        channel.basicConsume("helloworld-ren", true, deliverCallback, cancelCallback);
    }
}
  • 广播模式
生产者发送消息
package m3;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("wht6.cn");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin" );
        Channel channel = connectionFactory.newConnection().createChannel();
        //创建Fanout交换机:logs
        //channel.exchangeDeclare("logs", "fanout");
        channel.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
        //向logs交换机发送消息
        while (true){
            System.out.println("输出消息:");
            String s = new Scanner(System.in).nextLine();
            channel.basicPublish("logs", "", null, s.getBytes());
        }
    }
}
消费者接收消息
package m3;

import com.rabbitmq.client.*;
import jdk.nashorn.internal.parser.Scanner;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("wht6.cn");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Channel channel = f.newConnection().createChannel();
        //1.创建随机队列   2.创建交换机  3.绑定
        String queue = UUID.randomUUID().toString();
        channel.queueDeclare(queue, false, true, true, null);
        channel.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
        //对fanout交换机,第三个参数无效
        channel.queueBind(queue, "logs", "");
        //接收处理消息
        DeliverCallback deliverCallback= (consumerTag,message)->{
            String s = new String(message.getBody());
            System.out.println("收到:"+s);
        };
        CancelCallback cancelCallback=consumerTag -> {};
        channel.basicConsume(queue, true, deliverCallback, cancelCallback);
    }
}
  • 路由模式
  • 主题模式
RabbitMQ 使用场景
  • 流量削峰
  • 服务解耦
  • 异步调用
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/673778.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号