基于SpringBoot 2.0的spring cloud : 网关Zuul

总的pom如下:
pom.xml


         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
   
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
       
   

    com.example
    parent
    0.0.1-SNAPSHOT
    parent
    Demo project for Spring Boot

   
        eureka-server
        one-service
        zuul-service
   

   
        UTF-8
        UTF-8
        1.8
        Finchley.SR1
   

   
       
           
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
           

       

   

   
       
       
            org.springframework.cloud
            spring-cloud-starter-config
       

       
       
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
       

       
       
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
       

       
            org.springframework.boot
            spring-boot-starter
       

       
            org.springframework.boot
            spring-boot-starter-test
            test
       

       
            org.springframework.boot
            spring-boot-starter-web
       

   

eureka-server的pom.xml


         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
   
        com.example
        parent
        0.0.1-SNAPSHOT
       
   

    com.example
    eureka-server
    0.0.1-SNAPSHOT
    eureka-server
    Demo project for Spring Boot

one-service的pom.xml


         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
   
        com.example
        parent
        0.0.1-SNAPSHOT
       
   

    com.example
    one-service
    0.0.1-SNAPSHOT
    eureka-server
    Demo project for Spring Boot

two-service的pom.xml


         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
   
        com.example
        parent
        0.0.1-SNAPSHOT
       
   

    com.example
    two-service
    0.0.1-SNAPSHOT
    eureka-server
    Demo project for Spring Boot

zuul-service的pom.xml


         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0
   
        com.example
        parent
        0.0.1-SNAPSHOT
       
   

    com.example
    zuul-service
    0.0.1-SNAPSHOT
    eureka-server
    Demo project for Spring Boot


配置文件:
euraka注册中心:
eureka-server:
application.yml

#端口号
server:
  port: 8888
#Eureka实例名,集群中根据这里相互识别
eureka:
  instance:
    hostname: eureka-server
#客户端
  client:
    service-url:
      defaultZone: http://localhost:8889/eureka/
  server:
#是否开启安全保护,默认是开启的,如果默认开启,注册中心的服务列表就算有些服务断开了,也会继续保存
    enable-self-preservation: false
spring:
  application:
    name: eureka-service

服务1,服务2通过euraka在注册中心eureka-server注册,

服务1:
one-service:
application.yml

#端口号
server:
  port: 8083
#Eureka实例名,集群中根据这里相互识别
spring:
  application:
    name: eureka-service

eureka:
#客户端
  client:
#注册中心地址
    service-url:
      defaultZone: http://localhost:8888/eureka/

服务2:
two-service:
application.yml

#端口号
server:
  port: 8082
#Eureka实例名,集群中根据这里相互识别
spring:
  application:
    name: eureka-service

eureka:
#客户端
  client:
#注册中心地址
    service-url:
      defaultZone: http://localhost:8888/eureka/

服务zuul:
zuul-service:
application.yml

#端口号
server:
  port: 8084
#Eureka实例名,集群中根据这里相互识别
spring:
  application:
    name: zuul-service
eureka:
#客户端
  client:
#注册中心地址
    service-url:
      defaultZone: http://localhost:8888/eureka/
zuul:
  routes:
    eureka-service: /api-a/**

启动类:
eureka-server的启动类
EurekaServerApplication.java

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication{

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

}


one-service的启动类
OneServiceApplication.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @SpringCloudApplication注解,它整合了@SpringBootApplication、@EnableEurekaClient、@EnableCircuitBreaker,主要目的还是简化配置。
 * 这里暂时需要 @SpringBootApplication、@EnableEurekaClient
 */
@SpringCloudApplication
public class OneServiceApplication {

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

}


two-service的启动类
TwoServiceApplication.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 *  @SpringCloudApplication注解,它整合了@SpringBootApplication、@EnableEurekaClient、@EnableCircuitBreaker,主要目的还是简化配置。
 *  这里暂时需要 @SpringBootApplication、@EnableEurekaClient
 */
@SpringCloudApplication
public class TwoServiceApplication{

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

}

zuul-service的启动类
ZuulServiceApplication.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * @SpringCloudApplication注解,它整合了@SpringBootApplication、@EnableEurekaClient、@EnableCircuitBreaker,主要目的还是简化配置。
 * 这里暂时需要 @SpringBootApplication、@EnableEurekaClient、@EnableZuulProxy
 */
@EnableZuulProxy
@SpringCloudApplication
public class ZuulServiceApplication{

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

}

对one-service,two-service服务测试

one-service:
TestController.java

package com.example;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController{
    @RequestMapping(value = "/test" ,method = RequestMethod.GET)
    public String test() {
        return "==============>>>>>>>hello,I am springCloud one-service<<<<<<<================="; //测试代码直接返回一个字符串,不再调用service层等等。
    }
}

two-service:
Testcontroller.java

package com.example;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController{
    @RequestMapping(value = "/test" ,method = RequestMethod.GET)
    public String test() {
        return "==============>>>>>>>hello,I am springCloud two-service<<<<<<<================="; //测试代码直接返回一个字符串,不再调用service层等等。
    }
}
 

 

部分效果

项目目录结构:

基于SpringBoot 2.0的spring cloud : 网关Zuul_第1张图片

eureka服务的注册

基于SpringBoot 2.0的spring cloud : 网关Zuul_第2张图片

service-one,service-two两个服务的测试

 

zuul轮询结果

你可能感兴趣的:(springboot)