标签:base spring art host arc 提供者 post 代码示例 exp
公司项目目前使用 SOA 架构(Service-oriented architecture:服务导向架构),每个服务作为一个单体对外提供接口,服务之间使用 GitHub OpenFeign 调用接口。
服务提供者正常提供接口,把需要暴露的接口封装为一个 client jar 包,服务调用者正常引入 jar 包,直接调用 jar 方法即可调用服务提供者接口。此文相关代码示例
@RestController
public class UserController {
@PostMapping("/signin")
public ApiResponse<Void> doSignin(@RequestParam("username") String username, @RequestParam("password") String password) {
if (username.equals("") && password.equals("")) {
return ApiResponse.fail(500);
}
return ApiResponse.success();
}
}
ProviderClientBuilder
@Configuration
public class ProviderClientBuilder {
@Value("${provider.service}")
public String providerService;
@Value("${provider.socketTimeout:60000}")
public int socketTimeout;
@Value("${provider.connectTimeout:10000}")
public int connectTimeout;
@Bean
public UserApi buildProviderApi() {
Preconditions.checkArgument(StringUtils.isNotBlank(providerService));
return Feign.builder()
.decoder(new GsonDecoder())
.encoder(new FormEncoder())
.logger(new Slf4jLogger(UserApi.class))
.retryer(Retryer.NEVER_RETRY)
.logLevel(Logger.Level.FULL)
.client(new AccessTraceHttpClient(httpClient(), requestConfig()))
.target(UserApi.class, providerService);
}
...
}
UserApi
@Headers("Content-Type: application/x-www-form-urlencoded") // 请求的编码格式为 application/x-www-form-urlencoded
public interface UserApi {
@RequestLine("POST /signin")
public ApiResponse<Void> signin(@Param("username") String username, @Param("password") String password);
@RequestLine("GET /status/version")
public ApiResponse<String> statusVersion();
}
@RestController
public class HomeController {
@Autowired
private UserApi userApi;
@PostMapping("/signin")
public ApiResponse<Void> signin() {
return userApi.signin("admin","admin");
}
}
@ComponentScan(basePackages = "wang.depp")
@SpringBootApplication
public class ConsumerApiApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApiApplication.class, args);
}
}
ProviderClientBuilder 为配置类,当 Provider 和 Consumer package 路径不同时,需要指定包扫描路径,使引入包配置类在容器启动时就加载到容器。
application.properties
server.port=8888
provider.demain=http://localhost:8080/ # 完整书写
provider.socketTimeout=60000
provider.connectTimeout=10000
pom.xml
<dependency>
<groupId>wang.depp</groupId>
<artifactId>provider-client</artifactId>
<version>1.0.6</version>
</dependency>
1、java.lang.IllegalArgumentException: target values must be absolute.
${*}
2、Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
3、接收参数不能为 Date
application/x-www-form-urlencoded
传输时将参数链接到 URL,将转换为 String可通过为不同环境配置不同的服务提供者 domain
标签:base spring art host arc 提供者 post 代码示例 exp
原文地址:https://www.cnblogs.com/deppwang/p/13917285.html