Spring Cloud服务网关

版本信息

Spring Cloud : Hoxton.SR1

Spring Boot : 2.2.2.RELEASE

Zookeeper : 3.5.6 (注册中心使用)

服务网关特性

服务网关是干什么用的?

  • 认证
  • 安全(授权)
  • 动态路由

基于RestTemplate自定义服务网关

服务端演示提供服务
1.添加pom依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!--zookeeper 客户端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>

2.服务网关服务应用GatewayServerApplication

/**
 * 服务网关服务应用
 *
 * @author FelixFly <chenglinxu@yeah.net>
 * @date 2020/2/8
 */
@SpringBootApplication
public class GatewayServerApplication {
   

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

3.演示服务端点

/**
 * 演示服务端点
 *
 * @author FelixFly <chenglinxu@yeah.net>
 * @date 2020/2/8
 */
@RestController
public class EchoController {
   

    @Autowired
    private Environment environment;


    @GetMapping("/echo")
    public String echo(String message) {
   
        // 由于采用的是随机端口,这地方必须采用这个方式获取端口
        String port = environment.getProperty("local.server.port");
        return "ECHO(" + port + "):" + message;
    }
}

4.服务配置application.yml

spring:
  application:
    name: gateway-server
  cloud:
    zookeeper:
      connect-string: 127.0.0.1:2181
server:
  port: 0

启动服务,根据启动日志查看本地的随机端口,此次端口是54692

http://127.0.0.1:54692/echo?message=Hello 返回信息ECHO(54692):Hello

自定义服务网关

基于Servlet

配置文件application.yml

spring:
  application:
    name: gateway-zuul
  cloud:
    zookeeper:
      connect-string: 127.0.0.1:2181
server:
  port: 7070

基于DiscoveryClient
GatewayCustomServlet网关Servlet

包名:top.felixfly.cloud.gateway.custom

/**
 * 服务网关Servlet实现
 *
 * @author FelixFly <chenglinxu@yeah.net>
 * @date 2020/2/8
 */
@WebServlet(name = "gateway", urlPatterns = "/gateway/*")
public class GatewayCustomServlet extends HttpServlet {
   

    @Autowired
    private DiscoveryClient discoveryClient;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
        // 访问地址的Uri:/{service-name}/${service-uri}
        String pathInfo = req.getPathInfo();
        String[] paths = StringUtils.split(pathInfo.substring(1), "/");
        // 服务名称
        String serverName = paths[0];
        // 服务访问地址
        String serverURI = paths[1];
        // 获取服务实例
        ServiceInstance serviceInstance = choose(serverName);
        // 目标地址
        String targetURL = createTargetURL(serviceInstance, serverURI, req);
        RestTemplate restTemplate = new RestTemplate();
        String method = req.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值