Spring boot complete steps for configuring multiple request service agents

Springboot configuration service agent

Sometimes, we may have the following needs:

That is, for distributed services, we will have a variety of business interface services, but only one service port may be required to be opened on the server. For example, the resta project port in the above figure is open to the public, but the restb project port is not open to the public. The problem is that users cannot directly request restb projects.

That's 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 for proxy forwarding, such as nginx and zuul. However, although nginx is simple, it always needs to install one more service; Zuul configuration is troublesome.

After Baidu, I found a very simple configuration, that is, the injection of servletregistrationbean, which is equivalent to the introduction of servlet. I haven't seen it in detail.

Here are the configuration steps:

1. Project structure and introduction

The following is my project structure. The blue items selected below are the items we want to configure. Other items are ignored first. They 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, and a picture is attached to introduce it:

The suiteoneservice, suitewoservice and masterservice projects in the above figure are our service interface publisher projects. I just draw it here, which has nothing to do with the content we want to configure.

The suiteone and suitetwo project ports are not put out, so users cannot access them directly, while the master project can access them directly. Therefore, users access the master project, and then the master project forwards the request agent to the two projects.

2. Specific configuration steps

There are few major configurations, all of which are in the master project.

(1) Import dependency:

 <!--ProxyFilter的引入依赖-->
 <dependency>
  <groupId>org.mitre.dsmiley.httpproxy</groupId>
  <artifactId>smiley-http-proxy-servlet</artifactId>
  <version>1.7</version>
 </dependency>

(2) . configure a configuration class:

This class can refer to the configuration in the master project downloaded.

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.
 */
@Configuration
public class ProxyServletConfiguration implements EnvironmentAware {
 @Bean
 public ServletRegistrationBean servletRegistrationBean(){
 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(),propertyResolver.getProperty("servlet_url_one"));
 //这个setName必须要设置,并且多个的时候,名字需要不一样
 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"));
 //这个setName必须要设置,并且多个的时候,名字需要不一样
 servletRegistrationBean.setName("suittwo");
 servletRegistrationBean.addInitParameter("targetUri",propertyResolver.getProperty("target_url_two"));
 servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG,"false"));
 return servletRegistrationBean;
 }

 private RelaxedPropertyResolver propertyResolver;

 @Override
 public void setEnvironment(Environment environment) {
 this.propertyResolver = new RelaxedPropertyResolver(environment,"proxy.test.");
 }
}

(3) . configure proxy address:

To the master configuration file of the master project, i.e. application In the properties file, add the following:

#请求testone时代理转发到30001项目中
proxy.test.servlet_url_one=/testone/*
proxy.test.target_url_one=http://localhost:30001

#请求testtwo时代理转发到30002项目中
proxy.test.servlet_url_two=/testtwo/*
proxy.test.target_url_two=http://localhost:30002

The above configuration is briefly introduced. The writing of testone / * means that when your request path starts with testone, such as http://localhost:30000/testone/test/get1 Such a path, the real path it requests is http://localhost:30001/test/get1 。 It is mainly to replace testone with the corresponding proxy path, * 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 master, suiteone and suitetwo projects respectively. After startup, access http://localhost:30000/testone/test/get1 , the content of the test / GET1 interface of the suiteone project will be returned.

This configuration is valid for both get and post requests.

The above is the complete configuration.

If you don't understand, you can download the project and have a look. GitHub address

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. Thank you for your support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>