我正在尝试为生产者的存根作为依赖对象的消费者创建一个(胖)弹簧靴子。这样,当我启动消费者应用程序时,它也应该启动合同存根。
这是我到目前为止所做的:
我使用了此pom示例(https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/producer_with_external_contracts/pom.xml#L98),并为生产者创建了一个胖子
producer.pom
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>${spring-cloud-contract.version}</version>
<configuration>
<baseClassMappings>
<baseClassMapping>
<contractPackageRegex>.*contract.*</contractPackageRegex>
</baseClassMapping>
</baseClassMappings>
<contractDependency>
<groupId>com.groupId</groupId>
<artifactId>producer</artifactId>
</contractDependency>
<contractsMode>LOCAL</contractsMode>
<classifier>stubs</classifier>
<basePackageForTests>com.groupId.producer</basePackageForTests>
<convertToYaml>true</convertToYaml>
</configuration>
</plugin>
然后将其导入到我的Consumer.pom中,如下所示:
<dependency>
<groupId>com.groupId</groupId>
<artifactId>producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
应用程序属性如下: 生产者-application.properties server.port = 8081
消费者-application.properties
stubrunner.ids=com.groupId:producer:+:stubs:8081
stubrunner.stubsMode=LOCAL
stubrunner.repositoryRoot=com.groupId.producer
stubrunner.minPort=8081
ControllerTests.java
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
@AutoConfigureJsonTesters
@AutoConfigureStubRunner( ids = "com.groupId:producer:+:stubs:8081",
stubsMode = StubRunnerProperties.StubsMode.LOCAL )
@DirtiesContext
public class ContractTests extends AbstractTest {
...
}
ConsumerApplication.java
@SpringBootApplication
@EnableWebMvc
@EnableStubRunnerServer
@Slf4j
public class ConsumerApplication {
...
}
ContractService.java
public class ContractService {
public @ResponseBody MockResponse verifyWithContract(SomeObj someObj) {
ResponseEntity<MockResponse> response = this.restTemplate.exchange(
RequestEntity
.post(URI.create( http://localhost:8081/someEndpoint ))
.contentType(MediaType.APPLICATION_JSON)
.body(someObj), MockResponse.class);
return response.getBody();
}
}
我有2个问题:
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ org.springframework.cloud.contract.stubrunner.server.HttpStubsController”的bean时出错:通过构造函数参数0表示的不满意依赖关系;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建在org.springframework.cloud.contract.stubrunner.spring.StubRunnerConfiguration中定义的名称为'batchStubRunner'的bean时出错:通过工厂方法的Bean实例化失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.cloud.cloud.contract.stubrunner.BatchStubRunner]:工厂方法'batchStubRunner'引发了异常;嵌套异常为java.lang.IllegalStateException:未指定存根的远程存储库,并且未传递脱机工作标志 在org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769)〜[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE] 在org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218)〜[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
我也在测试中尝试了以下方法,但这没有帮助:
@AutoConfigureStubRunner( ids = "com.groupId:producer:+:stubs:8081",
stubsMode = StubRunnerProperties.StubsMode.REMOTE, rootRepository="com.groupId.producer" )
2019-04-09 11:38:50.454 INFO 75383 --- [ main] o.s.c.contract.stubrunner.StubServer : Started stub server for project [com.groupId:producer:0.0.1-SNAPSHOT:stubs] on port -1
2019-04-09 11:38:50.454 INFO 75383 --- [ main] o.s.c.c.stubrunner.StubRunnerExecutor : All stubs are now running RunningStubs [namesAndPorts={com.gorupId:producer:0.0.1-SNAPSHOT:stubs=-1}]
2019-04-09 11:38:50.673 INFO 75383 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (https) with context path ''
但是当我向其余网址发送请求时:https://localhost:8080/someEndpoint 它返回以下错误:
{
"timestamp": 1554837050058,
"status": 500,
"error": "Internal Server Error",
"message": "I/O error on POST request for \"http://localhost:8081/someEndpoint\": Connect to localhost:8081 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8081 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)",
"path": "/someEndpoint"
}
日志行:
2019-04-09 12:10:50.045 ERROR 75383 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8081/someEndpoint": Connect to localhost:8081 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused); nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8081 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)] with root cause
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_171]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_171]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_171]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_171]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_171]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_171]
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:75) ~[spring-cloud-contract-shade-2.1.1.RELEASE.jar:2.1.1.RELEASE]
我认为我缺少stubrunner端口的一些stubRunner配置。