SpringCloud入门实战六

一:Springcloud之路由网关zuul

  1.  什么是zuul?
      SpringCloud入门实战六_第1张图片         

二:SpringCloud路由网关的基本配置

  1. 新建子模块microservicecloud-zuul-gateway-9527
      SpringCloud入门实战六_第2张图片          
  2. 在pom.xml中添加依赖
     
    
      4.0.0
      
        com.topcheer
        microservicecloud
        0.0.1-SNAPSHOT
      
      microservicecloud-zuul-gateway-9527
      
      
      	 
      	 
          org.springframework.cloud
          spring-cloud-starter-zuul
         
        
          org.springframework.cloud
          spring-cloud-starter-eureka
         
        
        
          org.springframework.boot
          spring-boot-starter-actuator
         
        
         
          org.springframework.cloud
          spring-cloud-starter-hystrix
         
        
          org.springframework.cloud
          spring-cloud-starter-config
         
         
         
           com.topcheer
           microservicecloud-api
           ${project.version}
    	
    	
    			org.springframework.boot
    			spring-boot-starter-jetty
    	
    	
    	       org.springframework.boot
    	       spring-boot-starter-web
    	
    	
    	       org.springframework.boot
    	       spring-boot-starter-test
    	
    	
    	
    	       org.springframework
    	       springloaded
    	
    	
    	       org.springframework.boot
    	       spring-boot-devtools
    	
      
    

     

  3. 创建application.yml配置文件(注意:hosts文件中gateway-9527.com需要添加上)

    server:
      port: 9527 #设置端口为9527
      
    spring:
      application:
        name: microservicecloud-zuul-gateway
    
    eureka: #这个就是将8001服务端注册进入注册中心,下面的就是注册中心的注册地址
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance: 
         instance-id: gateway-9527.com
         prefer-ip-address: true #访问路径可以显示IP地址     
     
    info:
      app.name: topcheer-microservicecloud
      company.name: com.topcheer
      build.artifactId: $project.artifactId$
      build.version: $project.version$   
       

    SpringCloud入门实战六_第3张图片

  4. 创建启动类Zuul_9527_StartSpringCloudApp
     SpringCloud入门实战六_第4张图片SpringCloud入门实战六_第5张图片

  5. 测试
     a:首先启动3个Eureka集群,再启动一个服务提供类microservicecloud-provider-dept-8001,最后启动刚健的路由微服务
     b:效果
         SpringCloud入门实战六_第6张图片
     c:不使用路由访问8001
         SpringCloud入门实战六_第7张图片
     d:启用路由访问
           地址:http://myzuul.com:9527/microservicecloud-dept/dept/get/1
           http://域名反射:路由网关微服务端口/指定提供者微服务名称/提供者微服务接口路径
           SpringCloud入门实战六_第8张图片

三:zuul路由访问映射规则

  1. 我们现在存在一个问题,我们现在通过网关访问提供者的微服务的路径是:
    http://myzuul.com:9527/microservicecloud-dept/dept/get/1,但是这个路径暴露了我们提供者微服务
    的名称,我们想要将这个路径包装一下。该如何操作了。
  2. 操作的步骤如下
     a:设置代理名称,修改路由网关微服务的application.yml
           SpringCloud入门实战六_第9张图片
     b:效果
          SpringCloud入门实战六_第10张图片
         但是我们发现原来的路径也能访问:
         SpringCloud入门实战六_第11张图片
     c:我们现在需要将真实的路径忽略掉,只留代理的名称可以访问
          SpringCloud入门实战六_第12张图片
     d:效果
          SpringCloud入门实战六_第13张图片
         SpringCloud入门实战六_第14张图片
     e:如果我们在实际项目中药忽略多个微服务名称可以使用下面的方式
          SpringCloud入门实战六_第15张图片
     f:我们还可以为代理名称添加前缀
          SpringCloud入门实战六_第16张图片    
     效果:
        SpringCloud入门实战六_第17张图片
           
                   

四:SpringCloud config之分布式配置中心

  1. 分布式系统面临的问题
         
  2. 简介config
      SpringCloud入门实战六_第18张图片   
        
     SpringCloud入门实战六_第19张图片
  3. config可以做的事情
     SpringCloud入门实战六_第20张图片
     

五:SpringCloud config服务端配置

  1. 首先我们需要使用自己的GitHub账户在GitHub上新建一个名为microservicecloud-config的新respository
     SpringCloud入门实战六_第21张图片          
  2. 由上一步获取SSH协议的git地址
     SpringCloud入门实战六_第22张图片
  3. 本地硬盘目录上新建git仓库并clone
     我们的本地地址为:D:\Git-Springcloud
     SpringCloud入门实战六_第23张图片SpringCloud入门实战六_第24张图片
     我们使用命令将远程仓库克隆一份到本地:git clone https://github.com/kangf897570/microservicecloud-config.git
     SpringCloud入门实战六_第25张图片
  4. 在本地仓库新建application.yml配置文件(注意:一定要以UTF-8的格式保存)
     SpringCloud入门实战六_第26张图片
     内容:
      SpringCloud入门实战六_第27张图片
  5. 将新建的这个application.yml文件推送到github上面
     SpringCloud入门实战六_第28张图片
     SpringCloud入门实战六_第29张图片
     SpringCloud入门实战六_第30张图片
  6. 下面我们需要新建微服务,促使它可以连接GitHub并且获取配置文件信息
     ⑴创建子模块microservicecloud-config-3344
          SpringCloud入门实战六_第31张图片
      ⑵在pom.xml中添加依赖
           
    
      4.0.0
      
        com.topcheer
        microservicecloud
        0.0.1-SNAPSHOT
      
      microservicecloud-config-3344
      
      
         
         
            org.springframework.cloud
            spring-cloud-config-server
         
         
          
            org.eclipse.jgit
            org.eclipse.jgit
            4.10.0.201712302008-r
         
         
         
            org.springframework.boot
            spring-boot-starter-actuator
         
         
          org.springframework.cloud
          spring-cloud-starter-eureka
         
        
         
          org.springframework.cloud
          spring-cloud-starter-hystrix
         
        
          org.springframework.cloud
          spring-cloud-starter-config
         
        
    			org.springframework.boot
    			spring-boot-starter-jetty
    	
    	
    	       org.springframework.boot
    	       spring-boot-starter-web
    	
    	
    	       org.springframework.boot
    	       spring-boot-starter-test
    	
    	
    	
    	       org.springframework
    	       springloaded
    	
    	
    	       org.springframework.boot
    	       spring-boot-devtools
    	
      
    

    ⑶创建application.yml文件
         SpringCloud入门实战六_第32张图片

    ⑷创建启动类
         SpringCloud入门实战六_第33张图片SpringCloud入门实战六_第34张图片
    ⑸修改window下的hosts文件
       SpringCloud入门实战六_第35张图片
    ⑹测试微服务是否可以获取GitHub下的配置文件
         a:启动3344微服务
         b:读取开发环境信息
               SpringCloud入门实战六_第36张图片
         c:读取测试环境信息
               SpringCloud入门实战六_第37张图片
        d:其他访问的路径方式
             SpringCloud入门实战六_第38张图片
             SpringCloud入门实战六_第39张图片

六:SpringCloud config客户端配置

  1. 在本地仓库新建microservicecloud-config-client.yml配置文件     
     SpringCloud入门实战六_第40张图片     
     内容:
      SpringCloud入门实战六_第41张图片
  2. 将文件上传到GitHub上
     SpringCloud入门实战六_第42张图片
  3. 新建子模块microservicecloud-config-client-3355客户端
     SpringCloud入门实战六_第43张图片
  4. 在新建的子模块的pom.xml中添加依赖
     
    
      4.0.0
      
        com.topcheer
        microservicecloud
        0.0.1-SNAPSHOT
      
      microservicecloud-config-client-3355
     
         
         
            org.springframework.cloud
            spring-cloud-starter-config
         
         
         
            org.springframework.boot
            spring-boot-starter-actuator
         
         
          org.springframework.cloud
          spring-cloud-starter-eureka
         
        
         
          org.springframework.cloud
          spring-cloud-starter-hystrix
         
        
    			org.springframework.boot
    			spring-boot-starter-jetty
    	
    	
    	       org.springframework.boot
    	       spring-boot-starter-web
    	
    	
    	       org.springframework.boot
    	       spring-boot-starter-test
    	
    	
    	
    	       org.springframework
    	       springloaded
    	
    	
    	       org.springframework.boot
    	       spring-boot-devtools
    	
      
    

     

  5. 创建bootstrap.yml配置文件
     SpringCloud入门实战六_第44张图片
     SpringCloud入门实战六_第45张图片
     内容:
      SpringCloud入门实战六_第46张图片

  6. 创建application.yml文件,注意:名称需要和bootstrap.yml中的一致(microservicecloud-config-client)
      SpringCloud入门实战六_第47张图片SpringCloud入门实战六_第48张图片

  7. 修改hosts文件
      SpringCloud入门实战六_第49张图片

  8. 新建ConfigClientRest.java类,验证能否从GitHub上读取配置信息
     SpringCloud入门实战六_第50张图片
     SpringCloud入门实战六_第51张图片

  9. 创建我们的主启动类
     SpringCloud入门实战六_第52张图片
     SpringCloud入门实战六_第53张图片

  10. 测试,启动3344,和3355进行测试
     
     访问地址的组成:
     http://IP:从Github上读取指定环境的端口/controller方法路径
     SpringCloud入门实战六_第54张图片
     GitHub上指定环境的内容:
     SpringCloud入门实战六_第55张图片

七:SpringCloud Config之配置实战

  1.  需求
          
  2. 在git本地仓库创建microservicecloud-config-eureka-client.yml配置文件(这个是eureka注册中心的)
     SpringCloud入门实战六_第56张图片   
     内容:
     SpringCloud入门实战六_第57张图片
  3. 在git本地仓库创建microservicecloud-config-dept-client.yml配置文件(这个是微服务提供者的)
     SpringCloud入门实战六_第58张图片
     内容:(不同的环境,链接的数据库也不一样)
      
    spring: 
      profiles: 
        active: 
        - dev
    ---    
    server:
      port: 8001 #设置端口为8001
    spring:
      profiles: dev #开发环境
      application:
        name: microservicecloud-config-dept-client 
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource #设置当前数据源
        driver-class-name: org.gjt.mm.mysql.Driver #设置mysql驱动包
        url: jdbc:mysql://localhost:3306/cloudDB01 #设置数据库名称
        username: root
        password: 897570
        dbcp2:
          min-idle: 5 #设置数据库连接池的最小维持连接数
          initial-size: 5 #设置初始化链接数
          max-total: 5 #设置最大连接数
          max-wait-millis: 200 #设置链接获取的最大超时时间    
    mybatis:
      config-location: classpath:mybatis/mybatis.cfg.xml  #设置mybatis配置文件所在的路径
      type-aliases-package: com.topcheer.springcloud.entities #扫描所有实体类所在包
      mapper-locations:
      - classpath:mybatis/mapper/**/*.xml #扫描mybatis的映射文件  
    
    eureka: #这个就是将8001服务端注册进入注册中心,下面的就是注册中心的注册地址
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/
      instance: 
        instance-id: dept-8001.com
        prefer-ip-address: true #访问路径可以显示IP地址     
     
    info:
      app.name: topcheer-microservicecloud
      company.name: com.topcheer
      build.artifactId: $project.artifactId$
      build.version: $project.version$ 
    ---
    server:
      port: 8001 #设置端口为8001
    spring:
      profiles: test #开发环境
      application:
        name: microservicecloud-config-dept-client 
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource #设置当前数据源
        driver-class-name: org.gjt.mm.mysql.Driver #设置mysql驱动包
        url: jdbc:mysql://localhost:3306/cloudDB02 #设置数据库名称
        username: root
        password: 897570
        dbcp2:
          min-idle: 5 #设置数据库连接池的最小维持连接数
          initial-size: 5 #设置初始化链接数
          max-total: 5 #设置最大连接数
          max-wait-millis: 200 #设置链接获取的最大超时时间    
    mybatis:
      config-location: classpath:mybatis/mybatis.cfg.xml  #设置mybatis配置文件所在的路径
      type-aliases-package: com.topcheer.springcloud.entities #扫描所有实体类所在包
      mapper-locations:
      - classpath:mybatis/mapper/**/*.xml #扫描mybatis的映射文件  
    
    eureka: #这个就是将8001服务端注册进入注册中心,下面的就是注册中心的注册地址
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/
      instance: 
        instance-id: dept-8001.com
        prefer-ip-address: true #访问路径可以显示IP地址     
     
    info:
      app.name: topcheer-microservicecloud
      company.name: com.topcheer
      build.artifactId: $project.artifactId$
      build.version: $project.version$   
       
  4. 下面我们将这刚刚创建的两个文件上传到GitHub上
     SpringCloud入门实战六_第59张图片
  5. 创建Config版的eureka服务端microservicecloud-config-eureka-client-7001
    SpringCloud入门实战六_第60张图片
  6. 在pom.xml添加依赖
     
    
      4.0.0
      
        com.topcheer
        microservicecloud
        0.0.1-SNAPSHOT
      
      microservicecloud-config-eureka-client-7001
     
     	
     	
          org.springframework.cloud
          spring-cloud-starter-config
         
        
        
          org.springframework.cloud
          spring-cloud-starter-eureka-server
         
        
    	
    	       org.springframework
    	       springloaded
    	
    	
    	       org.springframework.boot
    	       spring-boot-devtools
    	
      
    
  7. 创建主启动类Config_Git_EurekaServerApplication.java
     SpringCloud入门实战六_第61张图片
     SpringCloud入门实战六_第62张图片
  8. 创建bootstrap.yml文件
     SpringCloud入门实战六_第63张图片
     内容:
     SpringCloud入门实战六_第64张图片
  9. 创建application.yml文件
     SpringCloud入门实战六_第65张图片
     内容:
      SpringCloud入门实战六_第66张图片
  10. 下面我们把3344,以及刚建立的config版的7001注册中心启动起来,测试一下
     SpringCloud入门实战六_第67张图片
       
  11. 创建Config版的dept微服务microservicecloud-config-dept-client-8001
     SpringCloud入门实战六_第68张图片
     
  12. 在pom.xml中添加依赖
     
    
      4.0.0
      
        com.topcheer
        microservicecloud
        0.0.1-SNAPSHOT
      
      microservicecloud-config-dept-client-8001
    
        
          org.springframework.cloud
          spring-cloud-starter-config
         
        
        
    	       org.springframework.boot
    	       spring-boot-starter-actuator
    	
    	
    	
          org.springframework.cloud
          spring-cloud-starter-eureka
         
    	   
         
           com.topcheer
           microservicecloud-api
           ${project.version}
    	
    	
    	       junit
    	       junit
    	
    	
    	       mysql
    	       mysql-connector-java
    	
    	
    	       com.alibaba
    	       druid
    	
    	
    	       ch.qos.logback
    	       logback-core
    	
    	
    	       org.mybatis.spring.boot
    	       mybatis-spring-boot-starter
    	
    	
    			org.springframework.boot
    			spring-boot-starter-jetty
    	
    	
    	       org.springframework.boot
    	       spring-boot-starter-web
    	
    	
    	       org.springframework.boot
    	       spring-boot-starter-test
    	
    	
    	
    	       org.springframework
    	       springloaded
    	
    	
    	       org.springframework.boot
    	       spring-boot-devtools
    	
      
    

     

  13. 创建bootstrap.yml文件
     SpringCloud入门实战六_第69张图片
     SpringCloud入门实战六_第70张图片

  14. 创建application.yml文件
      SpringCloud入门实战六_第71张图片SpringCloud入门实战六_第72张图片

  15. 将之前的8001业务代码拷贝过来,还有mybatis配置文件
     SpringCloud入门实战六_第73张图片

  16. 好了,下面我们可以进行测试了,首先我们需要启动3344,7001,以及最后的8001

    SpringCloud入门实战六_第74张图片

 

文章目录

你可能感兴趣的:(SpringCloud,微服务,springboot,Eureka,config)