分享程序网
首页
  • java
微服务
微前端
环境搭建
数据库
设计模式
算法
软件
解决问题
链接
首页
  • java
微服务
微前端
环境搭建
数据库
设计模式
算法
软件
解决问题
链接
  • 微服务

    • 介绍
  • 微服务搭建

    • 初步搭建
  • 服务发现

    • Eureka
    • nacos
  • 网关

    • zuul
    • 网关配置
    • 过滤器
    • 动态加载
  • 认证(Oauth)

    • 初始化项目
    • Oauth2配置
    • 对外接口
  • 通用服务

    • 通用功能父模块
    • redis
  • 任务调度

    • 任务调度服务
    • xxl-job示例
  • 业务服务

    • 业务设计

zuul

创建pom.xml

<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.shareprog</groupId>
		<artifactId>shareprog-pom</artifactId>
		<version>1.0.0</version>
	</parent>
	<artifactId>shareprog-zuul</artifactId>
	<name>zuul模块</name>
	<description>网关zuul模块</description>

	<dependencies>
		<!-- eureka 注册中心依赖 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<!-- SpringCloud zuul 依赖 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
		</dependency>
		
		<!-- jdbc -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		
		<!--mysql-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

创建启动类

package com.shareprog.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;

import com.shareprog.feign.FeignConfiguration;

/**
 * @ClassName: ZuulApplication
 * @Description: 网关启动类
 * @author cl
 * @date 2021年1月18日
 */
@EnableZuulProxy
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackageClasses = FeignConfiguration.class)
public class ZuulApplication {

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

bootstrap.yml

server:
  port: 8002
eureka:
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true #注册服务器IP,而不是服务器名称
    health-check-url-path: /actuator/health
    lease-renewal-interval-in-seconds: 10 #心跳值;其设置为小于30的值可以加快使客户端连接到其他服务的过程
    lease-expiration-duration-in-seconds: 30
  client:
    registry-fetch-interval-seconds: 5 #指示从eureka获取注册表信息的频率(秒)
    healthcheck:
      enabled: true
    service-url: 
      defaultZone: http://eureka:eureka@localhost:8001/eureka
ribbon.eureka.enabled: false  #禁用ribbon和eureka的关联

application.yml

spring:
  application:
    name: shareprog-zuul
  profiles:
    active: dev

application-dev.yml

spring: 
  datasource: 
    url: jdbc:mysql://127.0.0.1:3306/shareprog?userUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
    username: root
    password: 123456
  redis: 
    host: 127.0.0.1
    database: 1
    password: 123456
Last Updated:
Contributors: clcheng
Next
网关配置