SpringCloud Finchley.M9尝鲜记录

写在开头,这篇不是小白教程,顶多算是碰坑记录,甚至可能连问题都没有描述清楚。html

由于之前弄过基于SpringBoot 1.X,因此忽略了一些基本配置,这个版本基于SpringBoot2.0.0,和1.X的没有很大区别,网上挺多1.X配置的教程,搭建过程这里不过多说明。java

主要搭建Netflit家族的几个部分,包括配置服务器、Eureka服务注册与发现、Ribbon负载均衡、Feign客户端、Hystrix服务熔断、Zuul网关等。git

  • 配置服务器

由于懒得在git上建立配置文件,因此配置成从本地读取配置文件:github

spring:
  profiles: 
    active: native

 

  • Eureka服务注册与发现

也比较简单,按照官方文档搭建好注册中心和服务,为了使配置服务器有所做用,将服务端口放在配置服务器上的配置文件上。
主要是Eureka Server +Spring Security认证配置时出了问题,配置文件上:web

#1.X版本:

security:
  basic:
    enabled: true
  user:
    name:
    password: 

#变成了:

spring:
  security:
    basic:
      enabled: true
    user:
      name:
      password: 

#并且只要添加了Spring Security Starter依赖,spring.security.basic.enabled默认就是true,改为false也没用。

启动起来,客户端报异常:Cannot execute request on any known server。致使服务注册失败。
各类检查配置,折腾了几个小时也没搞好,最后在GitHub上找到了答案【https://github.com/spring-cloud/spring-cloud-netflix/issues/2754】,原来是SpringBoot从2.0.0.RC1升级到2.0.0.RELEASE的时候,有个类SpringBootWebSecurityConfiguration发生了变化:spring

​public class SpringBootWebSecurityConfiguration {

    @Configuration
    @Order(SecurityProperties.BASIC_AUTH_ORDER)
    static class DefaultConfigurerAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            super.configure(http);//新包这两行被删了
            http.csrf().disable();
        }
    }
}

新建这个类放在Eureka Server项目里面就能够了;或者将SpringCloud降到Finchley.M6及如下,同时SpringBoot降级到2.0.0.RC1,只能说尝鲜需谨慎。。。服务器

  • Ribbon负载均衡

这个也没什么好说的,太简单了。负载均衡

  • Feign客户端

也没什么好说,官方把Feign改为了OpenFeign,用法和1.X基本没什么区别。ide

  • Hystrix服务熔断

在服务消费端规规矩矩改好配置,添加依赖:spring-boot

​
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

​

启动,又遇到坑了!!!
按照官方文档访问http://host:port/hystrix.stream 查看断路由状态发现是404,根本就没有暴露hystrix.stream这个端点。又折腾了将近一个小时,才在另一个文档上找到答案:

https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

原来是Actuator这个包变化了,默认只暴露health等几个端点,好吧,把配置加上:

management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream

启动后观察日志发现暴露的服务url居然是/actuator/hystrix.stream,固然这也能够用,若是不想要/actuator,添加如下配置便可:

management:
  endpoints:
    web:
      base-path: /

被SpringCloud官方文档坑了一把。

  • 配置更新

回头弄一下配置的刷新,配置服务器上的配置文件更新,其它服务能够不重启而获取最新配置。
在服务消费端进行了修改,理所固然在post http://host:port/refresh 时又是404,有了上面hystrix.stream的教训,立刻在配置上增长:

management:
  endpoints:
    web:
      exposure:
        include: refresh


OK,经过,配置刷新了。
单个服务刷新没问题,再集成Spring Cloud Bus来批量刷新试试吧,这里用了Kafka:<br/>

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


这下我聪明了先改配置文件把bus/refresh端点暴露出来,启动,post http://host:port/bus/refresh ,404!!!
好吧,不要过于相信官方文档,修改一下配置把全部端点暴露,观察启动日志发现有一个bus-refresh,post一下 http://ip:port/bus-refresh ,bingo,是想要的东西。

  • Zuul网关

没遇到什么问题了,主要是前面在服务上加了Spring Security,网关访问服务须要加上认证信息,具体看本身实现的PreFilter。

 

确实变化不大,变的基本都在Actuator里面了,陆陆续续随手写,后续再看要不要完善,顺手贴上示例地址:

https://gitee.com/cionhome/SpringCloudDemo

参考:

官方文档

Github

相关文章
相关标签/搜索