머리말
마이크로 서비스 설계에서 서비스 간의 통화는 정상입니다. 일반적으로 우리는 httpclient를 사용하여 원격 자원에 대한 통화를 구현합니다. 이 방법에는 서비스 주소, 비즈니스 인터페이스 주소 등에 대한 지식이 필요하며 호출하기 전에 완료 될 때까지 기다려야합니다. 이것은 통합 개발에 좋은 것이 아닙니다. A와 B 서비스 사이에 강한 의존성이 있습니다. 그렇다면 어떻게 분리 할 수 있습니까? 답은 SpringCloudy의 일부인 OpenFeign 프레임 워크입니다.
1 패키지 참조를 추가합니다
'org.springframework.cloud:spring-cloud-cleatter-openfeign',
참고 : SpringCloudy 버전을 참조하지 않으면 너무 많은 사람이 있습니다. 먼저 선언해야합니다.
종속성 관리 {imports {mavenbom "org.springframework.cloud:spring-cloud-dependencies:$ {SpringCloudversion}"}}2 프로파일 관련 구성을 정의합니다
// 기본 파일 경로 구성 sourcesetsets {integtest {java.srcdir file ( 'src/test/java') resources.srcdir 파일 ( 'src/test/resources')}} task test (type : test) {testClassesDirs = sourcesets.test.test.classsdirs dirs dirs dirs dirs. sourcesets.test.runtimeclasspath}3 서비스 인터페이스를 정의하고 서비스의 메소드 인 의사 메드를 정의하십시오. 메소드 매개 변수와 리턴 값을 알아야합니다. 구현에 대해 걱정할 필요가 없으며 단위 테스트에서 조롱하는 것만으로도 있습니다.
패키지 test.lind.lind.javalindday.feignclientDemo; import org.springframework.cloud.openfeign.feignclient; import org.sprameframework.context.annotation.profile; import org.spramframework.web.bind.annotation.getmapping;/** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** 다른 서비스. */@profile ( "! integtest")@feignclient (name = "serviceName") public interface mockclient {@getMapping (path = "/balancesheet/{clientCode}") String BalancEseet (String ClientCode);}4 프로필의 역할
프로필은 환경 변수입니다. 수업에서 ActiveProfile을 통해 활성화합니다. 그것을 사용할 때는 프로필 주석을 사용했습니다. 위의 코드에서는 Mockclient 객체를 정수 환경에서 사용할 수 없습니다.
5 모의 구현이 추가되어 자동으로 주입되므로 @bean 주석을 선언하십시오.
패키지 test.lind.javalindday; import static org.mockito.argumentmatchers.anystring; import static org.mockito.mockito.mockito.mockito; import static org.mockito.mockito org.springframework.context.annotation.configuration; import org.springframework.context.annotation.profile; import test.lind.lind.javalindday.feignclientdemo.mockclient;@configuration@public class mockclientest {@bean mockclient (@bean mockclient) { 모의 (mockclient.class); 언제 (client.balancesheet (anystring ()) .thenreturn ( "OK"); 반환 클라이언트; }}6 단위 테스트 추가, 단위 테스트에서 환경 변수를 지정하십시오.
패키지 test.lind.javalindday; import static org.junit.assert.assertequals; import org.junit.test; import org.junit.runwith; import org.springframework.bean.beans.annotation.autowed; import org.springframewort.boot.test.context.context.context.context.context org.springframework.test.context.activeprofiles; import org.springframework.test.test.context.junit4.springrunner; import test.lind.javalindday.feignclientdemo.mockclient; @Runwith (springrunner.class)@springboottest // @ActiveProftes (springBoottest) @ActiveProftes "") 클래스 javalinddayApplicationTests {@autowired mockclient mockclient; @test public void testmockclient () {assertequals (mockclient.balancesheet ( "ok"), "ok"); }}테스트를 실행하면 모의 클라이언트가 주입되며 모의 구현 클래스의 프로파일만이 정수 환경을 가리키기 때문에 모의 구현 클래스를 사용합니다.
OpenFeign을 사용하면 미래에 개발 서비스에 대한 서비스 호출이 분리 될 수 있습니다!
요약
위는이 기사의 전체 내용입니다. 이 기사의 내용에 모든 사람의 연구 나 작업에 대한 특정 참조 가치가 있기를 바랍니다. 궁금한 점이 있으면 의사 소통을 위해 메시지를 남길 수 있습니다. Wulin.com을 지원 해주셔서 감사합니다.