SpringCloud教程十:Zuul+Mysql实现动态路由

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wang_shuyu/article/details/79304417

实际开发中随着服务的增多,通过配置来设置zuul的路由就会变的很麻烦,给运维带来很大的不变,这里我们通过数据库获取的方式来实现动态路由的配置:

首先sql:

CREATE TABLE `gateway_api_define` (
  `id` VARCHAR(50) NOT NULL,
  `path` VARCHAR(255) NOT NULL,
  `service_id` VARCHAR(50) DEFAULT NULL,
  `url` VARCHAR(255) DEFAULT NULL,
  `retryable` TINYINT(1) DEFAULT NULL,
  `enabled` TINYINT(1) NOT NULL,
  `strip_prefix` INT(11) DEFAULT NULL,
  `api_name` VARCHAR(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

配置文件;

server.port=7084
spring.application.name=zuul-server
eureka.instance.hostname=peer1
#注册服务地址
eureka.client.service-url.defaultZone=http://peer1:7082/eureka/,http://peer2:7082/eureka/
#启用shutdown
endpoints.shutdown.enabled=true
#禁用/开启密码验证
endpoints.shutdown.sensitive=false
#使用OkHttpClient的API来调用集群服务
#ribbon.okhttp.enabled=true
#这里的配置表示,访问/it/**
#zuul.routes.service-hi.path=/test/**
#直接重定向到
#zuul.routes.service-hi.serviceId=SERVICE-HI

# 续约更新时间间隔(默认30秒)
eureka.instance.lease-renewal-interval-in-seconds=5
# 续约到期时间(默认90秒)
eureka.instance.lease-expiration-duration-in-seconds=15


# 数据库访问配置
# 主数据源,默认的
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://172.16.1.28:3306/springcloud
spring.datasource.username=cweserver
spring.datasource.password=cweserveryzhh

# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROMDUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

主启动类:

@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ZuulserverApplication {

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

}

pom文件:

<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
   <java.version>1.8</java.version>
   <spring-cloud.version>Edgware.SR1</spring-cloud.version>
</properties>

<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</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-zuul</artifactId>
   </dependency>

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>

   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
   </dependency>

   <dependency>
      <groupId>com.squareup.okhttp3</groupId>
      <artifactId>okhttp</artifactId>
   </dependency>


   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-jdbc</artifactId>
   </dependency>

   <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
   </dependency>

   <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.29</version>
   </dependency>
   <!--zipkin-->
   <!--<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-zipkin</artifactId>
   </dependency>-->
   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
   </dependency>
   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
   </dependency>
</dependencies>

<dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-dependencies</artifactId>
         <version>${spring-cloud.version}</version>
         <type>pom</type>
         <scope>import</scope>
      </dependency>
   </dependencies>
</dependencyManagement>
这样就可以实现路由的动态配置了

猜你喜欢

转载自blog.csdn.net/wang_shuyu/article/details/79304417