In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, Xiaobian introduces in detail how to use SpringCloud Bus components, with detailed content, clear steps and proper handling of details. I hope this article "how to use SpringCloud Bus components" can help you solve your doubts.
What is Bus?
0. explain
Https://spring.io/projects/spring-cloud-bus
Springcloudbus uses lightweight message agents to connect nodes of distributed systems. You can then use it to broadcast state changes (such as configuration changes) or other administrative instructions. AMQP and Kafka broker implementations are included in the project. Alternatively, any springcloudstream binder found on the classpath can be used as a transport.
Popular definition: bus is called message bus in springcloud, which is mainly used to notify all clients to refresh configuration information through broadcast when remote configuration updates are implemented in micro-service systems, so as to avoid manual restart of services.
Implement the principle of configuration refresh
Set up RabbitMQ service
0. Download rabbitmq installation package [it is more convenient to install directly using docker] official installation package download: https://www.rabbitmq.com/install-rpm.html#downloads
[note:] [here the installation package can only be used on centos7.x systems]
1. Upload the rabbitmq installation package to the linux system
Erlang-22.0.7-1.el7.x86_64.rpmrabbitmq-server-3.7.18-1.el7.noarch.rpm
two。 Install the Erlang dependency package
Rpm-ivh erlang-22.0.7-1.el7.x86_64.rpm
3. Install the RabbitMQ installation package (requires networking)
Yum install-y rabbitmq-server-3.7.18-1.el7.noarch.rpm Note: after the default installation, the configuration file template is in the: / usr/share/doc/rabbitmq-server-3.7.18/rabbitmq.config.example directory. You need to copy the configuration file to the / etc/rabbitmq/ directory and change the name to rabbitmq.config.
4. Copy Profil
Cp / usr/share/doc/rabbitmq-server-3.7.18/rabbitmq.config.example / etc/rabbitmq/rabbitmq.config
5. View profile location
Ls / etc/rabbitmq/rabbitmq.config
6. Modify the configuration file (see figure below)
Vim / etc/rabbitmq/rabbitmq.config
Remove%% of the red part of the configuration file in the above figure, and finally, modify the comma to the following figure:
7. Start plug-in management in rabbitmq by executing the following command
Rabbitmq-plugins enable rabbitmq_management appears as follows: Enabling plugins on node rabbit@localhost:rabbitmq_managementThe following plugins have been configured: rabbitmq_management rabbitmq_management_agent rabbitmq_web_dispatchApplying plugin configuration to rabbit@localhost...The following plugins have been enabled: rabbitmq_management rabbitmq_management_agent rabbitmq_web_dispatchset 3 plugins.Offline change; changes will take effect at broker restart.
8. Start the service of RabbitMQ
Systemctl start rabbitmq-serversystemctl restart rabbitmq-serversystemctl stop rabbitmq-server
9. View service status (see figure below)
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 three 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-A 64-MBas ageffcbf-MHas ageffcbf-
MBlmbcs...
├─ 3220 erl_child_setup 32768
├─ 3243 inet_gethost 4
└─ 3244 inet_gethost 4
...
10. Turn off the firewall service
Systemctl disable firewalldRemoved symlink / etc/systemd/system/multi-user.target.wants/firewalld.service.Removed symlink / etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.systemctl stop firewalld
11. Access the web management interface
Http://10.15.0.8:15672/
twelve。 Log in to the management interface
Username: guestpassword: guest! [] (https://img-blog.csdnimg.cn/8e49b6d997cd47ef8e79128841f08321.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQXJvZw==,size_20,color_FFFFFF,t_70,g_se,x_16)
13.MQ service built successfully
Implement automatic configuration refresh
1. Introduce bus dependencies in all projects
Org.springframework.cloud spring-cloud-starter-bus-amqp
two。 Configure Unified configuration Center to connect to mq
Spring.rabbitmq.host=localhost # connect host spring.rabbitmq.port=5672 # connect mq port spring.rabbitmq .username = user # connection mq username spring.rabbitmq.password=password # connection mq password
3. Add connection mq configuration to remote configuration
4. Start the Unified configuration Center service starts normally
5. After starting the client service to join the bus component, the client starts the error reason. If the remote server is not linked to the springcloud by default, the error will not be reported, but the failure to connect to the remote service must be enabled when using the bus message bus.
Spring.cloud.config.fail-fast=true
6. After modifying the remote configuration, the configuration center service configures curl-X POST http://localhost:7878/actuator/bus-refresh by performing post interface refresh
7. Through the above configuration, the unified refresh of the configuration is realized.
Specify service refresh configuration
1. Description
By default, using curl-X POST http://localhost:7878/actuator/bus-refresh to refresh the configuration is in the form of all broadcasts, that is, all micro-services can receive refresh configuration notifications, but sometimes we only modify the configuration of one service, so notifications for other services are superfluous, so you need to specify a service to notify.
two。 Specify service refresh configuration implementation
Specify a port to refresh a specific service: curl-X POST http://localhost:7878/actuator/bus-refresh/configclient:9090
Specify service id refresh service cluster node: curl-X POST http://localhost:7878/actuator/bus-refresh/configclient
[note:] [configclient represents the unique identity of the refresh service]
Integrate webhook to realize automatic refresh
1. Configure webhooks
Add webhooks
Add refresh configuration interface in webhooks
Private network penetration can be carried out using natapp, and a local penetration is free of charge. The https://natapp.cn/ step is very simple. Then put the penetrating address into the desired address bar.
two。 Resolve 400 error problem
Add a filter to the configuration center server to solve the problem (a pit in springcloud)
Componentpublic 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 ()) / / filter only / actuator/bus-refresh request if (! url.endsWith ("/ bus-refresh")) {chain.doFilter (request, response); return;} / / get the original body String body = readAsChars (httpServletRequest); System.out.println ("original body:" + body) / / use HttpServletRequest to wrap the original request to modify the body content in the post request 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 ();}
After reading this, the article "how to use SpringCloud Bus components" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.