RestTemplate模拟实现一个LoadBalanced注解

1、新建依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <version>1.5.4.RELEASE</version>
</dependency>

2、新建启动类

@SpringBootApplication

3、新建注解类(参考@LoadBalanced)

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface MyLoadBalanced {
 
}

4、新建配置类

import java.util.Collections;
import java.util.List;
 
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;
 
@Configuration
public class MyConfig {
 
    @Autowired(required = false)
    @MyLoadBalanced
    private List<RestTemplate> tpls = Collections.emptyList();
    
    @Bean
    public SmartInitializingSingleton lbInitializing() {
        return new SmartInitializingSingleton() {
 
            public void afterSingletonsInstantiated() {
                for(RestTemplate tpl : tpls) {
                    List<ClientHttpRequestInterceptor> interceptors = tpl.getInterceptors();
                    interceptors.add(new MyInterceptor());
                    tpl.setInterceptors(interceptors);
                }
            }
            
        };
    }
}

5、新建拦截器

import java.io.IOException;
import java.net.URI;
 
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
 
public class MyInterceptor implements ClientHttpRequestInterceptor {
 
    public ClientHttpResponse intercept(HttpRequest request, byte[] body,
            ClientHttpRequestExecution execution) throws IOException {
        System.out.println("===============  这是自定义拦截器");
        System.out.println("         旧的uri:" + request.getURI());
        
        HttpRequest newRequest = new MyRequest(request);
        System.out.println("         新的uri:" + newRequest.getURI());
        
        return execution.execute(newRequest, body);
    }
 
}

6、新建MyRequest

import java.net.URI;
 
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
 
public class MyRequest implements HttpRequest {
    
    HttpRequest sourceRequest;
    
    public MyRequest(HttpRequest sourceRequest) {
        this.sourceRequest = sourceRequest;
    }
 
    public HttpHeaders getHeaders() {
        return sourceRequest.getHeaders();
    }
 
    public HttpMethod getMethod() {
        return sourceRequest.getMethod();
    }
 
    public URI getURI() {
        try {
            URI newUri = new URI("http://localhost:8080/hello");
            return newUri;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sourceRequest.getURI();
    }
 
}

7、新建控制器实现转发

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
@RestController
@Configuration
public class MyController {
 
    @Bean
    @MyLoadBalanced
    public RestTemplate tplA() {
        return new RestTemplate();
    }
    
    @RequestMapping(value = "/call", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public String call() {
        RestTemplate tpl = tplA();
        String json = tpl.getForObject("http://hello-servie/call", String.class);
        return json;
    }
    
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() {
        return "Hello World";
    }
 
}

https://blog.csdn.net/chengqiuming/article/details/81055439

猜你喜欢

转载自www.cnblogs.com/lyx-me/p/10506524.html