SpringCloud-Zuul

路由网关---zuul

我们知道我们要进入一个服务本身,很明显我们没有特别好的办法,直接输入IP地址+端口号,我们知道这样的做法很糟糕的,这样的做法大有问题,首先暴露了我们实体机器的IP地址,别人一看你的IP地址就知道服务部署在哪里,让别人很方便的进行攻击操作。

某个路径,你输入该路径,它匹配到了,它就去替你访问这个服务,它会有个请求转发的过程,像Nginx一样,服务机器的具体实例,它不会直接去访问IP,它会去Eureka注册中心拿到服务的实例ID,即服务的名字。我再次使用客户端的负载均衡ribbon访问其中服务实例中的一台。

 

路由项目实例

  1 新建 eureka

     1.1 pom.xml

          

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

     1.2 application.properties

     

  • server.port=8088
    eureka.instance.hostname=127.0.0.1
    #不要向注册中心注册自己
    eureka.client.register-with-eureka=false
    #禁止检索服务
    eureka.client.fetch-registry=false
    eureka.client.service-url.defaultZone=http://127.0.0.1:8088/eureka
    spring.application.name=eurekserver
    
    eureka.server.enable-self-preservation=false

     1.3启动类

      

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class DemoApplication {

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

}

  2新建 eureka client

     2.1 pom.xml

               

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.1.6.RELEASE</version>
</dependency>

     2.2 application.properties

     

server.port=8111

#设置应用的名称

spring.application.name=eurekaClient1  


#服务注册的Eureka Server地址

eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8088/eureka
##http://shumeigang:123456@127.0.0.1:8081/eureka
eureka.instance.prefer-ip-address=true
#设置注册ip
#表示是否将自己注册在EurekaServer上,默认为true。由于当前应用就是EurekaServer,所以置为false
eureka.client.register-with-eureka=true
#表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据
eureka.client.fetch-registry=true

     2.3 Controller

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @GetMapping("/User/Name")
    public String UserName(){
        return "eurekaClient1:小明";
    }

}

     2.4启动类

package com.example.demo;

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

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {

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

}

  3 创新Zuuldemo

     3.1 pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

     3.2 application.properties

server.port=8020

#设置应用的名称

spring.application.name=Zuuldemo2
#eurekaClient1 接口项目名称
zuul.routes.eurekaClient1.path=/api/**
#接口地址
zuul.routes.eurekaClient1.url=http://127.0.0.1:8111/User/Name

zuul:
semaphore:
max-semaphores: 200

#服务注册的Eureka Server地址

eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8088/eureka
##http://shumeigang:123456@127.0.0.1:8081/eureka
eureka.instance.prefer-ip-address=true
#设置注册ip
#表示是否将自己注册在EurekaServer上,默认为true。由于当前应用就是EurekaServer,所以置为false
eureka.client.register-with-eureka=true
#表示表示是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据
eureka.client.fetch-registry=true

     3.3启动类

        

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class DemoApplication {

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

}

4运行

 第一步 运行eureka

 第二步 运行 eureka Client

 第三步 运行 Zuul

运行结果

运行eureka结果

运行 eureka Client

运行 Zuul http://127.0.0.1:8020/api

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值