개요
Spring Cloud Eureka Ribbon Service 서비스 등록 용사 기사에서 Spring Cloud에서 Eureka 및 Ribbon을 사용하는 방법에 대한 간략한 소개. 이 기사는 RestTemplate를 사용하여 다른 RESTFUL MICROSERVICE 인터페이스에 액세스합니다. 실제로 Spring Cloud에서는 Feign을 사용하여 다른 편안한 마이크로 서비스 인터페이스에 액세스 할 수 있습니다. 더 간결하고 사용하는 것이 명확합니다.
통합 된 Feign
Spring Cloud Eureka Ribbon Service Registration-Discovery-Call Call에서 주문 서비스의 POM 구성을 변경하고 Fegin을 소개하십시오.
<pectionency> <groupid> org.springframework.cloud </groupid> <artifactid> Spring-Cloud-Starter-Feign </artifactid> </fectionency>
OrderApplication 클래스를 수정하고 다음 코드를 삭제하십시오.
@bean @loadbalanced resttemplate resttemplate () {return new resttemplate (); }@enablefeignclients 주석을 추가하십시오. 전체 코드는 다음과 같습니다.
패키지 com.springboot; import org.springframework.boot.springApplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.discovery.enablediscoveryclient; 가져 오기 org.springframework.cloud.netflix.feign.enablefeignclients;@enablediscoveryclient@enablefeignclients@springbootapplicationpublic class orderApplication {public static void main (string [] args) {springApplication.run (OrderApplication.class, Argss); }}인터페이스 사용자 서비스를 추가하고 @feignclient 주석을 사용하십시오.
패키지 com.springboot; import org.springframework.cloud.netflix.feign.feignclient; import org.springframework.web.bind.annotation.getmapping; @feignclient (name = "user") public interface {@getmapping (value = "/getuser") String getuser ();};@feignclient (name = "user")의 name = user는 사용자 마이크로 서비스에 액세스하는 것을 의미합니다. 주문 마이크로 서비스는 유레카와 리본을 통합했습니다. 그런 다음 @feignclient (name = "user")를 사용하여 사용자 마이크로 서비스에 액세스 할 때 클라이언트 라우팅이 자동으로 지원되었습니다. 마이크로 서비스 사용자는 레지스트리에서 찾을 수 있습니다.
OrderController를 수정하고 사용자 서비스를 주입하십시오.
패키지 com.springboot; import org.springframework.bean.bean.bean.beans.annotation.autowired; import org.springframework.web.bind.annotation.getmapping; import org.springframework.bind.bind.annotation.restcontroller; @restcontrollerpublic classcontroller { @aultcontroller { @aultcontrollerpublic classcontroller; @GetMapping ( "/getOrterSer") public String getOrterSer () {return userErvice.getUser (); }}이렇게하면 사용할 필요가 없습니다
resttemplate.getForentity ( "http : // user/getUser", String.class) .getBody ();
사용자 서비스에서 GetUser 인터페이스를 호출합니다. 대신 userervice.getUser () 만 사용하십시오.
레지스트리 센터와 사용자 및 주문의 두 마이크로 서비스를 시작하십시오. http : // localhost : 8883/getOrteruser를 사용하십시오
방문하십시오. 예, 돌아올 수 있습니다
나는 사용자 목록입니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.