springcloud學習服務網關zuul-初步學習

1.打開idea建立springclud的zuul工程java

選中上圖的zuul,進行建立spring

建立成功apache

2.進行初步測試api

啓動類增長網關支持app

package com.example.zuul;

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);
   }

}

3. 配置文件,增長相關配置,轉發路由到網址maven

spring.application.name=gateway-service-zuul
server.port=8888

#這裏的配置表示,訪問/it/** 直接重定向到http://www.ityouknow.com/**
#zuul.routes.baidu.path=/it/**
#zuul.routes.baidu.url=http://www.ityouknow.com/
#zuul.routes.hello.path=/api/**
#zuul.routes.hello.url=http://localhost:9001/

啓動項目,請求,進行測試http://localhost:8888/it/spring-cloud  請求成功ide

接下來,進行改造,改形成一個微服務,註冊到eurekaspring-boot

1.pom文件,增長依賴eureka微服務

<?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>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.5.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>zuul</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>zuul</name>
   <description>Demo project for Spring Boot</description>

   <properties>
      <java.version>1.8</java.version>
      <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
   </properties>

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

      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>

      <!-- 增長服務註冊中心依賴 -->
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </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.配置文件,增長配置,註冊到eureka。而且對以前的微服務,根據路由,轉發不一樣的id測試

spring.application.name=gateway-service-zuul
server.port=8888

#這裏的配置表示,訪問/it/** 直接重定向到http://www.ityouknow.com/**
#zuul.routes.baidu.path=/it/**
#zuul.routes.baidu.url=http://www.ityouknow.com/
#zuul.routes.hello.path=/api/**
#zuul.routes.hello.url=http://localhost:9001/

zuul.routes.api-a.path=/producer/**
zuul.routes.api-a.serviceId=spring-cloud-producer

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3.啓動註冊中心,啓動spring-cloud-producer 服務,啓動zuul服務。進行訪問

 

ok了,zuul初級的集成已經完成了。

相關文章
相關標籤/搜索