前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >原 荐 最新SpringCloud 服务注入

原 荐 最新SpringCloud 服务注入

作者头像
kinbug [进阶者]
发布2018-06-07 15:24:46
1.3K0
发布2018-06-07 15:24:46
举报
文章被收录于专栏:IT进修之路IT进修之路

主要是针对Spring Cloud新出的版本(CloudFinchley.RC2与Springboot2.0.2.RELEASE),一些新的改动,与在使用中遇见的一些问题,踩过的坑,希望后面的人就不用踩了。

服务注入到Eureka需要的MAVEN配置

代码语言:javascript
复制
<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>

如果找不到包加入下面:

代码语言:javascript
复制
<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>

如果还是不行,再加

代码语言:javascript
复制
<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配置

代码语言:javascript
复制
spring:
  application:
    name: deal-center
server:
  port: 8902
eureka:
  client:
    serviceUrl:
      defaultZone: http://admin:123123@127.0.0.1:8000/eureka/

启动类

代码语言:javascript
复制
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class DealCentreApplication {

	public static void main(String[] args) {
		SpringApplication.run(DealCentreApplication.class, args);
	}
}

编写Feign接口类

代码语言:javascript
复制
@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不支持,现在已经可以了。但是复杂参数时还是不行,报错如下:

代码语言:javascript
复制
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请求啊,跟代码走...发现一段代码如下:

代码语言:javascript
复制
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)

代码语言:javascript
复制
@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,网上找了下资料,发现有专门针对这个的一个包如下:

代码语言:javascript
复制
<!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-httpclient -->
<dependency>
	<groupId>io.github.openfeign</groupId>
	<artifactId>feign-httpclient</artifactId>
</dependency>

切要求这个包版本与feign-core的版本保持一致,并在application.yml中添加配置如下:

代码语言:javascript
复制
feign: 
  httpclient: 
    enabled: true

结果发现,根本就没得东东,根据我用1.5版本的记忆,找到了这个类FeignHttpClientProperties,可是不能关联类,只能反编译看了下,发现有feign.httpclient,和以前一样啊,看了好多都一样的,可是就是用不起。是用了新的方法,还是配置有BUG,有大佬解决么?

说说项目结构

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 服务注入到Eureka需要的MAVEN配置
  • application.yml配置
  • 启动类
  • 编写Feign接口类
  • 说说项目结构
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档