Spring Cloud集群怎么使用Zuul

54次阅读
没有评论

共计 4016 个字符,预计需要花费 11 分钟才能阅读完成。

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

Spring Cloud 集群使用 Zuul

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

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

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

图 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 展示了本例的结构,可帮助读者理解本例。

图 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”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-16发表,共计4016字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)