读书笔记 spring-cloud Finchley.SR2版本 升级微服务到springboot 2.0
贴码云
https://gitee.com/imbobo_bo/angel-bo
码云上的代码已经升级完成
1. 修改 SpringBoot 依赖 到 2.0.6
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2. 修改 SpringCloud 依赖 到 Finchley.SR2
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3. SpringCloud 版本兼容 SpringBoot
https://spring.io/projects/spring-cloud看这里
4. Eureka依赖变更
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
5. hystrix依赖变更
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
6. ribbon依赖变更
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
7. zuul依赖变更
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
8. feigen依赖变更
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
9. center 变更
由原来的
security:
user:
name: angel
password: angel
变为了
spring:
security:
user:
name: angel
password: angel
10. ErrorController 依赖变更
再处理全局异常时依赖变更
org.springframework.boot.autoconfigure.web.ErrorController;
变为
import org.springframework.boot.web.servlet.error.ErrorController;
11. Eureka 认证
You can secure your Eureka server simply by adding Spring Security to your server’s classpath via spring-boot-starter-security. By default when Spring Security is on the classpath it will require that a valid CSRF token be sent with every request to the app. Eureka clients will not generally possess a valid cross site request forgery (CSRF) token you will need to disable this requirement for the /eureka/**
CSRF认证配置,不认证注册客户端
@EnableWebSecurity
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
12. spring-boot-admin 接入
看这个就可以了,但一定对应上版本 http://codecentric.github.io/spring-boot-admin/2.0.4/#_what_is_spring_boot_admin
. 看SpringCloud总结
.1 @RefreshScope
刷新bean属性
.2 @EnableDiscoveryClient
向注册中心注册服务,新版本可以自己实现 ServiceRegistry 向不同的注册中心注册,前提是要禁用自动注册autoRegister=false
.3 Retrying Failed Requests
将Spring-Retry 引入到classpath中
client.ribbon.MaxAutoRetries,
client.ribbon.MaxAutoRetriesNextServer,
client.ribbon.OkToRetryOnAllOperations
这些属性是关于retry的配置
*可以 spring.cloud.loadbalancer.retry.enabled=false 关闭这个功能*
.4 Ignore Network Interfaces
spring:
cloud:
inetutils:
ignoredInterfaces:
- docker0
- veth.*
spring:
cloud:
inetutils:
preferredNetworks:
- 192.168
- 10.0
You can also force the use of only site-local addresses, as shown in the following example: .application.yml
spring:
cloud:
inetutils:
useOnlySiteLocalInterfaces: true
.5 Why Is It so Slow to Register a Service
eureka.instance.leaseRenewalIntervalInSeconds
可以通过这个属性修改,小于30s 但坚持默认会更好,官网是这么说的
.6 在像Eureka注册时 可以设置Zone
eureka.instance.metadataMap.zone = zone1
eureka.client.preferSameZoneEureka = true
可以通过设置zone 可以让服务优先在同一个zone下查找
.7 ribbon 文档
https://github.com/Netflix/ribbon/wiki/Working-with-load-balancers#components-of-load-balancer
.8 Using Ribbon with Eureka 这个不是很明白
.9 配置zuul
9.1 看例子
https://github.com/spring-cloud-samples/sample-zuul-filters
9.2 可以为路由加上正则表达式
@Bean
public PatternServiceRouteMapper serviceRouteMapper() {
return new PatternServiceRouteMapper(
"(?<name>^.+)-(?<version>v.+$)",
"${version}/${name}");
}
9.3 可以为路由配置指定服务器不加前缀
zuul:
routes:
users:
path: /myusers/**
stripPrefix: false
9.4 可以设置路由重试次数 retryable
If you set a default route (/), an application with @EnableZuulProxy could act as a standalone server. For example, zuul.route.home: / would route all traffic ("/**") to the "home" service.
9.5 如果设置default route (/) 则这个可以作为一个单独的服务,接受请求
9.6 在设置路由时,如果需要有顺序要求
zuul:
routes:
users:
path: /myusers/**
legacy:
path: /**
9.7 要使用yaml配置,因为使用proterties时,顺序是无效的
9.8 敏感信息
zuul:
routes:
users:
path: /myusers/**
sensitiveHeaders: Cookie,Set-Cookie,Authorization
url: https://downstream
sensitiveHeaders: Cookie,Set-Cookie,Authorization 是默认的,不会转发给下游服务 如果想向下游发送这些敏感信息,记得要清空这个 可以直接用zuul.sensitiveHeaders 设置全局的
9.9 zuul会增加一个默认的Endpoint
GET /routes
GET /routes/details.
可以通过endpoints.routes.enabled 来禁用
9.10 上传文件
有一个前缀 /zuul/*
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
ConnectTimeout: 3000
ReadTimeout: 60000
9.11 设置zuul servlet 默认是 /zuul 可以通过 zuul.servlet-path 来设置
9.12 zuul 启动时加载ribbon相关的上限文
zuul:
ribbon:
eager-load:
enabled: true
.10 feign
@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {
//..
}
这样可以让当前feign客户端使用配置,但是 这个FooConfiguration配置不能有@Configuration注解,否则会变成全局的配置,别切还不能呗@CompoentScan 或者其他注解扫描到
.11 fallbackFactory
@FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class)
protected interface HystrixClient {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
Hello iFailSometimes();
}
@Component
static class HystrixClientFallbackFactory implements FallbackFactory<HystrixClient> {
@Override
public HystrixClient create(Throwable cause) {
return new HystrixClient() {
@Override
public Hello iFailSometimes() {
return new Hello("fallback; reason was: " + cause.getMessage());
}
};
}
}
.12 feign logger
@Configuration
public class FooConfiguration {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}