SpringCould 组件简单应用
参考文档

注册中心

配置文件application.properties写
server.port=8031
eureka.client.service-url.defaultZone=http://127.0.0.1:8031/eureka
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
|
启动类加注解
Ok
提供者服务
配置文件application.properties写
server.port=8021
spring.application.name=xiaofeizhe
eureka.client.service-url.defaultZone=http://127.0.0.1:8030/eureka
#不用额外的引入依赖,feign中包含hystrix,但默认是关闭的,想用就打开
feign.hystrix.enabled=true
#ribbon也是被集成了的,选服务,选均衡策略,指定策略,也可以自定义策略
#除了随机分配,还有些其他的自带均衡策略,一般来说足够用了
#BestAvailableRule,选择一个最小的并发请求的server
#AvailabilityFilteringRule,过滤掉那些因为一直连接失败的被标记为circuit tripped的后端server,并过滤掉那些高并发的的后端server
#WeightedResponseTimeRule,根据相应时间分配一个weight,相应时间越长,weight越小,被选中的可能性越低
#RoundRobinRule,roundRobin方式轮询选择server
#ZoneAvoidanceRule,复合判断server所在区域的性能和server的可用性选择server
demo-pdr.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule
|
启动类加注解
@EnableDiscoveryClient
@EnableFeignClients
|
Service加注解(开容断器)
@FeignClient(value = "liangZai",fallback=BookServiceImpl .class(该service类的实现类))
@RequestMapping()
@RequestParam()
@RequestBody
|
开熔断器需要有Service 实现类要加@Component
@Component
public class BookServiceImpl implements BookService
|
Ok
提供者服务
配置文件application.properties写
server.port=8011
spring.application.name=liangZai
eureka.client.service-url.defaultZone=http://127.0.0.1:8030/eureka
|
启动类加注解
实现消费者传过来的服务加注解@RestController
@RestController
public class BookServiceImpl {
@RequestMapping()
@RequestParam()
@RequestBody
}
|
Ok
网关
配置文件application.properties写
#server.port=8041
#spring.application.name=zuul-1
#eureka.client.service-url.defaultZone=http://127.0.0.1:8030/eureka
#zuul.routes.消费者服务名=前缀
zuul.routes.xiaofeizhe=/book/**
zuul.routes.user-consumer=/user/**
#解决重定向不加前缀问题
#zuul.add-host-header=true
#解决504网关超时问题
#ribbon.ReadTimeout=120000
#ribbon.ConnectTimeout=30000
|
启动类加注解
@EnableEurekaClient
@EnableZuulProxy
|
自定义拦截器继承ZuulFilter 并加注解@Component
@Component
public class toFilter extends ZuulFilter {
@Override
//return String pre 在前,post在后 route 执行中 error 出错
public String filterType() { return FilterConstants.PRE_TYPE;}
@Override
//过滤器的优先级,越小优先级越高(有负数)
public int filterOrder() {return 0;}
@Override
// 是否执行该过滤器 true 执行,false 不执行
public boolean shouldFilter() {
RequestContext context=RequestContext.getCurrentContext();
return context.sendZuulResponse();}
@Override
//过滤的内容(规则)
public Object run() throws ZuulException {
//获取上下文
RequestContext context=RequestContext.getCurrentContext();
//是否转发给消费者服务 默认为true
context.setSendZuulResponse(false);
context.setResponseStatusCode(401);
context.setResponseBody("{\"msg\":\"401,access without token,please login first\"}");
return "pass";
}
}
|
Config 配置服务
配置文件application.properties写
server.port=8044
spring.application.name=config-Server
eureka.client.service-url.defaultZone=http://127.0.0.1:8030/eureka
#远程
#uri指的是仓库的位置,不是具体哪个文件的详细位置
spring.cloud.config.server.git.uri=https://gitee.com/zeng_wei_li/config/#仓库路径
spring.cloud.config.server.git.default-label=dev #分支
spring.cloud.config.server.git.search-paths=/config #文件夹名
spring.cloud.config.server.git.username=15070433962
spring.cloud.config.server.git.password=11111111
#本地
#spring.cloud.config.server.native.search-locations=classpath/根目录不要设置
#spring.profiles.active=native
#pre生产 test测试 dev开发 base 基础
|
启动类加注解
@EnableEurekaClient
@EnableConfigServer
|
Ok
需要config的配置文件的服务
配置文件加一个名为bootstrap的配置文件
bootstrap会在application之前执行

配置文件bootstrap.properties写
server.port=8041
eureka.client.service-url.defaultZone=http://127.0.0.1:8030/eureka
spring.application.name=zuul-1
#特有的
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-Server #配置服务名
spring.cloud.config.name=zuul-config #需要的文件名
spring.cloud.config.profile=base #文件的描述 pre生产 test测试 dev开发 base 基础
|
在需要加载配置内容上加注解@Value("${名")
例:@Value("${aqaq}")
private String qqqq;