美文网首页
Spring Cloud Zuul 入门

Spring Cloud Zuul 入门

作者: 歌哥居士 | 来源:发表于2019-03-30 12:35 被阅读0次

添加依赖

<?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.3.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>org.baozi</groupId>
    <artifactId>cloud-zuul</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>zuul-registration</module>
        <module>zuul-provider</module>
        <module>zuul-provider2</module>
        <module>zuul-gateway</module>
    </modules>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</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>
<?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.baozi</groupId>
      <artifactId>cloud-zuul</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>zuul-registration</artifactId>

</project>
<?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.baozi</groupId>
      <artifactId>cloud-zuul</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>zuul-gateway</artifactId>

</project>
<?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.baozi</groupId>
      <artifactId>cloud-zuul</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>zuul-provider</artifactId>

</project>
<?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.baozi</groupId>
      <artifactId>cloud-zuul</artifactId>
      <version>1.0</version>
      <relativePath>..</relativePath>
   </parent>

   <artifactId>zuul-provider2</artifactId>

</project>

注册中心

spring.application.name=zuul-registration
server.port=9000

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class ZuulRegistrationApplication {

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

}

服务提供者

spring.application.name=zuul-provider
server.port=10000

eureka.client.service-url.defaultZone=http://localhost:9000/eureka
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ZuulProviderApplication {

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

}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {

    @GetMapping("/m1")
    public String m1() {
        return "provider1";
    }

}
spring.application.name=zuul-provider
server.port=10001

eureka.client.service-url.defaultZone=http://localhost:9000/eureka
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ZuulProvider2Application {

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

}
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {

    @GetMapping("/m1")
    public String m1() {
        return "provider2";
    }

}

zuul网关

spring.application.name=zuul-gateway
server.port=11000

eureka.client.service-url.defaultZone=http://localhost:9000/eureka
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
public class ZuulGatewayApplication {

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

}

通过网关访问:http://zuul的hostname:port/要访问的spring.application.name/位置
http://localhost:11000/zuul-provider/m1

相关文章

网友评论

      本文标题:Spring Cloud Zuul 入门

      本文链接:https://www.haomeiwen.com/subject/vrchvqtx.html