亲宝软件园·资讯

展开

SpringCloud Bus组件

Arog 人气:0

什么是Bus

0.解释

实现配置刷新原理

搭建RabbitMQ服务

0.下载rabbitmq安装包 [可以直接使用docker安装更方便] 官方安装包下载:https://www.rabbitmq.com/install-rpm.html#downloads
[注意:][这里安装包只能用于centos7.x系统]

1.将rabbitmq安装包上传到linux系统中

erlang-22.0.7-1.el7.x86_64.rpm
rabbitmq-server-3.7.18-1.el7.noarch.rpm

2.安装Erlang依赖包

rpm -ivh erlang-22.0.7-1.el7.x86_64.rpm

3.安装RabbitMQ安装包(需要联网)

yum install -y rabbitmq-server-3.7.18-1.el7.noarch.rpm
	注意:默认安装完成后配置文件模板在:/usr/share/doc/rabbitmq-server-3.7.18/rabbitmq.config.example目录中,需要将配置文件复制到/etc/rabbitmq/目录中,并修改名称为rabbitmq.config

4.复制配置文件

cp /usr/share/doc/rabbitmq-server-3.7.18/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config

5.查看配置文件位置

ls /etc/rabbitmq/rabbitmq.config

6.修改配置文件(参见下图:)

vim /etc/rabbitmq/rabbitmq.config 

将上图中配置文件中红色部分去掉%%,以及最后的,逗号 修改为下图:

7.执行如下命令,启动rabbitmq中的插件管理

rabbitmq-plugins enable rabbitmq_management

出现如下说明:
	Enabling plugins on node rabbit@localhost:
rabbitmq_management
The following plugins have been configured:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch
Applying plugin configuration to rabbit@localhost...
The following plugins have been enabled:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch

set 3 plugins.
Offline change; changes will take effect at broker restart.

8.启动RabbitMQ的服务

systemctl start rabbitmq-server
systemctl restart rabbitmq-server
systemctl stop rabbitmq-server

9.查看服务状态(见下图:)

systemctl status rabbitmq-server

● rabbitmq-server.service - RabbitMQ broker
Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2019-09-25 22:26:35 CST; 7s ago
Main PID: 2904 (beam.smp)
Status: “Initialized”
CGroup: /system.slice/rabbitmq-server.service
├─2904 /usr/lib64/erlang/erts-10.4.4/bin/beam.smp -W w -A 64 -MBas ageffcbf -MHas ageffcbf -
MBlmbcs…
├─3220 erl_child_setup 32768
├─3243 inet_gethost 4
└─3244 inet_gethost 4

10.关闭防火墙服务

systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
systemctl stop firewalld   

11.访问web管理界面

http://10.15.0.8:15672/    

12.登录管理界面

username:  guest
password:  guest

13.MQ服务搭建成功

实现自动配置刷新

1.在所有项目中引入bus依赖

<!--引入bus依赖-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

2.配置统一配置中心连接到mq

spring.rabbitmq.host=localhost											#连接主机
spring.rabbitmq.port=5672														#连接mq端口
spring.rabbitmq.username=user												#连接mq用户名
spring.rabbitmq.password=password										#连接mq密码

3.远端配置中加入连接mq配置

4.启动统一配置中心服务 正常启动

5.启动客户端服务 加入bus组件之后客户端启动报错原因springcloud中默认链接不到远程服务器不会报错,但是在使用bus消息总线时必须开启连接远程服务失败报错

spring.cloud.config.fail-fast=true

6.修改远程配置后在配置中心服务通过执行post接口刷新配置 curl -X POST http://localhost:7878/actuator/bus-refresh

7.通过上述配置就实现了配置统一刷新

指定服务刷新配置

1.说明

2.指定服务刷新配置实现

[注意:][configclient代表刷新服务的唯一标识]

集成webhook实现自动刷新

1.配置webhooks

内网穿透可以使用natapp进行穿透,本地一个穿透免费穿透,https://natapp.cn/ 步骤非常简单。然后将穿透的地址放入到所需地址栏即可。

2.解决400错误问题

@Component
public class UrlFilter  implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
 
    }
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)request;
        HttpServletResponse httpServletResponse = (HttpServletResponse)response;
        String url = new String(httpServletRequest.getRequestURI());
        //只过滤/actuator/bus-refresh请求
        if (!url.endsWith("/bus-refresh")) {
            chain.doFilter(request, response);
            return;
        }
        //获取原始的body
        String body = readAsChars(httpServletRequest);
        System.out.println("original body:   "+ body);
        //使用HttpServletRequest包装原始请求达到修改post请求中body内容的目的
        CustometRequestWrapper requestWrapper = new CustometRequestWrapper(httpServletRequest);
        chain.doFilter(requestWrapper, response);
    public void destroy() {
    private class CustometRequestWrapper extends HttpServletRequestWrapper {
        public CustometRequestWrapper(HttpServletRequest request) {
            super(request);
        @Override
        public ServletInputStream getInputStream() throws IOException {
            byte[] bytes = new byte[0];
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            return new ServletInputStream() {
                @Override
                public boolean isFinished() {
                    return byteArrayInputStream.read() == -1 ? true:false;
                }
                public boolean isReady() {
                    return false;
                public void setReadListener(ReadListener readListener) {
                public int read() throws IOException {
                    return byteArrayInputStream.read();
            };
    public static String readAsChars(HttpServletRequest request)
    {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder("");
        try
        {
            br = request.getReader();
            String str;
            while ((str = br.readLine()) != null)
            {
                sb.append(str);
            }
            br.close();
        catch (IOException e)
            e.printStackTrace();
        finally
            if (null != br)
                try
                {
                    br.close();
                catch (IOException e)
                    e.printStackTrace();
        return sb.toString();
}

加载全部内容

相关教程
猜你喜欢
用户评论