温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Cloud集群怎么使用Zuul

发布时间:2021-12-24 10:29:15 来源:亿速云 阅读:110 作者:小新 栏目:云计算

这篇文章给大家分享的是有关Spring Cloud集群怎么使用Zuul的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

Spring Cloud集群使用Zuul

        在前面小节的例子中,Zuul将请求转发到一个Web项目进行处理,如果实际处理请求的不是一个Web项目,而是整个微服务集群,那么Zuul将成为整个集群的网关。在加入Zuul前,Spring Cloud集群的结构请见图7-3。

Spring Cloud集群怎么使用Zuul

图7-3 原来的Spring Cloud集群结构

        为微服务集群加入Zuul网关后,结构如图7-4所示。

Spring Cloud集群怎么使用Zuul

图7-4 加入Zuul后集群结构

        在深入学习Zuul前,先按图7-4的搭建本章的测试项目。

集群搭建

        假设当前需要实现一个书本销售业务,在销售模块中需要调用书本模块的服务,用来查找书本,本小节案例以此为基础,建立以下项目:

  • zuul-eureka-server:Eureka服务器,应用端口为8761,读者可以到以下目录取得源代码:codes\07\03\zuul-eureka-server。

  • zuul-book-service:书本模块,属于服务提供者,提供“/book/{bookId}”服务,用于查找图书,最后返回Book的JSON字符串,应用端口为9000,代码目录codes\07\03\zuul-book-service。

  • zuul-sale-service:销售模块,属于服务调用者,对外发布销售服务“/sale-book/{bookId}”,该服务中会调用zuul-book-service来查找Book,应用端口为9100,代码目录codes\07\03\zuul-sale-service。

        书本模块“zuul-book-service”发布的服务,仅返回一个简单的Book对象,控制器代码如下:

    @RequestMapping(value = "/book/{bookId}", method = RequestMethod.GET, 
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Book findBook(@PathVariable Integer bookId) {
        Book book = new Book();
        book.setId(bookId);
        book.setName("Workflow讲义");
        book.setAuthor("杨恩雄");
        return book;
    }

        销售模块“zuul-sale-service”发布的服务,相关代码如下:

@FeignClient("zuul-book-service") // 声明调用书本服务
public interface BookService {

    /**
     * 调用书本服务的接口,获取一个Book实例
     */
    @RequestMapping(method = RequestMethod.GET, value = "/book/{bookId}")
    Book getBook(@PathVariable("bookId") Integer bookId);
}

@RestController
public class SaleController {

    @Autowired
    private BookService bookService;

    @RequestMapping(value = "/sale-book/{bookId}", method = RequestMethod.GET)    public String saleBook(@PathVariable Integer bookId) {
        // 调用book服务查找
        Book book = bookService.getBook(bookId);
        // 控制台输入,模拟进行销售
        System.out.println("销售模块处理销售,要销售的图书id: " + book.getId() + ", 书名:"
                + book.getName());
        // 销售成功
        return "SUCCESS";
    }
}

        销售模块的服务,使用Feign来调用书本模块的服务来获取Book实例,然后在控制台中输出信息,在实际应用中,销售的过程会更为复杂,例如有可能涉及支付等内容,本例为了简单起见,仅作简单的输出。接下来,创建网关项目。

路由到集群服务

        在前一小节的基础上,新建一个名称为“zuul-gateway”的Maven项目(代码目录codes\07\03\zuul-gateway),在pom.xml中加入以下依赖:

        <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-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>

        新建应用类,如代码清单7-3所示。

        代码清单7-3:

        codes\07\03\zuul-gateway\src\main\java\org\crazyit\cloud\GatewayApplication.java

@EnableZuulProxy
@SpringBootApplication
public class GatewayApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(GatewayApplication.class).properties(
                "server.port=8080").run(args);
    }
}

        应用类跟前面的例子一致,使用@EnableZuulProxy注解,但是,由于网关项目需要加入到集群中,因此要修改配置文件,让其注册到Eureka服务器中。本例的配置文件如代码清单7-4。

        代码清单7-4:codes\07\03\zuul-gateway\src\main\resources\application.yml

spring:
  application:
    name: zuul-gateway
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
zuul: 
  routes:
    sale:
      path: /sale/**
      serviceId: zuul-sale-service

        使用eureka的配置,将自己注册到8761的Eureka中。在配置Zuul时,声明所有的“/sale/**”请求,将会被转发到Id为“zuul-sale-service”的服务进行处理。

        一般情况下,配置了serviceId后,在处理请求的“routing”阶段,将会使用一个名称为RibbonRoutingFilter的过滤器,该过滤器会调用Ribbon的API来实现负载均衡,默认情况下调用HttpClient来调用集群服务。

        按照以下顺序启动集群:

  • 启动zuul-eureka-server(Eureka服务器)。

  • 启动zuul-book-service(服务提供者)。

  • 启动zuul-sale-service(服务调用者)。

  • 启动zuul-gateway(集群网关)。

        在浏览器中访问:http://localhost:8080/sale/sale-book/1,返回“SUCCESS”字符串,在销售模块的控制台,可以看到输出如下:

销售模块处理销售,要销售的图书id: 1, 书名:Workflow讲义

        根据输出可知,销售模块、书本模块均被调用。本例涉及了4个项目,图7-5展示了本例的结构,可帮助读者理解本例。

Spring Cloud集群怎么使用Zuul

图7-5 本例的结构

Zuul Http客户端

        我们知道,Ribbon用来实现负载均衡,Ribbon在选取了合适的服务器后,再调用REST客户端API来调用集群服务。在默认情况下,将使用HttpClient的API来调用集群服务。除了HttpClient外,还可以使用OkHttpClient,以及com.netflix.niws.client.http.RestClient,RestClient目前已经不推荐使用,如果想启用OkHttpClient,可以添加以下配置:ribbon.okhttp.enabled=true。除了该配置外,还要在pom.xml中加入OkHttpClient的依赖:

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

感谢各位的阅读!关于“Spring Cloud集群怎么使用Zuul”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI