SpringCloud
什么是SpringCloud?
SpringCloud是一系列框架的有序集合,简单来说就是一套分布式解决方案
SpringCloud主要组件
一·服务发现组件——Eureka
Ⅰ.引入依赖父工程pom.xml定义SpringCloud版本
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M9</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Ⅱ.eureka服务端模块pom.xml引入依赖
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
Ⅲ.eureka服务端模块编写配置文件
server:
  port: 6868 #服务端口
eureka:
  client:
    registerWithEureka: false #是否将自己注册到Eureka服务中,本身就是所有无需注册
    fetchRegistry: false #是否从Eureka中获取注册信息
    serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址
      defaultZone: http://127.0.0.1:${server.port}/eureka/
  server:
    enable-self-preservation: false # 关闭Eureka保护机制
Ⅳ.编写启动类
在启动类中加入 @EnableEurekaServer 注解即可
Ⅴ.eureka客户端模块
1.添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2.编写配置文件
server: 
  port: 9003
spring: 
  application:  
    name: tensquare-qa #指定服务名
eureka:
  client:
    service‐url:
      defaultZone: http://localhost:6868/eureka  # Eureka服务端地址
    fetch-registry: true  # 是否拉取服务列表
    register-with-eureka: true  # 是否将当前微服务注册到Eureka服务端
  instance:
    prefer‐ip‐address: true # 注册服务时,注册ip地址
3.给启动类添加 @EnableEurekaClient 注解即可
二·服务调用组件——Feign
Ⅰ.在要调用其他模块的模块添加如下依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
Ⅱ.添加 @EnableFeignClients 和 @EnableDiscoveryClient 注解到引导类
Ⅲ.在该模块中创建接口
        例子:远程调用tensquare-base模块中的 findById 方法
@FeignClient("tensquare-base")
public interface LabelClient {
    @RequestMapping(value = "/label/{labelId}",method = RequestMethod.GET)
    public Result findById(@PathVariable("labelId") String labelId);
}
三·熔断器——Hystrix
为什么使用熔断器?
以免一个微服务的故障可能会导致许多微服务出问题,熔断器就是为解决此问题而生
Hystrix熔断器原理:
     
四·服务网关Zuul
什么是服务网关zuul?
介于客户端和服务器端之间的中间层,所有的外部请求都会先经过的微服务网关,且使前端调用更方便,但简单来说就是过滤器。
       
Ⅰ.网关客户端依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
Ⅱ.配置文件
server:
  port: 9011
spring:
  application:
    name: tensquare-web #指定服务名
eureka:
  client:
    serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址
      defaultZone: http://127.0.0.1:6868/eureka/
  instance:
    prefer-ip-address: true
zuul:
  routes:
    tensquare-base: # 项目名称,自由填写
      path: /base/** #配置请求URL的请求规则
      serviceId: tensquare-base #指定Eureka注册中心中的服务id
    tensquare-qa: # 项目名称,自由填写
      path: /qa/** #配置请求URL的请求规则
      serviceId: tensquare-qa #指定Eureka注册中心中的服务id
    tensquare-friend: # 项目名称,自由填写
      path: /friend/** #配置请求URL的请求规则
      serviceId: tensquare-friend #指定Eureka注册中心中的服务id
    tensquare-user: # 项目名称,自由填写
      path: /user/** #配置请求URL的请求规则
      serviceId: tensquare-user #指定Eureka注册中心中的服务id
Ⅲ.引导类添加 @EnableZuulProxy 注解
Ⅳ.继承ZuulFilter类
@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("过滤器执行了"); return null; } }
五.集中配置组件SpringCloudConfig
为什么要集中管理配置文件?
太多的配置文件不方便后期维护,所以要集中管理配置文件。
Ⅰ.将配置文件提交到到git上
文件命名规则:
{application}-{profile}.yml 或 {application}-{profile}.properties
application为应用名称
profile指的开发环境(用于区分开发环境,测试环境、生产环境等)
例如:base-dev.yml
Ⅱ.配置中心微服务引入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
Ⅲ.配置文件
server:
  port: 12000
spring:
  application:
    name: tensquare-config
  cloud:
    config:
      server:
        git: # 指定从git仓库中加载配置
          uri: https://gitee.com/${路径}l/tensquare-config.git # 指定仓库所在路径
Ⅳ.启动类加入 @EnableConfigServer 注解即可
Ⅴ.配置客户端
1.导入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
2.编写配置文件bootstrap.yml
spring:
  cloud:
    config:
      name: base  # 配置文件名称前缀
      profile: dev # 配置文件名称后缀
      label: master # 加载git仓库哪个分支中的配置
      uri: http://127.0.0.1:12000 # 指定从哪个微服务加载配置
六.消息总线SpringCloudBus
消息总线是什么?
就是当更新git中的配置文件时,如果配置了消息总线就可以不重启微服务即自动更新
                    
                
                
            
        
浙公网安备 33010602011771号