SpringCloud集群使用 zuul


小的demo

@EnableZuulProxy
@SpringBootApplication
public class Application {
    public static void main(String[] args){
      SpringApplication.run(Application.class,args);
    }
}


zuul:
  routes:
    routeTest:
     path: /routeTest/163
     url: http://www.163.com
server:
  port: 8080


访问:localhost:8080/routeTest/163 会跳到163网站

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
  </parent>


  <dependencies>

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

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
   <!-- 还没有启动Eruka服务器的时候 时候下面的会报错找不到euraka服务器
    所以要注释-->
<!--
    <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>-->

  </dependencies>


  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Dalston.SR1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>


路由规则:



zuul:
  ignored-services: "*"            # 设置忽略的服务,即配置后将不会被路由(但对于明确配置在路由中的,将不会被忽略)
  routes:
    api-cal-url:                   # 基于 URL 的映射(这里自定义路由的名字为 api-cal-url,它可任意指定,唯一即可)
      path: /cal/**                # http://127.0.0.1:4100/cal/add?a=7&b=17会路由至http://127.0.0.1:2100/add?a=7&b=17
      url: http://127.0.0.1:2100/
    api-add:                       # 基于 ServiceId 的映射(自定义路由的名字)
      path: /caladd/**             # http://127.0.0.1:4100/caladd/add?a=6&b=16会路由至CalculatorServer服务的/add?a=6&b=16
      serviceId: CalculatorServer
    CalculatorServer:              # 基于 ServiceId 的映射(路由的名字等于 ServiceId 的情况下,serviceId 属性可以省略)
      path: /mycall/**             # http://127.0.0.1:4100/mycall/add?a=5&b=15会路由至CalculatorServer服务的 /add?a=5&b=15
      #serviceId: CalculatorServer


@EnableEurekaClient  @EnableFeignClients @EnableZuulProxy 都有相同的作用  都可以注册到Eureka服务器上  作为客户端

四个项目  一个Eureka服务器   三个客户端  服务提供者   调用者  以及zuul网关

    

Eureka服务器  zuul_eureka_server

 启动类:

@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {
    public static void main(String[] args){
       new SpringApplicationBuilder(ServerApplication.class).run(args);
    }

}

application.yml

server:
  port: 8761

pom.xml:

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.2.RELEASE</version>
</parent>


<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Dalston.SR1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
  </dependency>
</dependencies>


<build>
  <finalName>spring_hystrix_server</finalName>
</build>


服务提供者:zuul_eureka_provide

启动类  注意first-service-provider是 服务 提供者的服务名称用于服务调用者使用  下面还有服务调用者的服务名称first-service-provider 用于zuul网管使用

@SpringBootApplication
@EnableEurekaClient
public class ProvideApplication {
       public static void main(String[] args){
         new  SpringApplicationBuilder(ProvideApplication.class).run(args);
       }
}

application.properties

#spring mvc的配置
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp


#springCloud的配置
#在默认设置下,Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为。禁止方式如下
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false

server.port=8081
spring.application.name=first-service-provider
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/


 提供一个controller: 获取book

@RestController
public class MyController {

         @RequestMapping("/person/{personId}")
    public Person findPerson(@PathVariable("personId") Integer personId){

              Person person=new Person(1,"tom",12);
              return person;
         }


    @RequestMapping("/book/{bookId}")
    public Book getBook(@PathVariable("bookId") Integer bookId){
        Book book=new Book(1,"xx","df");

        return book;
    }


pom.xml

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.2.RELEASE</version>
</parent>


<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Dalston.SR1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
  </dependency>



    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

</dependencies>
<build>
  <finalName>spring_hystrix_server</finalName>
</build>


服务调用者:zuul_eureka_invoke

启动类:

@SpringBootApplication
@EnableDiscoveryClient  //在本类中还有其他客户端
@EnableFeignClients
public class InvokerApplication {


     public static void main(String[] args){

       SpringApplication.run(InvokerApplication.class,args);

     }
}


提供一个Service接口 未实现   注意这里调用的是注册的服务提供者的服务  获取book

@FeignClient("first-service-provider")
public interface BookService {

    @RequestMapping("/book/{bookId}")
    Book getBook(@PathVariable("bookId") Integer bookId);

}


提供一个controllert使用Server接口的方法 调用服务提供者的方法  在实现自己的逻辑

@RestController
public class BookController {

    @Autowired
    BookService bookService;


     @RequestMapping("/testBook")
    public String testBook(){
          Book book=bookService.getBook(3);
           System.out.println("书名是:"+book.getName());
           return "SUCCESS";
       }
}


application.yml   注意服务名称first-service-provider1

server:

  port: 9000

spring:

  application:

    name: first-service-provider1

eureka:

  instance:

    hostname: localhost

  client:

    serviceUrl:

      defaultZone: http://localhost:8761/eureka/



pom.xml

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.2.RELEASE</version>
</parent>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Dalston.SR1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>

  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
  </dependency>

  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
  </dependency>

  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
  </dependency>


</dependencies>


<build>
  <finalName>spring_hystrix_server</finalName>
</build>


服务网关:zuul_eureka_book 

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@SpringBootApplication
public class GatewayApplication {
    public static void main(String[] args){
        SpringApplication.run(GatewayApplication.class,args);

    }

}


application.yml

spring:
  application:
    name: zuul-gateway
eureka:
  instance:
    hostname: localhost
  client:
   serviceUrl:
    defaultZone: http://localhost:8761/eureka/


zuul:
 routes:
  sale:
    path: /sale/**
    serviceId: first-service-provider


先启动zuul_euraka_server  在zuul_euraka_provide  在  zuul_eureka_invoke 在 zuul_eureka_book

在访问:http://localhost:8761/    三个客户端都以及注册到服务器上


注意路由的规则


猜你喜欢

转载自blog.csdn.net/didi7696/article/details/80002734