zoukankan      html  css  js  c++  java
  • springboot12-zuul

    Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门,提供动态路由,监控,弹性,安全等的边缘服务

    所有请求都经过网关(API Gateway)zuul,然后转发到各个子服务上去

    1.注册中心eureka

    <!--eureka-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
            </dependency>
    复制代码
    @EnableEurekaServer//注解启动一个服务注册中心
    @SpringBootApplication
    public class EurekaApp {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaApp.class, args);
        }
    }
    复制代码
    复制代码
    #application配置
    server.port=8888
    
    #在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false
    
    #服务注册地址
    eureka.instance.ip-address=localhost
    eureka.instance.prefer-ip-address=true
    eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
    复制代码

    启动app,访问http://localhost:8888/

    2.service1

    复制代码
    @EnableDiscoveryClient//激活Eureka中的DiscoveryClient实现
    @SpringBootApplication
    public class Service1App {
    
        public static void main(String[] args) {
            SpringApplication.run(Service1App.class, args);
        }
    }
    复制代码
    复制代码
    server.port=1111
    server.address=localhost
    spring.application.name=service1
    
    #注册中心地址
    eureka.instance.ip-address=localhost
    eureka.instance.prefer-ip-address=true
    eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
    复制代码
    复制代码
    public class UserController {
    
        private final static Logger LOGGER = LoggerFactory.getLogger(User2Controller.class);
        @Autowired
        private DiscoveryClient discoveryClient;
    
        @RequestMapping("/index")
        public String index(HttpServletRequest request){
            return "service1 被调用, 请求地址:" + request.getRequestURI();
        }
    
    }
    复制代码

    3.service2

    复制代码
    @EnableDiscoveryClient//激活Eureka中的DiscoveryClient实现
    @SpringBootApplication
    public class Service2App {
    
        public static void main(String[] args) {
            SpringApplication.run(Service2App.class, args);
        }
    }
    复制代码
    复制代码
    server.port=2222
    server.address=localhost
    spring.application.name=service2
    
    #注册中心地址
    eureka.instance.ip-address=localhost
    eureka.instance.prefer-ip-address=true
    eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
    复制代码
    复制代码
    public class User2Controller {
    
        private final static Logger LOGGER = LoggerFactory.getLogger(User2Controller.class);
        @Autowired
        private DiscoveryClient discoveryClient;
    
        @RequestMapping("/index")
        public String index(HttpServletRequest request){
            return "service2 被调用, 请求地址:" + request.getRequestURI();
        }
    
    }
    复制代码

     4.zuul网关

    复制代码
    <!--eureka-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zuul</artifactId>
            </dependency>
    复制代码
    复制代码
    @EnableZuulProxy//声明zuul代理
    @SpringBootApplication
    public class ZuulMainApp {
    
        public static void main(String[] args) {
            SpringApplication.run(ZuulMainApp.class, args);
        }
    }
    复制代码
    复制代码
    #注册中心地址
    eureka.instance.ip-address=localhost
    eureka.instance.prefer-ip-address=true
    eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
    
    #路由配置
    #url形式
    zuul.routes.baidu.path=/baidu/**
    zuul.routes.baidu.url=http://www.baidu.com
    #注册服务
    zuul.routes.service1.path=/service1/**
    zuul.routes.service1.serviceId=service1
    zuul.routes.service2.path=/service2/**
    zuul.routes.service2.serviceId=service2
    复制代码

    测试:启动sevice1,service2,zuul

    查看注册中心:

    通过网关+路由名称访问:

    http://localhost:8080/baidu

    http://localhost:8080/service1/index

    http://localhost:8080/service2/index

    https://www.cnblogs.com/yangzhenlong/p/6924324.html

    项目地址:https://github.com/yangzhenlong/mySpringBootDemo/tree/master/springboot12-zuul

    https://www.cnblogs.com/yangzhenlong/p/6924324.html

  • 相关阅读:
    数据绑定表达式语法(Eval,Bind区别)
    使用博客园的第一件事 自定义主题
    sql2000 跨服务器复制表数据
    使用UpdatePanel 局部刷新出现中文乱码的解决方法!!
    MMC不能打开文件MSC文件
    sql 日期 、时间相关
    loaded AS2 swf call function in AS3 holder
    Rewrite the master page form action attribute in asp.net 2.0
    100万个不重复的8位的随机数
    flash 中实现斜切变型
  • 原文地址:https://www.cnblogs.com/smallfa/p/12782007.html
Copyright © 2011-2022 走看看