Feign be accessed using the service

Feign in SpringCloud use in serving access

Having said that the SpringCloud as about principles and methods when a "distributed micro-service" solution of. Access between the two is a web application, there are two ways to access previous:

  • Use RestTemplate with the packaged class, use the + ip + port service address access, this is the easiest way to access the
@GetMapping("demo1/consumer/hello/{id}")
public String hello(@PathVariable("id") String id){
        //远程调用provider中的接口
    return restTemplate.getForObject("http://localhost:8001/demo1/provider/hello/"+id,String.class);
}
  • Ribbon accessed using load balancing, that is the "provider" registered with the registry, then "consumer" service name using micro access
@GetMapping("demo3/consumer/hello/{id}")
public String hello(@PathVariable("id") String id){
        //远程调用provider中的接口
        return restTemplate.getForObject("http://demo3-ribbon-provider/demo3/provider/hello/"+id,String.class);
}

but? So access is very troublesome, when accessed using restTemplate, the parameters to be spliced. Is there a better way to do that?

And, if we want to RPC frameworks (such as Dubbo, etc.), using the local interface you can access it? So we're going to introduce Feign

1. Create a service interface to access

Since we imagine the same local access to a remote "micro-service" interfaces, of course, the remote code written in this project is not possible, then create an interface to access

  • Create a new project demo4-feign-interface

In fact, you create this project is to allow consumers to micro-services dependent, of course, you can create this interface directly on your consumer items, but not conducive to management, so we created a new project, designed to write feign Interface

  • the pom.xml (Import dependent Feign client)
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
  • Service class and then write interface
package cn.lyn4ever.provider;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 这个注解表明这是一个Feign的客户端,
 * 其中的value属性就是目标微服务的服务名
 */
@FeignClient(value = "demo3-ribbon-provider")
public interface ProviderService {
    /**
     * 这个其实是provider提供者中的方法
     *
     * @param id
     * @return
     */
    @GetMapping("demo3/provider/hello/{id}")
    public String hello(@PathVariable("id") Integer id);
}

The main value FeignClient look at the value in the top notes, is the goal of the micro-service service name

The hello () method and content of annotation, all items demo3-ribbon-provider in

  • Then, use the command maven, clean and then install to a local warehouse for calls to other projects
  • This application is only as a consumer dependency, there is no need to start class

2. Create a new project, using Feign access

  • Create a new project using Feign consumers will copy demo3-consumer before, and then modified as follows:
  • pom.xml and add dependencies feign demo4-feign-interface we created earlier
<!--之前的依赖省略,请查看源码-->
<!--feign的依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

<!--之前创建的feign接口项目-->
<dependency>
    <groupId>cn.lyn4ever</groupId>
    <artifactId>demo4-feign-interface</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
  • application.yml without modifying or copying over the previous configuration file
  • Add annotations to call the package name, the statement Feign in the startup class
@EnableFeignClients(basePackages= {"cn.lyn4ever.provider"})
  • The next step is to modify our controller class can be used to access the local interface
@RestController
public class HelloConsumerController {

    @Autowired
    private ProviderService providerService;

    @GetMapping("demo4/consumer/hello/{id}")
    public String hello(@PathVariable("id") Integer id){
        //直接使用本地的接口就可以访问了
        return providerService.hello(id);
    }
}

This is who starts Eureka clusters, three provicder service provider, the service consumer just created, and then use the browser to access

3. What are the benefits of using Feign access and principle have?

  • When accessed using FeignClient, you can no longer use the ip + port service name or use the micro access. May, after Autowired, call the method directly in the local
  • In fact, Feign locally generated dynamic proxy can be used directly Autowired calls. Its nature or the use of Ribbon Load balancing access
  • Feign built a Ribbon load balancing, it also can be customized as load balancing algorithms like Ribbon

More about SpringCloud study notes and code addresses concerns micro-channel public number "small fish and Java" reply "SpringCloud" get

Guess you like

Origin www.cnblogs.com/Lyn4ever/p/12446162.html
Recommended