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

  • 相关阅读:
    通过 curl 命令访问 K8S API
    k8s 调度 Affinity
    golang 定期发送 RA 报文
    Ticker 使用
    查看 host/container veth pair 关系
    Kubernetes 服务 service 的负载均衡分析
    698 TypeScript泛型的使用:泛型实现类型参数化,泛型接口,泛型类,泛型约束
    697 TypeScript接口的使用:接口的声明,可选属性,只读属性,索引类型,函数类型,接口继承,交叉类型,接口的实现,字面量赋值,枚举类型
    696 TypeScript类的使用:类的定义,继承,多态,成员修饰符,readonly,getters/setters,静态成员,抽象类abstract,抽象类演练,类的类型
    695 TypeScript函数类型:可选参数,默认参数,剩余参数,this类型,函数的重载,联合类型和重载
  • 原文地址:https://www.cnblogs.com/smallfa/p/12782007.html
Copyright © 2011-2022 走看看