Zuul鐨勪娇鐢紝璺敱璁块棶鏄犲皠瑙勫垯
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zuul鐨勪娇鐢紝璺敱璁块棶鏄犲皠瑙勫垯相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/config' title='config'>config
澶氫釜 framework 鍚敤 涓嶇敤 鍥剧墖 res 鎻掍欢 info涓€銆乑uul鐨勪粙缁?/h1>
Zuul鍖呭惈浜嗗璇锋眰鐨?strong>璺敱
鍜?strong>杩囨护涓や釜鏈€涓昏鐨勫姛鑳斤細鍏朵腑璺敱鍔熻兘璐熻矗灏嗗閮ㄨ姹傝浆鍙戝埌鍏蜂綋鐨勫井鏈嶅姟瀹炲姏涓婏紝鏄疄鐜板閮ㄨ闂粺涓€鍏ュ彛鍩虹鑰岃繃婊ゅ櫒鍔熻兘鍒欒礋璐e璇锋眰鐨勫鐞嗚繃绋嬭繘琛屽共棰勶紝鏄疄鐜拌姹傛牎楠屻€佹湇鍔¤仛鍚堢瓑鍔熻兘鐨勫熀纭€銆?/span>
銆€銆€Zuul鍜宔ureka杩涜鏁村悎锛屽皢Zuul鑷韩娉ㄥ唽鍒癊ureka鏈嶅姟娌荤悊涓嬬殑搴旂敤锛屽悓鏃朵粠Eureka涓幏寰楀叾浠栧井鏈嶅姟鐨勬秷鎭紝涔熷嵆浠ュ悗鐨勮闂井鏈嶅姟閮芥槸閫氳繃Zuul璺宠浆鍚庤幏寰楃殑銆?/span>
娉ㄦ剰锛歓uul鏈€缁堣繕鏄細娉ㄥ唽鍒癊ureka銆?/span>
鎻愪緵=浠g悊+璺敱+杩囨护涓夊ぇ鍔熻兘
浜屻€佽矾鐢辩殑鍩烘湰閰嶇疆
1銆佹柊寤哄伐绋嬶細microservicecloud-zuul-gateway-9527
2銆乸om.xml鏂囦欢娣诲姞渚濊禆鐨刯ar
<dependencies>
<!-- zuul璺敱缃戝叧 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- actuator鐩戞帶 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- hystrix瀹归敊 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 鏃ュ父鏍囬厤 -->
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 鐑儴缃叉彃浠?-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
3銆亂ml鏂囦欢鐨勯厤缃?/span>
server:
port: 9527
spring:
application:
name: microservicecloud-zuul-gateway
# zuul娉ㄥ唽鍒癊ureka
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: gateway-9527.com
prefer-ip-address: true
info:
app.name: yufeng-microcloud
company.name: www.yufeng.com
build.artifactId: $project.artifactId$
build.version: $project.version$
4銆乭osts鐨勪慨鏀?/span>
锛?锛夋墦寮€ C:WindowsSystem32driversetchosts 鏂囦欢锛?/span>
锛?锛夊鍔狅細127.0.0.1 myzuul.com
5銆佷富鍚姩绫伙紝娣诲姞@EnableZuulProxy
@SpringBootApplication
@EnableZuulProxy
public class Zuul_9527_StartSpringCloudApp
{
public static void main(String[] args)
{
SpringApplication.run(Zuul_9527_StartSpringCloudApp.class);
}
}
6銆佹祴璇?/span>
锛?锛夊惎鍔ㄤ笁涓猠ureka闆嗙兢, 涓€涓猰icroservicecloud-provider-dept-8001锛屽惎鍔ㄨ矾鐢辩綉鍏冲井鏈嶅姟锛?/span>
锛?锛変笉鐢ㄨ矾鐢卞拰鍚姩璺敱鐨勭粨鏋滀竴鏍凤紱
涓嶇敤璺敱锛歨ttp://localhost:8001/dept/get/2
鍚敤璺敱锛歨ttp://myzuul.com:9527/microservicecloud-dept/dept/get/2
涓夈€佽矾鐢辫闂槧灏勮鍒?/span>
浠ヤ笂浣跨敤 http://myzuul.com:9527/microservicecloud-dept/dept/get/2 璁块棶锛屽垯鏆撮湶鍑轰簡璋冪敤鐨勫井鏈嶅姟鍚嶄负 microservicecloud-dept锛涗负浜嗕笉鏆撮湶鍑哄井鏈嶅姟鐨勭湡姝e悕绉帮紝浣跨敤璺敱璁块棶鏄犲皠锛?/span>
1銆佸湪yml鏂囦欢涓鍔犺矾鐢辨槧灏?/span>
# 璺敱鏄犲皠
zuul:
routes:
mydept:
serviceId: microservicecloud-dept
path: /mydept/**
serviceId锛氭槧灏勭殑鏈嶅姟鍚嶏紱path锛氭槧灏勬垚涓虹殑璺緞锛?/span>
鍦▂ml淇敼涔嬪墠浣跨敤锛歨ttp://myzuul.com:9527/microservicecloud-dept/dept/get/2 璁块棶锛?/span>
鍦▂ml淇敼涔嬪悗浣跨敤锛歨ttp://myzuul.com:9527/mydept/dept/get/2 璁块棶锛?/span>
2銆佹鏃朵娇鐢?nbsp;http://myzuul.com:9527/microservicecloud-dept/dept/get/2 鍜?nbsp;http://myzuul.com:9527/mydept/dept/get/2 閮藉彲浠ユ垚鍔熺殑璋冪敤璇ユ帴鍙o紝杩樻槸浼氭毚闇茶皟鐢ㄧ殑寰湇鍔″悕涓簃icroservicecloud-dept锛涙墍浠ラ渶瑕佸幓璁╀娇鐢?nbsp;http://myzuul.com:9527/microservicecloud-dept/dept/get/2 閾炬帴涓嶈兘澶熻闂紱
锛?锛夊洜姝わ紝闇€瑕佸湪yml鏂囦欢涓鍔犲師鏈嶅姟鐨勫拷鐣ワ細
# 璺敱鏄犲皠
zuul:
ignored-services: microservicecloud-dept
routes:
mydept:
serviceId: microservicecloud-dept
path: /mydept/**
ignored-services:鐗瑰埆娉ㄦ剰姝ゅ鐨勯厤缃€傞€氳繃閰嶇疆姝ら€夐」锛岄檺鍒朵笉鑳介€氳繃璁块棶鏈嶅姟鍚嶅幓璁块棶寰湇鍔?/span>
濡傛灉鎯虫妸鍏朵粬寰堝鐨勫井鏈嶅姟閮介€氳繃閰嶇疆蹇界暐鎺夌湡瀹炶闂矾寰勫垯鐩存帴浣跨敤 ”*” 鏉ヤ唬鏇裤€?/span>
锛?锛夐噸鍚井鏈嶅姟
<1> 浣跨敤鍘熻矾寰勮闂姤閿欙紝http://myzuul.com:9527/microservicecloud-dept/dept/get/2
<2> 浣跨敤璺敱鏄犲皠璺緞鍙互姝e父璁块棶锛歨ttp://myzuul.com:9527/mydept/dept/get/2
锛?锛夊湪瀹為檯鐨勪娇鐢ㄦ椂鍊欙紝鏈夊緢澶氫釜鏈嶅姟鍣ㄩ渶瑕侀檺鍒朵笉鑳介€氳繃鏈嶅姟鍚嶅幓璁块棶锛涘垯鍦▂ml鍋氬涓嬬殑淇敼锛?/span>
# 璺敱鏄犲皠
zuul:
ignored-services: "*"
routes:
mydept:
serviceId: microservicecloud-dept
path: /mydept/**
娴嬭瘯缁撴灉涓庝笂闈㈢殑缁撴灉涓€鏍凤紱
3銆佽缃粺涓€鍏叡鍓嶇紑
锛?锛変慨鏀箉ml鏂囦欢锛屽涓嬫墍绀?/span>
# 璺敱鏄犲皠
zuul:
prefix: /yufeng
ignored-services: "*"
routes:
mydept:
serviceId: microservicecloud-dept
path: /mydept/**
锛?锛夋祴璇?/span>
<1> 鍦ㄦ祻瑙堝櫒涓墦寮€锛歨ttp://myzuul.com:9527/mydept/dept/get/2锛屾姤閿欏涓嬶細
<2> 鍦ㄦ祻瑙堝櫒涓墦寮€锛歨ttp://myzuul.com:9527/yufeng/mydept/dept/get/2锛屽彲浠ユ甯歌闂紱
以上是关于Zuul鐨勪娇鐢紝璺敱璁块棶鏄犲皠瑙勫垯的主要内容,如果未能解决你的问题,请参考以下文章