In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how springboot configures multiple requesting service agents. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Springboot configuration Service proxy
Sometimes, we may have the following needs:
That is, for distributed services, we will have multiple business interface services, but only one service port may be required on the server. For example, the restA project port in the figure above is open to the public, but the restB project port is not open to the public. The problem is that users cannot request the restB project directly.
Think of the time when you can access restA and the request path meets certain specifications, such as http://ip:port/test. When the request starts with rest, you can forward the request to the restB project.
Of course, there are many solutions to proxy forwarding, such as nginx, zuul, etc., but nginx is simple, but it always needs to install one more service; zuul configuration is more troublesome.
After Baidu, I found a very simple configuration, that is, what I want to say here, the injection of ServletRegistrationBean is equivalent to the introduction of servlet. I haven't seen it specifically.
Write down the configuration steps below:
1. Project structure and introduction
The following is my project structure. The blue project is selected as the project we want to configure, and the other items are ignored first, which are used when we configure based on dubbo and zookeeper.
The project has been placed on GitHub. Download the project GitHub address.
The introduction of this project is as follows, together with a picture to introduce:
The suiteoneservice, suitetwoservice, and masterservice projects in the figure above are our service interface publisher projects. I'm just drawing it here, which has nothing to do with what we're going to configure.
The ports of suiteone and suitetwo projects are not externalized, and users cannot access them directly, while master projects can be accessed directly, so users visit master projects, and then the master project requests are forwarded to these two projects by the request agent.
2. Specific configuration steps
The main configurations are very few, all in the master project.
(1) introduce dependency:
Org.mitre.dsmiley.httpproxy smiley-http-proxy-servlet 1.7
(2) configure a configuration class:
This class can refer to the configuration in the downloaded master project.
Package microservice.sc.config;import org.mitre.dsmiley.httpproxy.ProxyServlet;import org.springframework.boot.bind.RelaxedPropertyResolver;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.EnvironmentAware;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment;/** * Created by lsf on 2018-7-31. * / @ Configurationpublic class ProxyServletConfiguration implements EnvironmentAware {@ Bean public ServletRegistrationBean servletRegistrationBean () {ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean (new ProxyServlet (), propertyResolver.getProperty ("servlet_url_one")); / / this setName must be set, and when there are multiple, the name needs to be different servletRegistrationBean.setName ("suitone"); servletRegistrationBean.addInitParameter ("targetUri", propertyResolver.getProperty ("target_url_one"); servletRegistrationBean.addInitParameter (ProxyServlet.P_LOG, propertyResolver.getProperty ("logging_enabled", "false"); return servletRegistrationBean) } @ Bean public ServletRegistrationBean servletRegistrationBean2 () {ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean (new ProxyServlet (), propertyResolver.getProperty ("servlet_url_two")); / / this setName must be set, and multiple names require different servletRegistrationBean.setName ("suittwo"); servletRegistrationBean.addInitParameter ("targetUri", propertyResolver.getProperty ("target_url_two")); servletRegistrationBean.addInitParameter (ProxyServlet.P_LOG, propertyResolver.getProperty ("logging_enabled", "false")); return servletRegistrationBean;} private RelaxedPropertyResolver propertyResolver Override public void setEnvironment (Environment environment) {this.propertyResolver = new RelaxedPropertyResolver (environment, "proxy.test.");}}
(3) configure the proxy address:
To the main configuration file of the master project, the application.properties file, add the following:
# when requesting testone, the agent forwards to the proxy.test.servlet_url_one=/testone/*proxy.test.target_url_one= http://localhost:30001# request testtwo in the 30001 project, and the agent forwards to the proxy.test.servlet_url_two=/testtwo/*proxy.test.target_url_two= http://localhost:30002 in the 30002 project.
The configuration above is briefly introduced. For testone/*, it means that when your request path starts with testone, such as a path like http://localhost:30000/testone/test/get1, the real path it requests is http://localhost:30001/test/get1. The main thing is to replace the testone with the corresponding proxy path, which means the path of the interface in the actual request project!
Port 30001 is the suiteone project, and port 30002 is the suitetwo project. After downloading the project, start the startup classes of the master, suiteone and suitetwo projects respectively. After the startup is completed, visit http://localhost:30000/testone/test/get1, and you will return the contents of the test/get1 API of the suiteone project.
This configuration is valid for both get and post requests.
Thank you for reading! This is the end of this article on "how to configure multiple request service agents for springboot". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.