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

其实还有一种,就是项目之间,全用map来传递。当生产者数据架构改变时候,只要消费者未使用到生产者改变的字段就不会受到影响。不过这样需要对数据结构比较清晰,或者文档比较完善。感觉都不完美...
微信群在QQ群中发,QQ技术群:
