I decided to use a proxy service with Java crawling PJ and implemented it with reference to other articles. It can be easily applied by adding the following 2 files. -Src / main / java / ProxySetConfig.java ・ Src / main / resources / proxy.properties
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import javax.annotation.PostConstruct;
@Configuration
@Profile("pro")
@PropertySource("classpath:proxy.properties")
public class ProxySetConfig {
@Value("${proxy.host}")
private String host;
@Value("${proxy.port}")
private String port;
@PostConstruct
private void setProxy() {
// Proxy for System
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxyPort", port);
System.setProperty("https.proxyHost", host);
System.setProperty("https.proxyPort", port);
}
}
The above @Profile ("pro") is @Profile ("local") for local If it is a test, you can switch it by rewriting it to @Profile ("test").
proxy.host=xxx.xx.xx.xxx
proxy.port=xxxxx
In the above proxy.properties file It is very convenient because you can respond to proxy changes simply by rewriting proxy.host and proxy.port.
https://kcf-developers.hatenablog.jp/entry/2018/09/04/112933
Recommended Posts