In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use the openfeign framework for decoupling, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Preface
In microservice design, the invocation between services is very normal, usually we use httpClient to call remote resources, and this method requires the address of the knowledge service, business interface address, etc., and you can not call it until his development is completed, which is not a good thing for integrated development, resulting in a strong dependency between A business and B business, so how do we decouple it? The answer is the openfeign framework, which is part of springcloudy.
1 add package reference
'org.springframework.cloud:spring-cloud-starter-openfeign'
Note: if you don't quote the springcloudy version, you need to declare it first.
DependencyManagement {imports {mavenBom "org.springframework.cloud:spring-cloud-dependencies:$ {springCloudVersion}"}}
2 define profile related configuration
/ / default configuration of some 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, define pseudo-methods, that is, the methods in the service. You need to know the method parameters and their return values. Do not worry about the implementation, you can only 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;/** * simulates 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 you use it, you have Profile comments to use it. The MockClient object in the above code cannot be used in the integTest environment.
5 add the MOCK implementation, which is automatically injected, so declare the @ Bean annotation
Package test.lind.javaLindDay;import static org.mockito.ArgumentMatchers.anyString;import static org.mockito.Mockito.mock;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 a unit test, and be sure to specify its environment variable on the unit test
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// specifies the profile environment @ ActiveProfiles ("integTest") public class JavaLindDayApplicationTests {@ Autowired MockClient mockClient Test public void testMockClient () {assertEquals (mockClient.balanceSheet ("OK"), "OK");}}
After running the test, 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 development service can decouple the service invocation later!
These are all the contents of the article "how to decouple using the openfeign Framework". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.