springCloud学习(三)之Zuul网关组件和Ribbon负载均衡组件

  1. zuul是什么

Zuul是netflix提供的一个开源的api网关服务器,是客户端和网站后端所有请求的中间层,对外开放一个api将所有的请求导入同一的入口,屏蔽了服务端的具体实现逻辑,Zuul可以实现反向代理的功能,在网关内部实现动态路由,身份认证,ip过滤,数据监控

2.zuul的maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>ygdSpringCloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>zuul</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>

3.zuul的application.yml文件

server:
  port: 8030
spring:
  application:
    name: getway
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
#zuul.routes.provider给服务提供者provider设置映射
zuul:
  routes:
    provider: /p/**

服务提供者的别名为 provider 映射为 localhost:8030/p/->localhost:8010/

4.启动类

@EnableZuulProxy   //网关的启动类,包含了@EnableZuulServer
@EnableAutoConfiguration //可以帮助SpringBoot应用将所有符合条件的@Configration配置加载到当前SpringBoot创建到IOC容器中
public class ZuulApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class);
    }
}
  1. Ribbon 负载均衡

Spring Cloud Ribbon是一个负载均衡解决方案,Ribbon是NetFlix发布的负载均衡器。Spring Cloud Ribbon是基于Netfilx Ribbon实现的,是一个对于http请求进行控制的负载均衡客户端

在注册中心对Ribbon进行注册之后,Ribbon就可以基于某种负载均衡算法,如轮询,随机,加权轮询,加权随机等自动帮助消费者调用接口,开发者也可以根据具体需求自定义Ribbon负载均衡算法,实际开发中,spring cloud Ribbon 需要结合Spring cloud eureka来使用,Eureka Server 提供所有可以调用的服务提供者列表,Ribbon基于特定的负载均衡算法从这些服务提供者选择要调用的具体实例

2.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>ygdSpringCloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ribbon</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>

3.application.yml配置文件

server:
  port: 8040
spring:
  application:
    name: ribbon
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

4.启动类,在RestTemplate Bean上加上@LoadBlanced轮询策略

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class RibbonApplication {
    public static void main(String[] args) {
        SpringApplication.run(RibbonApplication.class);
    }
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值