1 新建微服務工程 導入須要的依賴java
<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.shi.springCloud04</groupId> <artifactId>springCloud04-core</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>
2 修改yml文件web
server: port: 9527 spring: application: name: zuul-getway-9527 zuul: prefix: /shi #訪問前綴 ignored-services: "*" #全部真實姓名進行隱藏 routes: mydept.serviceId: SPRINGCLOUD04-PRODECT-8001 #真實訪問的服務名稱 mydept.path: /mydept/** #虛擬訪問名稱 eureka: client: #客戶端註冊進eureka服務列表內 service-url: defaultZone: http://eureka9001.com:9001/eureka/,http://eureka9002.com:9002/eureka/,http://eureka9003.com:9003/eureka/ info: app.name: zuul-getway-9527 company.name: com.shi build.artifactId: $project.artifactId$ build.version: $project.version$
3 建立啓動類配置啓動項spring
package com.shi.zuul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; /** * 開啓服務代理網關配置 * @author SHF * @version 建立時間:2018年12月3日 下午4:16:35 */ @SpringBootApplication @EnableZuulProxy public class SpringZuul_APP_9527 { public static void main(String[] args) { SpringApplication.run(SpringZuul_APP_9527.class, args); } }
測試: http://localhost:9527/shi/mydept/dept/get/1app