网关Zuul/集群中使用Zuul(二)

  • SpringCloud中来使用Zuul
  • Zuul自带负载均衡功能
  • 回顾原来的SpringCloud集群

  • 在SpringCloud中使用Zuul框架后(Zuul网关也会作为客户端注册到Eureka服务器中)

1、建立集群项目

  • Eureka服务器
  • 服务提供者
  • 服务调用者
  • Zuul网关

1.1、Eureka服务器端(8761)

1.1.1、依赖
<!-- Spring Cloud -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- Eureka服务器 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
1.1.2、配置
server:
  port: 8761

eureka:
 client:
  registerWithEureka: false
  fetchRegistry: false

1.2、服务提供者(8080)

1.2.1、依赖
<!-- Spring Cloud -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- 服务提供者 -->
<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>
<!-- 服务提供者 -->
1.2.2、配置
spring:
 application:
  name: atm-zuul-provider
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8761/eureka/
1.2.3、MyController
package com.atm.cloud;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @RequestMapping(value = "/person/{personId}", method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Person findPerson(@PathVariable("personId")Integer personId){
        Person person=new Person();

        person.setId(personId);
        person.setAge(18);
        person.setName("atm");

        return person;
    }

}
1.2.4、启动类
package com.atm.cloud;

import java.util.Scanner;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ZuulProviderApp {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String port = scanner.nextLine();

        new SpringApplicationBuilder(ZuulProviderApp.class).properties(
                "server.port=" + port).run(args);
    }

}

1.3、服务调用者(9000)

1.3.1、依赖
<!-- Spring Cloud -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- 服务调用者 -->
<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>
<!-- 服务调用者 -->
<!-- SpringCloud 整合 Feign -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
1.3.2、配置
server:
 port: 9000
spring:
 application:
  name: atm-zuul-invoker
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8761/eureka/
1.3.3、HelloClient接口
package com.atm.cloud;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient("atm-zuul-provider")
public interface HelloClient {
    @RequestMapping(value = "/person/{id}", method = RequestMethod.GET)
    Person getPerson(@PathVariable("id") Integer id);
}
1.3.4、MyHelloClient实现
package com.atm.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MyHelloClient {

    @Autowired
    private HelloClient helloClient;

    @RequestMapping(value = "/person/{id}", method = RequestMethod.GET)
    public Person sayHello(@PathVariable("id") Integer id) {
        Person result = helloClient.getPerson(id);
        return result;
    }
}
1.3.5、启动类
package com.atm.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients//开启Feign
public class ZuulInvokerApp {

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

}

1.4、Zuul网关(8085)

1.4.1、依赖
<!-- Spring Cloud -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- Zuul -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<!-- HttpClient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</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>
<!-- 服务提供者 -->

1.4.2、配置
server:
  port: 8085
###访问myServer的均转发至atm-zuul-invoker
zuul:
  routes:
    myServer:
     path: /myServer/**
     serviceId: atm-zuul-invoker
spring:
 application:
  name: atm-zuul-router
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8761/eureka/

2、启动测试

  • 启动Eureka服务器端
  • 启动服务提供者
  • 启动服务调用者
  • 启动Zuul网关项目
  • 浏览器访问http://127.0.0.1:8085/myServer/person/5
  • 第一次访问可能报错,bean没加载完成(解决方法暂不做介绍)

3、更换Http客户端

  • Ribbon默认使用HttpClient
  • 可更换OkHttpClient
ribbon.okhttp.enable=true

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
</dependency>
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐