springcloud zuul

新建工程spring-cloud-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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>consul-test</groupId>
	<artifactId>zuul-test</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>consul-test</name>
	<packaging>jar</packaging>
	<description>zuul-test</description>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>
	<dependencyManagement>
		<dependencies>
			<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> 
				<version>2.0.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> -->
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M8</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<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>
	</dependencies>
</project>

  application.properties

spring.application.name=spring-cloud-zuul
server.port=8506
zuul.routes.spring-cloud-consul-producer.path=/service-producer/**
#zuul.routes.spring-cloud-consul-producer.service-id=service-producer

  bootstrap.yml

spring:
  cloud:
    config:
      uri: http://localhost:8500

  demo启动代码

package test;

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.web.bind.annotation.RestController;

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

}

启动之前的SpringCloudConsulProducerApplication,然后启动Application

在consul上可以看到注册的服务,http://127.0.0.1:8500/ui/dc1/services

除了consul自身外,有两个:service-producer和spring-cloud-zuul

使用zuul的ip和端口访问producer的hello接口:http://127.0.0.1:8506/service-producer/hello

可以正常访问。

原文地址:https://www.cnblogs.com/yaoyu1983/p/12444095.html