最近因为之前了解了dubbo,spring cloud的源代码(粗颗粒的了解,嘻嘻~),今天没事,搜搜网上(垃圾)教程,想学着搭建个spring cloud。没料到又开启了自闭的一天(有新手在公司做项目的感觉了,又有那味了),还好我是公费自习。

看的搭建教程是如下(但是他好像也不是原创):
使用IDEA搭建springcloud微服务(一)----父工程的搭建
使用IDEA搭建springcloud微服务(二)-----Eureka服务注册中心(BASE认证)
使用IDEA搭建springcloud微服务(三)----微服务服务方cloud-provider
使用IDEA搭建springcloud微服务(四)----微服务消费方cloud-client
使用IDEA搭建springcloud微服务(五)----微服务运行展示

他的版本和我的电脑不同,存在的几个问题:

1. 我目前用的IDEA版本2017.2.6搭建项目时:
①选项略有些不同,比如feign,我这儿只有选openfeign
②spring boot没他那低版本而就用了默认选的2.2.1
③我搭建出来生成的pom里springcloud版本是<spring-cloud.version>Hoxton.RC1</spring-cloud.version>,并且我这pom里还有<repositories>,和他的教程不符,不改成他那样也没事,无伤大雅。
④学到了接口&内部类且该类又要实现某方法,看起来很诡异的文件。(UserFeignClient.java)

在这里插入图片描述
IDEA创建出module后,红框内这些文件可以删掉,看着更清爽。


开启一天踩坑之路:

一上午,按照他的步骤搭建完之后(夸一波他的教程,的确相当详细,一步一步,在垃圾遍地的教程中,可以看出费了不少心血。对没有玩过分pom的同学是一个很好的示范),但是启动不了,自闭了。
一、
Eureka服务注册中心(BASE认证) cloud-discovery
启动不了,报错

java.lang.IllegalStateException: Failed to introspect annotated methods on class

解决办法:
https://blog.csdn.net/qq_32198005/article/details/78435648

In maven, I changed the scope like this: <scope>provided</scope> to <scope>compile</scope> and it worked!!. 

然后,又报错

Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception;  nested exception is java.lang.IllegalStateException: Can't configure anyRequest after itself

看不懂,但是又是什么Chain的又是什么Security的,我想是不是启动项里静态内部类WebSecurityConfigure错了,干脆注了

@EnableEurekaServer
@SpringBootApplication
public class CloudDiscoveryApplication {
	public static void main(String[] args) {
		SpringApplication.run(CloudDiscoveryApplication.class, args);
	}
	/*@EnableWebSecurity
	static class WebSecurityConfigure extends WebSecurityConfigurerAdapter {
		@Override
		protected void configure(HttpSecurity http) throws Exception {
			// 在/eureka/**端点忽略csrf验证
			http.csrf().ignoringAntMatchers("/eureka/**");
			// 配置使请求需要通过httpBasic或form验证
			http.authorizeRequests().anyRequest()
					.authenticated()
					.and()
					.formLogin()
					.and()
					.httpBasic();
			super.configure(http);
		}
	}*
}

欸,能启动了,很神奇。不懂。有无懂哥说说看原理。
在这里插入图片描述
中途岔道:
因为想试试看什么地方变动可以跑起来,所以copy了一份程序到其他文件夹,用那份程序试验,没想到如此又搞了一些幺蛾子:
1.编码UTF-8(csdn只能手动复制,别点代码右上角“复制”功能,会中文乱码的)、jar包的神奇问题
2.莫名其妙说pom没slf4j
3.client里的注解@EnableFeignClients,@EnableDiscoveryClient死活导不到jar包

于是,又换回在原程序文件上操作了,不过也仍出现一个莫名其妙的坑(在下文cloud-client里再写)。
一些其他乱七八糟的改动,但是发现不必要:

<parent>
		<!--<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
		<relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
		<groupId>com.cloud</groupId>
		<artifactId>spring-cloud</artifactId>
		<version>1.0-SNAPSHOT</version>
</parent>

<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<!--<spring-cloud.version>Hoxton.RC1</spring-cloud.version>-->
		<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
	
<dependencies>
	<!-- tomcat支持 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>compile</scope>
		</dependency> 
	<!-- 新增酸辣粉4j 也可直接在父类pom加而一了百了。(后来发现不需要,应该是这份copy工程是直接复制来的而打开项目导致的蛋疼)  抽风了,加了后当时正常,但在再次点开pom时发现pom头<project标签也仍仍仍仍仍冒红,maven bug了 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.25</version>
		</dependency>
</dependencies>

<!--	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
	</repositories>-->	

二、
微服务服务方cloud-provider
注册中心启动了之后,我以为事儿就成了,抓紧想体验一下远程调用的舒适感,不料,一启动provider

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

解决办法:
https://www.cnblogs.com/expiator/p/9696887.html
provider的配置中添加(而注册中心的配置中是有的(见篇首他的教程),不知provider这里为何还得再配一次)

eureka:
  client:
    registerWithEureka: false   
    fetchRegistry: false

至此,三座大山终于翻过了两座o(╥﹏╥)o,provider也“顺利”启动了
在这里插入图片描述
三、
微服务消费方cloud-client

上文说到,换回在原程序文件上操作了,不过也仍出现一个莫名其妙的坑:
client启动时它就自闭了——

2019-11-08 17:51:52.368  INFO 56440 --- [  main] o.s.c.n.e.s.EurekaServiceRegistryRegistering application CLOUD-CLIENT with eureka with status UP
	                            Unregistering application CLOUD-CLIENT with eureka with status DOWN

解决办法:
百度到的说法都是说缺spring-boot-starter-web,但我明明有啊,莫名其妙的,于是我重复放个依赖,

<dependencies>
		<!--web依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--我再放一次-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>	

特么的就好了(但只是能启动而已,当时18岁的我不知道后面还有一个天坑在等着我o(╥﹏╥)o)
在这里插入图片描述但是,新问题是,访问网址localhost:8082/user/1,错误,显示springboot的错误页面,F12看请求报500,http://localhost:8080/ 里看 Instances currently registered with Eureka 里 No instances available(不过后来跑通了后这里也啥都没,和教程的显示不同。是网页后台抽风了吗???)
控制台报错:com.netflix.client.ClientException: Load balancer does not have available server for client: cloud-provider
又开始自闭百度,看了很多垃圾,最后终于看到了一篇懂哥:
https://blog.csdn.net/IManiy/article/details/86505652
可以从注释的密集程度看到debug的辛酸坎坷:
client的application.yml

#ribbon:
 # okhttp:
  #  enabled: true
  #restclient:
   # enabled: true
  #eureka:
   # enabled: true

#hystrix:
 # command:
  #  default:
   #   execution:
    #    isolation:
     #     thread:
      #      timeoutInMilliseconds: 5000
	  
ribbon:
  eureka:
    enabled: false
cloud-provider:
  ribbon:
    listOfServers: http://localhost:8081

provider的pom

#报错啦  Cannot execute request on any known server
#security:
#  basic:
#    enable: false  #经测试,不顶用

#ribbon:
 # okhttp:
  #  enabled: true
  #restclient:
   # enabled: true

折腾了一天,终于:
在这里插入图片描述

------------------------------谢邀,以上------------------------------------
人在美国,刚下飞机。
bug滚出我的魔仙堡。
这么好的天气的日子不用来debug真是太可惜了呢。

-----------附github代码下载地址
https://github.com/FA-CAI/springcloud

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐