SpringCloud Finchley.M9尝鲜记录

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

  • 配置服务器

因为懒得在git上创建配置文件,所以配置成从本地读取配置文件:

spring:
  profiles: 
    active: native

 

  • Eureka服务注册与发现

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

#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发生了变化:

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基本没什么区别。

  • Hystrix服务熔断

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

<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>

猜你喜欢

转载自blog.csdn.net/singularinty/article/details/80826862