spring cloud using Feign call service

On the basis of a hello world on the transformation of Consumer, more convenient to use, easy to understand Feign to invoke the service

Feign is a declarative Web Service clients.

Feign make use of Web Service clients written in simpler, its use is to define an interface, then add annotations on top, also supports standard JAX-RS annotations.

Feign also supports plug-type encoder and decoder.

Spring Cloud to Feign the package, to support the Spring MVC standard annotation and HttpMessageConverters.

Feign Ribbon can be used in combination with Eureka and to support load balancing.

Add Feign support pom.xml

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.1.3.RELEASE</version>
 </dependency>

Write interfaces MessageRemote

@FeignClient (name = " the PROVIDER " )    // name registration service center
 public  interface MessageRemote { 

    @RequestMapping (value = " / Provider / Hello " )     // Path service 
    the Map Hello (); 
}

Conroller

@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    MessageRemote messageRemote;


    @RequestMapping("/hello")
    public Map hello(){
      return  restTemplate.getForObject("http://PROVIDER/provider/hello", HashMap.class);
    }

    @RequestMapping("/remote/hello")
    public Map remoteHello(){

        return messageRemote.hello();
    }

}

Browser Access: localhost: 8080 / message / remote / hello

 

 carry out

Guess you like

Origin www.cnblogs.com/yhood/p/11571666.html