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://github.com/yangzhenlong/mySpringBootDemo/tree/master/springboot12-zuul

posted @ 2017-05-31 15:13  艺言弈行  阅读(3096)  评论(0编辑  收藏  举报