6.Spring Cloud初相识-------Zool路由

前言:

在生产环境中,我们不可能将每个服务的真实信息暴漏出去,因为这样太不安全。

我们会选择使用路由代理真实的服务信息,由它负责转发给真实的服务。

新建一个Zool:

1.添加依赖

<?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.xm.cloud</groupId>
	<artifactId>cl_zool</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>cl_zool</name>
	<description>This is a Web about springcloud</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<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>Finchley.SR2</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</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>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

2.修改配置

server.port=9090
spring.application.name=cl-zool-gateway

eureka.client.service-url.defaultZone=http://127.0.0.1:7001/eureka/
#服务名统一前缀
zuul.prefix=/xm
#所有服务真实名称隐藏
zuul.ignored-services="*"
zuul.routes.myHello.service-id=CL-HELLO-PRODUCER
zuul.routes.myHello.path=/myHello/**

3.开启注解

package com.xm.cloud;

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;

@EnableDiscoveryClient
@EnableZuulProxy
@SpringBootApplication
public class ClZoolApplication {

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

测试:

开启eureka7001、服务生产者8001、路由zool9090

1.访问Eureka客户端:

2.访问服务生产者localhost:8001/hello

3.访问不加前缀真实服务名称http://localhost:9090/CL-HELLO-PRODUCER/hello:

4.访问加前缀真实服务名称http://localhost:9090/xm/CL-HELLO-PRODUCER/hello:

5.访问不加前缀路由服务名称http://localhost:9090/myHello/hello:

6.访问加前缀路由服务名称http://localhost:9090/xm/myHello/hello:

posted @ 2018-10-25 17:53  零度微笑  阅读(1312)  评论(0编辑  收藏  举报