cloud-zuul路由网关

北战南征 提交于 2020-12-05 07:00:05

九、zuul路由网关

概述

1.1

能干嘛

    路由、过滤

路由基本配置

POM

<dependencies>
		<!-- zuul路由网关 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zuul</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<!-- actuator监控 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- hystrix容错 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<!-- 日常标配 -->
		<dependency>
			<groupId>com.wby.springcloud</groupId>
			<artifactId>microservicecloud-api</artifactId>
			<version>${project.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<!-- 热部署插件 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>
</project>

YML

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway
 
eureka: 
  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 
 
 
zuul: 
  #ignored-services: microservicecloud-dept
  prefix: /wby
  ignored-services: "*"
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**
 
info:
  app.name: wby-microcloud
  company.name: www.wby.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

HOST修改

    C:\Windows\System32\drivers\etc

127.0.0.1	eureka7001.com
127.0.0.1	eureka7002.com
127.0.0.1	eureka7003.com

127.0.0.1	user.sc.com card.sc.com manage.sc.com www.gmall.com myzuul.com

主启动类

@SpringBootApplication
@EnableZuulProxy
public class Zuul_9527_StartSpringCloudApp
{
	public static void main(String[] args)
	{
		SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
	}
}

启动

测试

不用路由

 

启用路由

 

路由访问映射规则

代理名称

 

使用代理名称的地址,可正确获取到数据

此时的问题:使用原名称依然可以获取数据,违背单入口单出口的原理,不合理。

原真实服务名忽略

忽略单个写具体名称,多可可以用"*"

设置统一公共前缀:prefix

zuul: 
  #ignored-services: microservicecloud-dept
  prefix: /wby
  ignored-services: "*"  #忽略原真实单个微服务名字写微服务名字即可,忽略多个微服务名字写“*”
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**

最后YML

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway
 
eureka: 
  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 
 
 
zuul: 
  #ignored-services: microservicecloud-dept
  prefix: /wby
  ignored-services: "*"  #忽略原真实单个微服务名字写微服务名字即可,忽略多个微服务名字写“*”
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**
 
info:
  app.name: wby-microcloud
  company.name: www.wby.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

十、springCloud config分布式配置中心

概述

分布式系统面临的问题

是什么

能干嘛

与github整合配置

SpringCloud Config服务端配置

在github上新建名为microservicecloud-config的新的Repository

由上一步获得SSH协议的git地址

本地硬盘目录上新建git仓库并clone

在本地microservicecloud-config项目里面新建一个application.yml,切记!!!以UTF-8保存!!

spring:
   profiles:
      active:
      - dev
---
spring:
   profiles: dev   #开发环境
   application: 
      name: microservicecloud-config-wby-dev
---
spring:
   profiles: test   #测试环境
   application: 
      name: microservicecloud-config-wby-test
#切记保存为UTF-8格式

将上一步的YML文件推送到github上


Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master)
$ pwd       ###显示当前文件夹
/e/gitRepository/microservicecloud-config

Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master)
$ git status   ###git仓库状态
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        application.yml    ###红色,表示发生了彼岸花

nothing added to commit but untracked files present (use "git add" to track)

Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master)
$ git add .       ###提交修改和提交新文件是一样的两步,第一步是git add

Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master)
$ git commit -m "init file"   ###提交修改和提交新文件是一样的两步,第二步是git commit,""中的是提交的注释
[master (root-commit) 681a2a1] init file
 1 file changed, 15 insertions(+)
 create mode 100644 application.yml

Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master)
$ git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 377 bytes | 377.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/justtimo/microservicecloud-config.git
 * [new branch]      master -> master

Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master)
$

新建Module模块microservicecloud-config-3344,他就是Cloud的配置中心模块

POM

	<dependencies>
		<!-- springCloud Config -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback -->
		<dependency>
			<groupId>org.eclipse.jgit</groupId>
			<artifactId>org.eclipse.jgit</artifactId>
			<version>4.10.0.201712302008-r</version>
		</dependency>
		<!-- 图形化监控 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- 熔断 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<!-- 热部署插件 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>

YML

server: 
  port: 3344 
  
spring:
  application:
    name:  microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri: git@github.com:justtimo/microservicecloud-config.git #GitHub上面的git仓库名字

主启动类config_3344_startSpringCloudApp

package com.wby.springcoud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class Config_3344_StartSpringCloudApp
{
	public static void main(String[] args)
	{
		SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);
	}
}

windows下修改hosts文件,增加映射

127.0.0.1	eureka7001.com
127.0.0.1	eureka7002.com
127.0.0.1	eureka7003.com

127.0.0.1	user.sc.com card.sc.com manage.sc.com www.gmall.com myzuul.com config-3344.com

测试通过config微服务是否可以从github上获取配置内容

配置读取规则

官网

第二种已经试过了

第一种:

第三种:

成功实现了用springcloud config通过github获取配置信息

SpringCloud Config客户端配置与测试

在本地E:\gitRepository\microservicecloud-config下新建文件microservicecloud-config-client.yml

microservicecloud-config-client.yml内容

将上一步提交到github上


Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        microservicecloud-config-client.yml

nothing added to commit but untracked files present (use "git add" to track)

Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master)
$ git add
Nothing specified, nothing added.
Maybe you wanted to say 'git add .'?

Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master)
$ git add .

Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master)
$ git commit -m "test config"
[master eaaf5b6] test config
 1 file changed, 27 insertions(+)
 create mode 100644 microservicecloud-config-client.yml

Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master)
$ git push origin master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 533 bytes | 533.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/justtimo/microservicecloud-config.git
   681a2a1..eaaf5b6  master -> master

Administrator@WBY MINGW64 /e/gitRepository/microservicecloud-config (master)
$

新建microservicecloud-config-client-3355

POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.wby.springcloud</groupId>
    <artifactId>microservicecloud-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>microservicecloud-config-client-3355</artifactId>
<dependencies>
		<!-- SpringCloud Config客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>
</project>
 

bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名
      profile: test   #本次访问的配置项
      label: master   
      uri: http://config-3344.com:3344  #本微服务启动后先去找3344号服务,通过3344号服务的SpringCloudConfig获取GitHub的服务地址

application.yml

spring:
  application:
    name: microservicecloud-config-client

windows下修改hosts文件,增加映射

127.0.0.1	eureka7001.com
127.0.0.1	eureka7002.com
127.0.0.1	eureka7003.com

127.0.0.1	user.sc.com card.sc.com manage.sc.com www.gmall.com myzuul.com config-3344.com client-config.com

新建rest类,验证是否能从github上读取配置

package com.wby.springcloud.rest;

@RestController
public class ConfigClientRest
{

	@Value("${spring.application.name}")
	private String applicationName;

	@Value("${eureka.client.service-url.defaultZone}")
	private String eurekaServers;

	@Value("${server.port}")
	private String port;

	@RequestMapping("/config")
	public String getConfig()
	{
		String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
		System.out.println("******str: " + str);
		return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
	}
}

主启动类ConfigClient_3355_StartSpringCloudApp


package com.wby.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClient_3355_StartSpringCloudApp
{
	public static void main(String[] args)
	{
		SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class, args);
	}
}

测试

启动3344并自测:http://config-3344.com:3344/application-dev.yml

profile的值:

成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息

SpringCloud Config配置实战

目前情况

Git配置文件本地配置

microservicecloud-config-dept-client.yml

spring: 
  profiles:
    active:
    - dev
--- 
server:
  port: 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
    url: jdbc:mysql://localhost:3306/cloudDB03
    username: root
    password: wby6225104
    dbcp2:
      min-idle: 5
      initial-size: 5
      max-total: 5
      max-wait-millis: 200 
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml
  type-aliases-package: com.my.springcloud.entites
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml
 
eureka: 
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: dept-8001.com
    prefer-ip-address: true
 
info:
  app.name: my-microservicecloud-springcloudconfig03-java1129
  company.name: www.my.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$
---
server:
  port: 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
    url: jdbc:mysql://localhost:3306/cloudDB02
    username: root
    password: wby6225104
    dbcp2:
      min-idle: 5
      initial-size: 5
      max-total: 5
      max-wait-millis: 200  
  
  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml
  type-aliases-package: com.my.springcloud.entites
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml
 
eureka: 
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: dept-8001.com
    prefer-ip-address: true
 
info:
  app.name: my-microservicecloud-springcloudconfig02
  company.name: www.my.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

microservicecloud-config-eureka-client.yml

spring: 
  profiles: 
    active: 
    - dev
---
server: 
  port: 7001 #注册中心占用7001端口,冒号后面必须要有空格
   
spring: 
  profiles: dev
  application:
    name: microservicecloud-config-eureka-client
    
eureka: 
  instance: 
    hostname: eureka7001.com #冒号后面必须要有空格
  client: 
    register-with-eureka: false #当前的eureka-server自己不注册进服务列表中
    fetch-registry: false #不通过eureka获取注册信息
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/
---
server: 
  port: 7001 #注册中心占用7001端口,冒号后面必须要有空格
   
spring: 
  profiles: test
  application:
    name: microservicecloud-config-eureka-client
    
eureka: 
  instance: 
    hostname: eureka7001.com #冒号后面必须要有空格
  client: 
    register-with-eureka: false #当前的eureka-server自己不注册进服务列表中
    fetch-registry: false #不通过eureka获取注册信息
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/

Config版的eureka服务端

pom

<dependencies>
		<!-- SpringCloudConfig配置 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
		<!-- 热部署插件 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>
</project>

bootstrap.yml

spring: 
  cloud: 
    config: 
      name: microservicecloud-config-eureka-client     #需要从github上读取的资源名称,注意没有yml后缀名
      profile: dev 
      label: master 
      uri: http://config-3344.com:3344      #SpringCloudConfig获取的服务地址

application.yml

spring:
  application:
    name: microservicecloud-config-eureka-client

Config_Git_EurekaServerApplication.java

package com.wby.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer // EurekaServer服务器端启动类,接受其它微服务注册进来
public class Config_Git_EurekaServerApplication
{
	public static void main(String[] args)
	{
		SpringApplication.run(Config_Git_EurekaServerApplication.class, args);
	}
}

测试

Config版的dept微服务

Config版的dept微服务

配置说明

启动3344和7001、8001

启动后

test连接的是2号库

测试通过。

正式测试通过github方式:

启动3344、7001、8001

总结

 

 

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!