Feign 소개
Feign은 선언 된 웹 서비스 클라이언트로 웹 서비스 클라이언트를보다 쉽게 작성할 수 있습니다. Feign을 사용하여 인터페이스를 만들고 주석을 달 수 있습니다. Feign 주석 및 JAX-RS 주석을 포함하여 플러그 가능한 주석 지원이 있습니다. Feign은 또한 플러그 가능한 엔코더 및 디코더를 지원합니다. 스프링 클라우드는 스프링 MVC에 주석을 추가합니다. Spring Web은 기본적으로 httpmessageconverters를 사용하고 Spring Cloud는 리본과 유레카가 제공하는 하중 균형 잡힌 HTTP 클라이언트 Feign을 통합합니다.
선언적 휴식 클라이언트 : Feign
먼저 Eureka_register_Service Project (등록 센터) 및 Biz-Service-0 Project (서비스 생산자)를 시작하십시오.
Maven Project Eureka_Feign_Client를 만듭니다
pom.xml
<부모> <groupid> org.springframework.boot </groupid> <artifactid> 스프링-부트-스타터-팔렌트 </artifactid> <bersion> 1.4.3.3.3.30 </version </version> <realiveativePath/> <!-저장소에서 모험 부모 -> </parent> <properties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </properties> <pection> <groupId> groupId> org.springFramework.cloud </GroupId> <Artifactid> Spring-Cloud-Starter-Feign </artifactid> </fectionency> <pectionement> <groupid> org.springframework.cloud </groupid> <trucicid> Spring-Cloud-Starter-Eureka </artifactid> </dependency> <groupid> <groupid> org.springframework.boot> <artifactid> Spring-Boot-starter-web </artifactid> </fectionency> <pectionement> <groupid> org.springframework.boot </groupid> <trefactid> spring-boot-starter-test </artifactid> <scope> test </scope> </dependencies> <peplencymanagement> <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-dependencies </artifactid> <bersion> brixton.sr5 </version> <type> pom </type> <cope> 가져 오기 </scope> </fexendence> </dependence> </fectionement 관리>
메인 애플리케이션 클래스에서 @enablefeignclients 주석을 통해 Feign 기능 활성화
feignapplication.java 파일을 시작하십시오
@SpringBootApplication@enablediscoveryClient@enableFeignClientsPublic Class feignApplication {public static void main (String [] args) {springApplication.Run (feignApplication.class, args); }}서비스 인터페이스 클래스 userClient.java 정의
@FeignClient ( "Biz-Service-0") 주석을 사용 하여이 인터페이스의 해당 Biz-Service-0 서비스를 바인딩하십시오.
@FeignClient ( "biz-service-0") public interface userclient {@requestmapping (method = requestmethod.get, value = "/getUser") public user getUserInfo (); @requestmapping (method = requestMethod.get, value = "/getUser") public String getUserInfostr (); @requestmapping (method = requestmethod.get, value = "/info") public String info ();}다음과 같이 웹 계층에서 위에서 정의 된 UserController를 호출하십시오.
@RestControllerPublic Class USERCONTROLLER {@autowired userClient userClient; @requestMapping (value = "/getUserInfo", method = requestMethod.get) 공개 사용자 getUserInfo () {return userClient.getUserInfo (); } @requestmapping (value = "/getUserInfostr", method = requestMethod.get) public String getUserInfostr () {return userClient.getUserInfostr (); } @requestmapping (value = "/info", method = requestMethod.get) public String info () {return userClient.info (); }} Application.Properties 구성 변수
spring.application.name = feign-consumerserver.port = 8004eureka.client.serviceurl.defaultzone = http : // localhost : 8000/eureka/
http://127.0.0.1:8004/getuserinfo를 방문하십시오
요약 :
실제로 HTTP 통화 서비스 방법은 FEIGN을 통해 캡슐화되므로 클라이언트는 로컬 메소드를 호출하는 것과 같은 방법을 직접 호출합니다. Dubbo에서 원격 서비스를 노출시키는 방식과 마찬가지로 Dubbo는 개인 바이너리 프로토콜을 기반으로하는 반면 Feign은 본질적으로 HTTP 클라이언트입니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.