spring cloud
Spring Cloud是建立在Spring Boot上面的
spring cloud组件
Eureka 注册中心 [juˈriːkə]
Feign 调用 [feɪn] 声明式REST调用
Hystrix 容错 [hɪst'rɪks]
Ribbon 负载均衡
Zuul 网关
Config 远程配置
Sleuth
创建Eureka注册中心
1、IDEA:new-->project-->Spring Initializr-->输入项目名-->Spring Cloud Discovery-->Eureka Server
Discovery [dɪˈskʌvəri] 发现
2、在启动类添加注解@EnableEurekaServer
3、配置application.application
# 自己的端口号 server.port=9990 # 自己是注册中心,发布自己的地址,让别人找到自己 # 集群配置,http://127.0.0.1:9991/eureka,http://127.0.0.1:9992/eureka eureka.client.service-url.defaultZone=http://127.0.0.1:9990/eureka/ # 是否把当前项目注册到注册中心,注册中心客户端项目设置为true,此项目是注册中心服务端 eureka.client.register-with-eureka=false # 默认是true,注册中心集群同步数据,这里使用单机方式,所以关闭 eureka.client.fetch-registry=false # 安全配置 spring.security.user.name=root spring.security.user.password=root
4、springboot引入spring-boot-starter-security做安全校验后,自动开启CSRF安全认证,任何一次服务请求默认都需要CSRF 的token,而Eureka-client不会生成该token,故启动时会报如上错误。
在启动类里添加如下内部类代码:
@EnableWebSecurity static class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().ignoringAntMatchers("/eureka/**"); super.configure(http); } }
5、启动,测试
这个地址 http://127.0.0.1:9990/eureka 是给注册中心客户端使用的
浏览器直接访问http://127.0.0.1:9990
创建服务提供者
1、IDEA:new-->project-->Spring Initializr-->输入项目名-->Spring Cloud Discovery-->Eureka Discovery Client
2、在启动类添加注解@EnableEurekaClient
3、配置application.application
# 自己的端口 server.port=8800 # 把自己注册到注册中心后,别人用这个名字引用我 spring.application.name=user-provider # 注册到哪里,即注册中心地址 eureka.client.service-url.defaultZone=http://root:root@127.0.0.1:9990/eureka/ logging.level.root=trace
4、启动,测试
浏览器访问http://127.0.0.1:9990以查看服务提供者
创建服务消费者
1、IDEA:new-->project-->Spring Initializr-->输入项目名-->Spring Cloud Discovery-->Eureka Discovery Client
2、在启动类添加注解@EnableEurekaClient和@EnableFeignClients
3、配置application.application
# 自己的端口 server.port=7770 spring.application.name=user-consumer # 注册到哪里,即注册中心地址 eureka.client.service-url.defaultZone=http://root:root@127.0.0.1:9990/eureka/ # 开启容错 feign.hystrix.enabled=true # 负载均衡,默认是轮询,不需要配置;这里配置的是随机策略 user-provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule logging.level.root=trace
4、启动,测试
浏览器访问http://127.0.0.1:9990以查看服务提供者
创建网关
1、IDEA:new-->project-->Spring Initializr-->输入项目名-->Spring Cloud Routing-->Zuul
2、在启动类添加注解@EnableZuulProxy
3、配置application.application
server.port=6660 # spring.application.name=zuul 在bootstrap.properties文件里已经配置了 # 注册中心配置 eureka.client.service-url.defaultZone=http://root:root@127.0.0.1:9990/eureka/ # 网关配置,拦截访问路径以/user/开头的 zuul.routes.user-consumer=/user/** # 负载均衡默认就是开启的 # ribbon.eureka.enable=true # 请求处理的超时时间 ribbon.ReadTimeout=120000 # 请求连接的超时时间 ribbon.ConnectTimeout=30000
4、启动,测试
a. 浏览器访问http://127.0.0.1:9990以查看服务提供者
b. 浏览器访问http://127.0.0.1:6660/user/hello
创建 config server
1、在码云创建一个新项目,两个分支master和feature1,两个分支下建立一个文件夹,文件夹下建立一个demo.properties:
2、IDEA:new-->project-->Spring Initializr-->输入项目名-->Spring Cloud Config-->Config Server
3、在启动类添加注解@EnableConfigServer
4、配置application.application
server.port=5550 spring.application.name=config-server # config server也可以不用注册到注册中心 eureka.client.service-url.defaultZone=http://127.0.0.1:9990/eureka/ spring.cloud.config.server.git.uri=https://gitee.com/zhangrunwei/spring-cloud-config-demo.git spring.cloud.config.server.git.username=码云手机号 spring.cloud.config.server.git.password=码云密码 spring.cloud.config.server.git.search-paths=码云上配置文件所在的文件夹名
5、启动测试
一、浏览器测试
1. config server项目启动后,浏览器访问方式:http://xxx:9090/文件名/版本(dev、pro)/分支
2. 项目名、文件夹在配置文件里已经配置了,这里只需提供文件名、版本、分支名
3. http://localhost:5550/demo/dev 没指定分支,那么默认就是master分支
4. http://localhost:5550/demo/dev/feature1 访问feature1分支
二、config client读取远程配置,此次使用网关模块做为config client
1. 在config client的bootstrap.properties里配置
bootstrap.properties比application.properties文件级别更高; bootstrap.properties文件可以给application.properties文件做准备配置
spring.application.name=zuul # config server address + file name + profile(dev、pro) + feature # config server 地址 + 文件名 + 版本 + 分支 spring.cloud.config.uri=http://localhost:5550 spring.cloud.config.name=demo spring.cloud.config.profile=dev spring.cloud.config.label=master # spring.cloud.config.label=feature1