zl程序教程

您现在的位置是:首页 >  后端

当前栏目

SpringCloud+Consul配置Zuul网关服务

SpringCloud配置服务 网关 Consul zuul
2023-09-11 14:20:01 时间

转发的目标服务microservice-provider-user配置

server:
  port: 8000
spring:
  application:
    name: microservice-provider-user
  cloud:
    consul:
      discovery:
        instance-id: ${spring.application.name}:${server.port}
        prefer-ip-address: true
        healthCheckPath: /health
        healthCheckInterval: 15s
        hostname: ${spring.application.name}
        service-name: ${spring.application.name}
        enabled: true
        register: true
        heartbeat:
          enabled: true
      host: aliyun.lzh
      port: 8500  
      enabled: true

logging:                                # 配置日志级别,让hibernate打印出执行的SQL
  level:
    root: INFO
    org.hibernate: INFO
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE
    org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
<?xml version="1.0" encoding="UTF-8"?>
<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>
  <groupId>com.itmuch.cloud</groupId>
  <artifactId>microservice-gateway-zuul</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <!-- 引入spring boot的依赖 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
    <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  </dependencies>

  <!-- 引入spring cloud的依赖 -->
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Edgware.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <!-- 添加spring-boot的maven插件 -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>
package com.itmuch.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

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

Zuul网关microservice-gateway-zuul 配置

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
  cloud:
    consul:
      discovery:
        instance-id: ${spring.application.name}:${server.port}
        prefer-ip-address: true
        healthCheckPath: /health
        healthCheckInterval: 15s
        hostname: ${spring.application.name}
        service-name: ${spring.application.name}
        enabled: true
        register: true
        heartbeat:
          enabled: true
      host: aliyun.lzh
      port: 8500  
      enabled: true
      
management:
  security:
    enabled: false

zuul:
  ignored-services: '*'   # 使用'*'可忽略所有微服务
  routes:
    microservice-provider-user: /user/**
    
    
hystrix:
 command:
   default:
     execution:
       timeout:
         enabled: false
       isolation:
         thread:
           timeoutInMilliseconds: 60000

ribbon:
  ReadTimeout: 60000
  ConnectTimeout: 60000
<?xml version="1.0" encoding="UTF-8"?>
<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>
	<groupId>com.itmuch.cloud</groupId>
	<artifactId>microservice-provider-user</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<!-- 引入spring boot的依赖 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

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

		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</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-consul-discovery</artifactId>
		</dependency>

	</dependencies>

	<!-- 引入spring cloud的依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Edgware.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
</project>

遇到的问题:

请求接口http://localhost:8040/user/123 正常应该被zuul网关转发到microservice-provider-user服务的接口中但是测试过程中一直出现

com.netflix.client.ClientException: Load balancer does not have available server for client

 原因:zuul网关服务microservice-gateway-zuul 的健康检查服务没开启,

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
spring.cloud.consul.heartbeat.enabled = true

spring.cloud.consul.healthCheckPath = /health 

spring.cloud.consul.healthCheckInterval = 15s

完整代码:

https://gitee.com/lzhcode/spring-cloud-docker-microservice-book-code/tree/master/microservice-gateway-zuul

https://gitee.com/lzhcode/spring-cloud-docker-microservice-book-code/tree/master/microservice-provider-user