Skip to content

Commit

Permalink
验证网关转发
Browse files Browse the repository at this point in the history
  • Loading branch information
Yingbo Wang committed Oct 18, 2022
1 parent 725790d commit f86decd
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.wyb.rms.auth.center.controller;

import com.alibaba.fastjson.JSON;
import com.wyb.rms.auth.center.service.IJWTService;
import com.wyb.rms.common.vo.JwtToken;
import com.wyb.rms.common.vo.UsernameAndPassword;
import com.wyb.rms.mvc.annotation.IgnoreResponseAdvice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* <h1>对外暴露的授权服务接口</h1>
*/
@Slf4j
@RestController
@RequestMapping("/authority")
public class AuthorityController {

private final IJWTService ijwtService;

public AuthorityController(IJWTService ijwtService) {
this.ijwtService = ijwtService;
}

/**
* <h2>从授权中心获取 Token (其实就是登录功能), 且返回信息中没有统一响应的包装</h2>
*/
@IgnoreResponseAdvice
@PostMapping("/token")
public JwtToken token(@RequestBody UsernameAndPassword usernameAndPassword) throws Exception {

log.info("request to get token with param: [{}]", JSON.toJSONString(usernameAndPassword));
return new JwtToken(ijwtService.generateToken(usernameAndPassword.getUsername(), usernameAndPassword.getPassword()));
}

/**
* <h2>注册用户并返回当前注册用户的 Token, 即通过授权中心创建用户</h2>
*/
@IgnoreResponseAdvice
@PostMapping("/register")
public JwtToken register(@RequestBody UsernameAndPassword usernameAndPassword) throws Exception {

log.info("register user with param: [{}]", JSON.toJSONString(usernameAndPassword));
return new JwtToken(ijwtService.registerUserAndGenerateToken(usernameAndPassword));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class UserTest {
public void createUserRecord() {

UserDO userDO = new UserDO();
userDO.setUsername("ImoocQinyi@imooc.com");
userDO.setUsername("wangyb@qq.com");
userDO.setPassword(MD5.create().digestHex("12345678"));
userDO.setExtraInfo("{}");
log.info("save user: [{}]",
Expand Down
22 changes: 11 additions & 11 deletions rms-mall-gateway/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
server:
port: 9001
servlet:
context-path: /rms
context-path: /imooc

spring:
application:
Expand Down Expand Up @@ -57,16 +57,16 @@ spring:
# uri: 127.0.0.1:8080/user/{id} # 匹配后路由地址
# predicates: # 断言, 路径相匹配的进行路由
# - Path=/user/{id}
kafka:
bootstrap-servers: 127.0.0.1:9092
producer:
retries: 3
consumer:
auto-offset-reset: latest
zipkin:
sender:
type: kafka # 默认是 web
base-url: http://localhost:9411/
# kafka:
# bootstrap-servers: 127.0.0.1:9092
# producer:
# retries: 3
# consumer:
# auto-offset-reset: latest
# zipkin:
# sender:
# type: kafka # 默认是 web
# base-url: http://localhost:9411/
main:
allow-bean-definition-overriding: true # 因为将来会引入很多依赖, 难免有重名的 bean

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"apiName": "nacos-client-api",
"predicateItems": [
{
"pattern": "/imooc/ecommerce-nacos-client/nacos-client/project-config"
},
{
"pattern": "/imooc/ecommerce-nacos-client/**",
"matchStrategy": 1
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"resource": "e-commerce-nacos-client",
"resourceMode": 0,
"count": 3,
"intervalSec": 60
},
{
"resource": "nacos-client-api",
"resourceMode": 1,
"count": 1,
"intervalSec": 60
}
]
17 changes: 17 additions & 0 deletions rms-mall-gateway/src/main/resources/http/login.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### 登录
POST 127.0.0.1:9001/imooc/e-commerce/login
Content-Type: application/json

{
"username": "[email protected]",
"password": "25d55ad283aa400af464c76d713c07ad"
}

### 注册
POST 127.0.0.1:9001/imooc/e-commerce/register
Content-Type: application/json

{
"username": "[email protected]",
"password": "25d55ad283aa400af464c76d713c07ad"
}
5 changes: 5 additions & 0 deletions rms-mall-gateway/src/main/resources/http/nacos-client.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### 查询服务
GET http://127.0.0.1:9001/imooc/rms-mall-nacos-client/nacos-client/service-instance?serviceId=rms-mall-gateway
Accept: application/json
e-commerce-user: eyJhbGciOiJSUzI1NiJ9.eyJlLWNvbW1lcmNlLXVzZXIiOiJ7XCJpZFwiOjEwLFwidXNlcm5hbWVcIjpcIndhbmd5YkBxcS5jb21cIn0iLCJqdGkiOiJiYTU0MjBjNy0wMTM1LTRhZjYtOGVjYS0wZGUwZmQ5OTI5ZDgiLCJleHAiOjE2NjYxMDg4MDB9.eulJFnRt_I7ycLFhcZVi2wLk8HdWvnHgrXKqBm9j86StFnMl4D4fSFgWGOQoD1dwFgOMT00JhHb77hK1wsxQDMbymIj9iii5m21tNmdRfGY7FbwMh7Bd9M2s3_wkct7xHxcdyQ-z7T_0GgUYRJsT6zpTzBW5bloiltG_V1mqNCxRpvXo9HlztizZg69BaZuVSpoSee-w9g74_cvS9FsZyB9ztu19d6pz-OoByjBbWC0u4F_9TS8zCkWsGpGTReiKiP4MVPVHv44BqAUqJFLsLky7kPRx351tVQDuCNT5mXN0U-Pu4HEOtIV59C8GwvtXJooLAQtsJ5vccF_ax_WkMQ
token: imooc
17 changes: 17 additions & 0 deletions rms-mall-gateway/src/main/resources/http/nginx.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### 登录
POST http://www.ecommerce.com/imooc/e-commerce/login
Content-Type: application/json

{
"username": "[email protected]",
"password": "25d55ad283aa400af464c76d713c07ad"
}

### 查询服务 Nginx -> Gatyeway -> MicroService
GET http://www.ecommerce.com/imooc/ecommerce-nacos-client/nacos-client/service-instance?serviceId=e-commerce-gateway
Accept: application/json
e-commerce-user: eyJhbGciOiJSUzI1NiJ9.eyJlLWNvbW1lcmNlLXVzZXIiOiJ7XCJpZFwiOjEsXCJ1c2VybmFtZVwiOlwiUWlueWlAaW1vb2MuY29tXCJ9IiwianRpIjoiZTk3Yjg2NjEtMjczYy00ZGFkLThhNTEtNzFjNzBlNTI4MDI0IiwiZXhwIjoxNjMwODU3NjAwfQ.K2yehUtgZOQnHgUAKezuqdAVl9JVN0y_8xx1UiNXHP_oiHDmBYxUbdkBvjq3cvBQLF_UKjfuYaZ0zmdJq3QgHqLThIS35Bd9pewjlcxUmDvLaVPzrdj-Ov22lbT6GmPwqcC2b_Fx0TqvEMe_1QGAQjcLCNCS8Pg9xmahY1_nf-5hqiSezRJBlLBko7aa82TkfkGZjfC_7rP6ev6F22l3_Bi-FUf5WnXqxqao260nD-tH-hN5wJ-_tZ4B5y2pVgtGBH_ebbGFiBl2eE9Azmz2vwbEoPWbrQvKGPj6R1v-w8uX7m4okNatxe0eqc_O2S8UGjQ7IIsB7fqXJ8b37yJt5A
token: imooc

### SpringBoot Admin
http://www.ecommerce.com/e-commerce-admin
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public List<ServiceInstance> getServiceInstances(@RequestParam(defaultValue = "r
return nacosClientService.getServiceInstances(serviceId);
}

/**
* <h2>根据 service id 获取服务所有的实例信息</h2>
* */
@GetMapping("/service-instance")
public List<ServiceInstance> logNacosClientInfo(
@RequestParam(defaultValue = "rms-mall-nacos-client") String serviceId) {

log.info("coming in log nacos client info: [{}]", serviceId);
return nacosClientService.getNacosClientInfo(serviceId);
}
/**
* 动态获取配置文件
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.wyb.rms.nacos.client.service;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
* @author Marcher丶
Expand All @@ -28,4 +31,38 @@ public List<ServiceInstance> getServiceInstances(String serviceId) {
log.info("coming in get service instances: [{}]", serviceId);
return discoveryClient.getInstances(serviceId);
}


/**
* <h2>打印 Nacos Client 信息到日志中</h2>
* */
public List<ServiceInstance> getNacosClientInfo(String serviceId) {

// 测试 UseHystrixCommandAnnotation 的超时
// try {
// Thread.sleep(2000);
// } catch (InterruptedException ex) {
// //
// }

// 测试 NacosClientHystrixCommand 熔断
// throw new RuntimeException("has some error");

log.info("request nacos client to get service instance info: [{}]", serviceId);
return discoveryClient.getInstances(serviceId);
}

/**
* <h2>提供给编程方式的 Hystrix 请求合并</h2>
* */
public List<List<ServiceInstance>> getNacosClientInfos(List<String> serviceIds) {

log.info("request nacos client to get service instance infos: [{}]",
JSON.toJSONString(serviceIds));
List<List<ServiceInstance>> result = new ArrayList<>(serviceIds.size());

serviceIds.forEach(s -> result.add(discoveryClient.getInstances(s)));
return result;
}

}
2 changes: 2 additions & 0 deletions rms-mall-nacos-client/src/main/resources/bootstrap.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
server:
port: 8000
servlet:
context-path: /rms-mall-nacos-client
spring:
profiles:
active: dev
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
### 查询服务实例信息
GET http://127.0.0.1:8000/nacos-client/getServerList
GET http://127.0.0.1:8000/rms-mall-nacos-client/nacos-client/getServerList
Accept: application/json

### 动态从 Nacos Server 中获取配置信息
GET http://127.0.0.1:8000/nacos-client/getConfig
GET http://127.0.0.1:8000/rms-mall-nacos-client/nacos-client/getConfig
Accept: application/json

### 查看 Sleuth 跟踪信息
Expand Down

0 comments on commit f86decd

Please sign in to comment.