Preface
In microservice design, calls between services are normal. Usually we use httpClient to implement calls to remote resources. This method requires knowledge of the address of the service, business interface address, etc., and you need to wait until it is completed before you can call it. This is not a good thing for integrated development. It has a strong dependence between A and B services. So how do we decouple it? The answer is the openfeign framework, which is part of springcloudy.
1 Add a package reference
'org.springframework.cloud:spring-cloud-starter-openfeign',
Note: If you don't reference the springcloudy version, there are too many people, you need to declare it first
dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" }}2 Define profile related configuration
//Configuration of default file paths sourceSets { integTest { java.srcDir file('src/test/java') resources.srcDir file('src/test/resources') }}task integTest(type: Test) { testClassesDirs = sourceSets.test.output.classesDirs classpath = sourceSets.test.runtimeClasspath}3 Define the service interface and define the pseudo-method, which is the method in the service. You need to know the method parameters and its return value. You don’t have to worry about the implementation, just MOCK in the unit test.
package test.lind.javaLindDay.feignClientDemo;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.context.annotation.Profile;import org.springframework.web.bind.annotation.GetMapping;/** * Simulate other services. */@Profile("!integTest")@FeignClient(name = "serviceName")public interface MockClient { @GetMapping(path = "/balanceSheet/{clientCode}") String balanceSheet(String clientCode);}4 The role of Profile
Profile is an environment variable. You activate it through ActiveProfile on the class. When using it, you have used Profile annotations. In the above code, the MockClient object cannot be used in the integTest environment.
5 Added the MOCK implementation, it is automatically injected, so declare the @Bean annotation
package test.lind.javaLindDay;import static org.mockito.ArgumentMatchers.anyString;import static org.mockito.Mockito.mockito.mockito;import static org.mockito.Mockito.when;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Profile;import test.lind.javaLindDay.feignClientDemo.MockClient;@Configuration@Profile("integTest")public class MockClientTest { @Bean public MockClient mockClient() { MockClient client = mock(MockClient.class); when(client.balanceSheet( anyString())) .thenReturn("OK"); return client; }}6 Add unit tests, be sure to specify its environment variables on unit tests
package test.lind.javaLindDay;import static org.junit.Assert.assertEquals;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.ActiveProfiles;import org.springframework.test.context.junit4.SpringRunner;import test.lind.javaLindDay.feignClientDemo.MockClient;@RunWith(SpringRunner.class)@SpringBootTest//Specify the profile environment @ActiveProfiles("integTest")public class JavaLindDayApplicationTests { @Autowired MockClient mockClient; @Test public void testMockClient() { assertEquals(mockClient.balanceSheet("OK"), "OK"); }}After running the test, the MockClient will be injected, and it will use the Mock implementation class, because only the Profile of the Mock implementation class points to the integtest environment.
With openfeign, the service calls to the development service can be decoupled in the future!
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.