Implementation SpringCloud in Feign adapter

Foreword

In a recent project to make micro-services, the need for calls between various systems, and then use an adapter to achieve feign calls between the service, using an adapter for unified management.

Implementation

First, we need to separate the name of the service configuration, you can easily switch and expansion, we use bootstrap.yml be configured so that the introduction of the jar package, they can be configured to application.yml complement our own projects.
Configured in our bootstrap.yml in.

## 配置的服务名称
server-name:
  # 配置在eureka中注册的服务名称
  feignDemo: demo

Here we configured a demo, demo is spring.application.name. We need to call the demo project interface.
Then configure feign with ribbon

feign:
  hystrix:
   threadpool:
    default:
     coreSize: 100
    enabled: true
    command:
      default:
       execution:
         isolation:
          thread:
           timeoutInMilliseconds: 30000
      circuitBreaker:
       requestVolumeThreshold: 1000
ribbon:
 ConnectTimeout: 30000
 ReadTimeout: 30000

After we have established a feign interface.

@FeignClient(value = "${server-name.feignDemo}")
@Component
public interface IDemoFeign {

    @PostMapping("/demo/list")
    public List<Demo> findDemoListByQueryVO(
            @RequestBody DemoFeignQueryVO demoFeignQueryVO);

}

Let's get the name of the service by way of a $ value. The method in the interface must be exactly the same with the mapping method of service. The issue here should be noted that the parameters have to use @RequestBody or @RequestParam to acess.
Then build a controller class.

@Component
public class DemoApi {

    @Autowired
    private IDemoFeign iDemoFeign ;

        public List<Demo> findDemoListByQueryVO(DemoFeignQueryVO demoFeignQueryVO){
            return iDemoFeign .findDemoListByQueryVO(demoFeignQueryVO);
        }

}

We call by using the call controller, so that we have a problem in the future if the interface, the interface can be changed directly, do not change the code calls the project.
Finally, we establish the main start classes in the master boot class, the configuration controller class we just created, this time labeled jar package called to inject a master class to start, and then you can call other api.
Start main classes are as follows:

@SpringBootApplication
@EnableFeignClients
public class ApiAdapter {

    public static void main(String[] args) {
        SpringApplication.run(ApiAdapter.class, args);
    }

    @Autowired
    public DemoApi demoApi ;

}

We need to pay attention to is the need to annotate @EnableFeignClients.
End implementations which was labeled jar package, introduction can be used.

Guess you like

Origin www.cnblogs.com/jichi/p/11621472.html