Sping Cloud
服务发现组件 Eureka
Eureka Server 服务中心
Eureka Client 启动 就会注册到Server
- 用到的库
spring-cloud-starter-netflix-eureka-server
spring-cloud-starter-netflix-eureka-client- 注解
@EnableEurekaServer
@EnableEurekaClient- application.yml 配置信息
//服务端 server: port: 6868 eureka: client: service-url: defaultZone: http://127.0.0.1:${server.port}/eureka/ register-with-eureka: false fetch-registry: false spring: application: name: study-eureka //客户端 server: port: 8087 eureka: client: service-url: defaultZone: http://127.0.0.1:6868/eureka/ instance: prefer-ip-address: true spring: application: name: study-admin
- 打开网页可以看到http://localhost:6868/
Feign实现服务间的调用(默认带有负载均衡)
Feign是简化Java HTTP客户端开发的工具(java-to-httpclient-binder),它的灵感
来自于Retrofit、JAXRS-2.0和WebSocket。Feign的初衷是降低统一绑定Denominator到
HTTP API的复杂度,不区分是否为restful
- 库
spring-cloud-starter-openfeign- 注解
@EnableFeignClients
@EnableDiscoveryClient
创建FeignClient接口@FeignClient(value = "study-api", fallback = ApiClientImpl.class) @Component public interface ApiClient { //, @RequestHeader(value = "token", required = false) String token @PostMapping(value = "/api/test") R<String> test(@RequestParam(value = "data") String data); }
熔断器Hystrix
- 配置
feign: hystrix: enabled: true
- 代码
@Component public class ApiClientImpl implements ApiClient { @Override public R<String> test(String data) { return RUtil.success("熔断器起作用了" + data); } }
微服务网关Zuul
微服务网关是介于客户端和服务器端之间的中间层,所有的外部请求都会先经过微服务网关,然后通过spring
-application-name 转发对应的服务
- 库
spring-cloud-starter-netflix-zuul
- 注解
@EnableZuulProxy- 路由转发
zuul: routes: study-api: path: /api/** serviceId: study-api study-admin: path: /admin/** serviceId: study-admin
- 过滤器
package com.ityu.studyadminzuul.filter; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @Component public class WebFilter extends ZuulFilter { @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { System.out.println("拦截器启动了"); RequestContext currentContext = RequestContext.getCurrentContext(); HttpServletRequest request = currentContext.getRequest(); if (request.getMethod().equals("OPTIONS")) { return null; } String url = request.getRequestURL().toString(); if (url.indexOf("/admin/login") > 0) { return null; } String header = request.getHeader("token"); if (StringUtils.isNotEmpty(header)) { currentContext.addZuulRequestHeader("token", header); } else { try { //URLDecoder.decode("xxxxx","UTF-8"); 解码 微服务获取时解码 //在网关添加内容是编码 currentContext.addZuulRequestHeader("token", URLEncoder.encode("没有头的信息", "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return null; } }
集中配置组件SpringCloudConfig
config server
config client
- 库
spring-cloud-config-server
spring-cloud-starter-config
- 注解
@EnableConfigServer
3.配置信息cloud: config: server: git: uri: https://gitee.com/lanlingkeji_yu.jl/srpingcloudstudyconfig.git
4.客户端配置信息 bootstrap.yml
spring: cloud: config: name: api profile: dev lable: master uri: http://127.0.0.1:12000
消息总线组件SpringCloudBus
SpringCloudBus来实现配置的自动更新
- 库
spring-cloud-stream-binder-rabbit
spring-cloud-bus
spring-boot-starter-actuator- 注解
@RefreshScope 刷新自定义配置信息- 配置
management: endpoints: web: exposure: include: bus-refresh
- 客戶端也要配置 rabbitmq 信息