主要是针对Spring Cloud新出的版本(CloudFinchley.RC2与Springboot2.0.2.RELEASE),一些新的改动,与在使用中遇见的一些问题,踩过的坑,希望后面的人就不用踩了。
服务注入到Eureka需要的MAVEN配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
如果找不到包加入下面:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
如果还是不行,再加
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
这个呢,是因为比较新,maven可能还么相关的包。
application.yml配置
spring:
application:
name: deal-center
server:
port: 8902
eureka:
client:
serviceUrl:
defaultZone: http://admin:123123@127.0.0.1:8000/eureka/
启动类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class DealCentreApplication {
public static void main(String[] args) {
SpringApplication.run(DealCentreApplication.class, args);
}
}
编写Feign接口类
@FeignClient("user-center")
public interface UserService {
@GetMapping("/user/getUserById")
public User getUserById(@RequestParam("id")Long id);
@GetMapping("/user/getById/{id}")
public User getById(@PathVariable("id")Long id);
@GetMapping("/user/queryUserList")/*这个是不对的*/
public List<User> queryUserList(@RequestBody User user);
}
Feign请求类中,第一个和第二个,都可以请求通。第三个不行。如果都换成POST请求,也可以请求正确。
在SpringCloud 1.5版本的时候,@GetMapping和@PostMapping不支持,现在已经可以了。但是复杂参数时还是不行,报错如下:
UserService#queryUserList(User); content:
{"timestamp":"2018-05-30T09:15:02.095+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/user/queryUserList"}
意思是说不支持POST请求,可是明明发的是POST请求啊,跟代码走...发现一段代码如下:
privatesynchronizedOutputStream getOutputStream0()throwsIOException {
try{
if(!this.doOutput) {
thrownewProtocolException("cannot write to a URLConnection if doOutput=false - call setDoOutput(true)");
}else{
if(this.method.equals("GET")) {
this.method ="POST";
}
}
}
}
所以是不能接受requestbody方式传递参数的。
问题来了:feign通过jdk中的HttpURLConnection向下游服务发起http请求(源码详见feign.Client.Default)
@Override
public Response execute(Request request, Options options) throws IOException {
HttpURLConnection connection = convertAndSend(request, options);
return convertResponse(connection).toBuilder().request(request).build();
}
HttpURLConnection convertAndSend(Request request, Options options) throws IOException {
final HttpURLConnection
connection =
(HttpURLConnection) new URL(request.url()).openConnection();
if (connection instanceof HttpsURLConnection) {
HttpsURLConnection sslCon = (HttpsURLConnection) connection;
if (sslContextFactory != null) {
sslCon.setSSLSocketFactory(sslContextFactory);
}
.........
得出结论:缺乏连接池的支持,在达到一定流量的后服务肯定会出问题 ,我想用Apach的httpclient替换掉了原生的UrlConnection,网上找了下资料,发现有专门针对这个的一个包如下:
<!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-httpclient -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
切要求这个包版本与feign-core的版本保持一致,并在application.yml中添加配置如下:
feign:
httpclient:
enabled: true
新的CloudFinchley.RC2版中这不需要这个了,引入此包后,Spring Cloud会将LoadBalancerFeignClient中的Client注入为ApacheHttpClient。默认的连接池信息在FeignHttpClientProperties。
说说项目结构
其实还有一种,就是项目之间,全用map来传递。当生产者数据架构改变时候,只要消费者未使用到生产者改变的字段就不会受到影响。不过这样需要对数据结构比较清晰,或者文档比较完善。感觉都不完美...
微信群在QQ群中发,QQ技术群: