Spring Probe Micro Cloud Services

The original intention of learning

Because the addition of a lot of good knowledge of the planet, make more small partners, add more groups, often at the time of self-introduction, say they are Android & Java siege lion.

However, geese, some little friends came to ask, you are engaged in Java, that are familiar strategy of Spring? Oh, big brother, Spring Cloud engage how about it? I am a micro-development services, and you expect to communicate with the exchange ...... such a friend, I feel very ashamed.

In fact, I was a middleware development. My current technology stack is based Netty, WebSocket working on a chat server, so these I would not ah. However, in the circle we have indeed heard the word micro-services, know that many companies are already using micro very familiar with the service, and then if I can not keep up, it would really have to kneel. So, give yourself a small target, micro-services must be turned on tour, even if only practiced hand.

Micro service, here I come

Small series of reference books] [ape world's main number, Yoon Ji Huanzhuan wrote "Spring Cloud micro-services." Here's rivers and lakes have to thank [Domingo] number of the primary Domingo donated books, I can be considered to save books for the meal.

Initial contact Spring Cloud, saw various versions, just started a little ignorant force. For everyone to look at the latest version of Spring Cloud of what it was like.

These versions naming names using the name of the London Underground station, but according to the order of the alphabet to the corresponding version of chronological order. For example, the earliest version of Release: Angel, Second Release version: Brixton, followed by Camden, Dalston, Edgware, Finchley, Gerrnwich, and now the latest version is a snapshot of the Hoxton.

SNAPSHOT: snapshot version, may at any time modify
SR: Service Release, SR1 represents the first official version, while the general labeling GA: (GenerallyAvailable), represents a stable version.

Therefore, Xiao Bian here to select the latest Greenwich SR1 version. Corresponding, Spring Boot at the official website where there will be introduced. Greenwich is the corresponding 2.1.x, because small series are beginners, so also with the latest version, 2.1.5 Release . Version of choice is important, it is important to choose the version, version selection is very important , important things to say three times, version choose how to determine good programming after you bad way to go.

Micro Services stepped pit tour

Because the choice of the latest version, there is a lot of information very existence, but fortunately Spring Cloud 2.x version can be found by searching or part of the material, but also thanks to the large Yin line help in the learning process is to avoid the difficult part.

I call this part of his foot pit journey, because in practice, encountered many problems, step by step debugging is over, really hard. Presumably also want to see the part we are more immediately goes to.

  1. Importing Spring Boot 2.1.5 version is being given a start

In the first row pom.xml file, always reported "Maven Configuration Problem, Unknwon", it is silent with wood there, you chant error on the error, and gave me an unkown, so I can not start.

The answer is to find the answer on Stackoverflow, as follows

This step is shining, the environment where I was solved, if you have a similar error, and not a perfect solution, can communicate with each other exchanges.

  1. Import dependency encountered missing

There is a kind of problem, Xiao Bian just get started, learning is registered, it would have used eureka components. The book version is used Dalston version, Sping boot with the 1.5.x, it is dependent on imported

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

However, in this upgrade version on the error, simply did not find a dependent. There are several need to use the back spring-cloud-starter-xxx place, it will be reported the same error. such as

<artifactId>spring-cloud-starter-hystrix</artifactId>

也是如此。遇到此类情况,我们需要更换下地址,得把netflix这个重要的加上,加这个的原因,不仅仅是为了升级,我想也是Netflix公司为了更好的宣传自家的产品吧。解决方案就是

<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  1. 基于第二条,也有例外的情形

原本以为这个套路很深,都加netflix就搞定了,但是遇到feign组件的是,这个不奏效了,摊手。

<artifactId>spring-cloud-starter-feign</artifactId>

加了netflix也不奏效,好吧,继续努力寻找答案去呗。因为这里找的资料比较多了,来源有点不清晰,最后的结果就是将这个依赖改成如下即可

<artifactId>spring-cloud-starter-openfeign</artifactId>
  1. eureka开启验证,登录的用户名和密码始终错误

eureka开启验证之后,需要在配置文件里把用户名和密码配置上去,否则就会有默认的用户名“user”,以及在控制台自动生成一串密码供我们登录。

小编刚开始的配置,还是按照书上写的

用户名
security.user.name=xxx
密码
security.user.password=xxx

结果启动的时候,看到控制台上还是生成了一串密码,并且输入了自己在配置文件中的用户名和密码,都是进不了注册后台的。这个时候,就彻底懵逼了,代码也看不到错误,配置文件错误的话,也看不到啥信息,如何是好。

最后,小编在CSDN上找到一篇名为《security.basic.enabled 配置过时或不可用》的文章里,看到了,2.x版本的属性配置,是需要加前缀spring的,也就是如下:

spring.security.user.name=xxx
spring.security.user.password=xxx
  1. eureka开启关闭认证方式

继续上一个问题的补充,在开启验证的时候,老版本是需要在配置文件里将eureka开关打开,也就是用

security.basic.enabled=true

只不过当小编把值改成false的时候,还是出现需要用户名和密码登录的验证框。在没看上一篇的解决方案之前,也确实有点不知所措,后来看了CSDN的那篇解决方案下面就有了答案。

结合其他地方的资料,总结起来就是:旧版本的Spring Security的依赖是可以在配置文件内容直接通security.basic.enabled参数进行开启basic认证,不过目前版本已经被废除,既然这种方式不可行,那我们就使用另外一种方式进行配置,通过继承WebSecurityConfigurerAdapter安全配置类来完成开启认证权限

@EnableWebSecurity
    static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable().authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .httpBasic();
        }
    }
  1. 代码在编译阶段报错

这里小编遇到了两次,解决方式都是同样的。第一个是遇到Caused by: java.lang.ClassNotFoundException: com.google.common.collect.Lists
在这里,网上有朋友就说了,如果在查阅完代码,发现没有问题的时候,那么有一种可能是你的此依赖包并没有下载完整。记住这个重点哦,小编就是因为第一次遇到的时候没在意,第二次还碰到了。

第一次出现的时候,小编直接看到就去删了maven的依赖,在这个错误下删除的路径是“/repository/com/google/guava/guava” ,然后就可以编译成功,并执行完成了。到第二次,我竟然又踩坑。

第二次的错误是一堆堆栈报错

看到小编划的红线了没,其实这个时候,已经很明确告诉我们是没找到包下的类,但是小编一开始并没有仔细看这个错,而是去搜索引擎上查找答案。这次遇到麻烦了,全网就一篇类似的文章,还各种中文简体、繁体、英文大肆的写,可能对当时的场景有用,对我们这个场景没啥帮助。

这次,因为没有仔细吸取上次的教训,小编花了3小时来排查问题,等到夜深人静的时候,小编又在报错看了又看,看了又看,把眼泪都看出来了。好的,最终决定去删除一下依赖包。

就在小编没报任何希望的时候,代码竟然过了,跑的不亦乐乎。写程序的你,能感受到那份兴奋吧。噢,如果你是大拿,那可能不会这么强烈,不过我是达到高潮了,这个必须高潮。在此之前,小编在知识星球上求助了,贴子刚发完,没想到啊没想到,看来有时候不逼自己一把,还真不知道自己潜力有多少。

  1. Feign整合Hystrix又有错

错误真的是一环扣一环,每次学习完这个组件,马上进入下一个组件,开始功能整合的时候,Bug总是不停地向你招手,招手,要是小姐姐也就算了,可惜她不是呀。

在微服务架构中,Hystrix除了实现容错外,还提供了实时监控功能。在服务调用时,Hystrix会实时累积关于HystrixCommand的执行信息,比如每秒的请求数、成功数等。

当你启动了Hystrix,并且输入http://localhost:8082/hystrix.stream这个地址的时候,原本我们期望看到的结果是能在页面中看到"ping"请求,以及请求的""data"数据,但是这次小编并没有看到。

看过前面遇到的这么多错,想必你也知道,这是因为新版本引起了的吧。这个还好说,把地址改成http://localhost:8082/actuator/hystrix.stream,
Actuator 2.x 以后endpoints全部在/actuator下,可以通过management.endpoints.web.base-path修改)

management.endpoints.web.exposure.include=*
  1. 拦路虎再次出现,这次是Zuul

这本书的第一阶段就快学完了,小编甚是兴奋。本以为到最后了,该解决的问题基本都差不多了,该遇到的问题基本上也都遇到过了,呵呵,太年轻。

Zuul在自定义异常上换包名了。其中其中的ErrorAttributes,ErrorController,DefaultErrorAttributes在SpringBoot 2.x的时候都转到org.springframework.boot.web.servlet.error包中,原本是在org.springframework.boot.autoconfigure.web上的。

所以,原先使用的

public Object errorApiHandler(HttpServletRequest request,boolean includeStackTrace) {
    RequestAttributes requestAttributes = new ServletRequestAttributes(request);
    Map<String, Object> attr = this.errorAttributes.getErrorAttributes((WebRequest)requestAttributes,includeStackTrace);
    ......
}

就不能这么使用了,得把代码换成

public Object errorApiHandler(HttpServletRequest request,boolean includeStackTrace) {
    WebRequest webRequest=new ServletWebRequest(request);
    Map<String, Object> attr = this.errorAttributes.getErrorAttributes(webRequest, includeStackTrace);
    ......
}

小结

至此,在学完《Spring Cloud微服务》的准备篇和基础篇之后,小编算是见识到了微服务的厉害。

也因为小编选择了新版本,有部分问题出现的时候并不能第一时间在网上找到解决方案,这就让自己对微服务的研究更加深入。其实一开始,小编连版本都不知道怎么选,都不知道刚开始接触的时候,是哪来的自信。

微服务社区现在已经很强大了,小编在找资料的过程中,很多疑问都是通过Netflix在GitHub上的issue找到的答案,还有尹大的GitHub上也给了我很大的帮助。这些都是宝贵的经验所在。以及尹大的【猿天地】知识星球,在那里提出问题,尹大说了,我就是在最专业的星球里,让我不能畏首畏尾,尽管放马过去。

除了需要好好使用搜索引擎之外,小编最大的感触就是要看官方文档,看官方文档,看官方文档,很多文章肯定也是根据官方文档得出的答案,在使用过程中,小编有部分问题也是在那里解决的。这次学习,单独整理了一个微服务的书签,每天看着里面的数量在增加,问题在减少,帮我一步步完成了小白到入门的进化,很是欣慰。

学习是艰辛的,尤其是遇到困扰了3小时的问题,还一筹莫展的时候,都想过放弃。但是,放弃了,我就不能在其他地方更好地给自己贴上Java开发的标签了,也不能更好地和朋友们做技术交流。想到这里,我就会去泡杯咖啡,洗把脸,继续回来努力着,坚持着,奋斗着,或许这就是技术的魅力所在吧。

爱生活,爱学习,爱感悟,爱挨踢

Guess you like

Origin www.cnblogs.com/dimple91/p/10942559.html