在Guns网关服务的Rest接口中通过Dubbo调用用户服务的登录方法

项目介绍:

用户服务: 服务提供方,提供了如登录方法。

网关服务: 提供Rest接口,如授权接口,然后在通过Dubbo调用用户服务的登录方法。

1、项目结构如下如

guns-api: 公共接口

guns-gateway: 网关服务 (从guns-rest复制过来,依赖于guns-api)

guns-user: 用户服务(从guns-rest复制过来,依赖于guns-api)

2、公共接口guns-api

定义了一个接口,里面有一个登陆方法

public interface UserAPI {
    boolean login(String username, String password);
}

  

3、 用户服务guns-user

1) 部分配置参数如下

rest:
  auth-open: false #jwt鉴权机制是否开启(true或者false)
  sign-open: false #签名机制是否开启(true或false)

server:
  port: 8083 #项目端口

spring:
  application:
    name: film-user
  dubbo:
    server: true
    registry: zookeeper://localhost:2181
    protocol:
      name: dubbo
      port: 20881
 

  

2) 创建服务提供者

package com.stylefeng.guns.rest.modular.user;

import com.alibaba.dubbo.config.annotation.Service;
import com.stylefeng.guns.api.UserAPI;
import org.springframework.stereotype.Component;

@Component
@Service(interfaceClass = UserAPI.class)
public class UserImpl  implements  UserAPI{
    @Override
    public boolean login(String username, String password) {
        System.out.println("this is user service. username=" + username + ", password=" +password);
        return true;
    }
}

  

4、 网关服务guns-gateway

1) 部分配置如下

server:
  port: 8080 #项目端口

spring:
  application:
    name: film-gateway
  dubbo:
    server: true
    registry: zookeeper://localhost:2181
 

  

2)在AuthController层增加如下图红框的方法。

实现网关调用用户服务

5、测试

1) 启动用户服务

2) 启动网关服务

3) 调用网关服务的接口 http://localhost:8080/auth?userName=admin&password=admin

4) 用户服务打印结果: this is user service. username=admin, password=admin

原文地址:https://www.cnblogs.com/linlf03/p/12842819.html