<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
@EnableDiscoveryClient
@EnableFeignClients
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.demo.client;
import com.example.demo.dto.FooDto;
import feign.QueryMap;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
@FeignClient(name = "foo-service", path = "/foos")
public interface FooClient {
@GetMapping("/{id}")
FooDto findById(@PathVariable("id") Long fooId);
@GetMapping("/all")
List<FooDto> findAll(@RequestParam @QueryMap Map parameters);
}
package com.example.demo.controller;
import com.example.demo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/foos")
public class FooController {
@Autowired
private FooService fooService;
@GetMapping("/{id}")
public FooDto findById(@PathVariable("id") Long id) {
return fooService.findById(id);
}
@GetMapping("/all")
public List<FooDto> findAll(FooQo fooQo) {
return fooService.findAll(fooQo);
}
}
package com.example.demo.service;
import com.example.demo.client.FooClient;
import com.example.demo.dto.FooDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BarService {
@Autowired
private FooClient fooClient;
public FooDto getFoo(Long fooId) {
return fooClient.findById(fooId);
}
}