Gateway
의 의존성을 추가했을 때 확인할 수 있다.
### Spring Cloud Dependency ###
ext {
set('springCloudVersion', "2022.0.2")
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
### Feign Dependency ###
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign' // feign client
}
@EnableFeignClients
선언import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class BaekerApplication {
public static void main(String[] args) {
SpringApplication.run(BaekerApplication.class, args);
}
}
@FeignClient
name
url
@RequestMapping
과 비슷한 기능으로 사용할 수 있다.@Qualifier
MemberClient
를 그대로 주입시켜주게 된다.import com.baeker.baeker.base.request.RsData;
import com.baeker.baeker.feign.dto.MemberJoinDto;
import com.baeker.baeker.msaController.dto.Member;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Map;
@FeignClient(name = "member", url = "http://localhost:8088/member")
@Qualifier
public interface MemberClient {
@GetMapping("/get_member")
RsData<Member> findByUsername(@SpringQueryMap Map<String, Object> parm);
@PostMapping("/create")
RsData<Member> join(@RequestBody MemberJoinDto dto);
}
@SpringQueryMap
은 url 에 파라미터를 노출시켜 요청시켜주는 어노테이션이다.@GetMapping("/get_member")
RsData<Member> findByUsername(
@SpringQueryMap Map<String, Object> param
);
@RequestBody
를 선언하면 message body 에 파라미터가 입력된다.@PostMapping("/create")
RsData<Member> join(
@RequestBody MemberJoinDto dto
);
@GetMapping("/member/{id}"}
RsData<Member> findById(
@PathVariable(id) Long id
);
⚠️ client 객체에 정의된 필드값에 server 의 반환값 객체에 정의된 필드값이 없다면 오류가 발생하지 않고 생략된다.
...
private final MemberClient client;
...
Map<String, Object> pram = new HashMap<>();
pram.put("username", user.getUsername());
RsData<Member> memberRs = memberClient.findByUsername(pram);
...
private final MemberClient client;
...
RsData<Member> memberRs = client.join(new MemberJoinDto(username, nickName, "", provider, email, token, profileImage));