• Net6使用Yarp做网关


    1、启用nacos服务端,本例使用的是2.1版本

    在nacos创建yarp命名空间

    2、创建net5webapi服务

    2.1、ordersvc服务

    appsetting.json

    1. {
    2. "Logging": {
    3. "LogLevel": {
    4. "Default": "Warning",
    5. "Nacos.AspNetCore": "Information",
    6. "Microsoft.Hosting.Lifetime": "Information"
    7. }
    8. },
    9. "AllowedHosts": "*",
    10. "nacos": {
    11. "EndPoint": "",
    12. "ServerAddresses": [ "http://localhost:8848" ],
    13. "DefaultTimeOut": 15000,
    14. "Namespace": "yarp",
    15. "ListenInterval": 1000,
    16. "ServiceName": "ordersvc",
    17. "GroupName": "DEFAULT_GROUP",
    18. "ClusterName": "DEFAULT",
    19. "Ip": "",
    20. "PreferredNetworks": "",
    21. "Port": 0,
    22. "Weight": 100,
    23. "RegisterEnabled": true,
    24. "InstanceEnabled": true,
    25. "Ephemeral": true,
    26. "Secure": false,
    27. "AccessKey": "",
    28. "SecretKey": "",
    29. "UserName": "",
    30. "Password": "",
    31. "ConfigUseRpc": false,
    32. "NamingUseRpc": false,
    33. "NamingLoadCacheAtStart": ""
    34. }
    35. }

     代码

    1. using Nacos.AspNetCore.V2;
    2. var builder = WebApplication.CreateBuilder(args);
    3. // nacos server v1.x or v2.x
    4. builder.Services.AddNacosAspNet(builder.Configuration);
    5. builder.Services.AddControllers();
    6. var app = builder.Build();
    7. if (app.Environment.IsDevelopment())
    8. {
    9. app.UseDeveloperExceptionPage();
    10. }
    11. app.UseRouting();
    12. app.UseEndpoints(endpoints =>
    13. {
    14. endpoints.MapGet("/", () =>
    15. {
    16. return Results.Ok("order svc 9001");
    17. });
    18. });
    19. app.Run("http://*:9001");

    2.2 usersvc服务

    appsetting.json

    1. {
    2. "Logging": {
    3. "LogLevel": {
    4. "Default": "Warning",
    5. "Nacos.AspNetCore": "Information",
    6. "Microsoft.Hosting.Lifetime": "Information"
    7. }
    8. },
    9. "AllowedHosts": "*",
    10. "nacos": {
    11. "EndPoint": "",
    12. "ServerAddresses": [ "http://localhost:8848" ],
    13. "DefaultTimeOut": 15000,
    14. "Namespace": "yarp",
    15. "ListenInterval": 1000,
    16. "ServiceName": "usersvc",
    17. "GroupName": "DEFAULT_GROUP",
    18. "ClusterName": "DEFAULT",
    19. "Ip": "",
    20. "PreferredNetworks": "",
    21. "Port": 0,
    22. "Weight": 100,
    23. "RegisterEnabled": true,
    24. "InstanceEnabled": true,
    25. "Ephemeral": true,
    26. "Secure": false,
    27. "AccessKey": "",
    28. "SecretKey": "",
    29. "UserName": "",
    30. "Password": "",
    31. "ConfigUseRpc": false,
    32. "NamingUseRpc": false,
    33. "NamingLoadCacheAtStart": ""
    34. }
    35. }

    代码

    1. using Nacos.AspNetCore.V2;
    2. var builder = WebApplication.CreateBuilder(args);
    3. // nacos server v1.x or v2.x
    4. builder.Services.AddNacosAspNet(builder.Configuration);
    5. builder.Services.AddControllers();
    6. var app = builder.Build();
    7. if (app.Environment.IsDevelopment())
    8. {
    9. app.UseDeveloperExceptionPage();
    10. }
    11. app.UseRouting();
    12. app.UseEndpoints(endpoints =>
    13. {
    14. endpoints.MapGet("/", () =>
    15. {
    16. return Results.Ok("user svc 9002");
    17. });
    18. });
    19. app.Run("http://*:9002");

    2.3、yarp网关,也可以把网关注册到服务中心统一管理,下图为无注册到服务中心情况

    1. using global::Nacos.V2.DependencyInjection;
    2. using Yarp.ReverseProxy.Configuration;
    3. var builder = WebApplication.CreateBuilder(args);
    4. builder.Services.AddNacosV2Naming(x =>
    5. {
    6. x.ServerAddresses = new System.Collections.Generic.List<string> { "http://localhost:8848/" };
    7. x.Namespace = "yarp";
    8. // swich to use http or rpc
    9. x.NamingUseRpc = false;
    10. });
    11. builder.Services.AddReverseProxy()
    12. .AddNacosServiceDiscovery(
    13. groupNames: "DEFAULT_GROUP",
    14. percount:100,
    15. enableAutoRefreshService: true,
    16. autoRefreshPeriod: 30);
    17. var app = builder.Build();
    18. app.UseRouting();
    19. app.UseEndpoints(endpoints =>
    20. {
    21. endpoints.MapGet("/yarp", (IProxyConfigProvider provider) =>
    22. {
    23. var res = provider.GetConfig();
    24. return Results.Ok(res);
    25. });
    26. endpoints.MapReverseProxy();
    27. });
    28. app.Run("http://*:9099");

    3、启动服务和网关,在nacos服务中心查看

     4、查看服务详情并测试

    4.1访问 http://localhost:9099/yarp  服务详细如下

    1. {
    2. "routes": [{
    3. "routeId": "DEFAULT_GROUP@@usersvc-route",
    4. "match": {
    5. "methods": null,
    6. "hosts": null,
    7. "path": "/usersvc/{**catch-all}",
    8. "queryParameters": null,
    9. "headers": null
    10. },
    11. "order": null,
    12. "clusterId": "DEFAULT_GROUP@@usersvc",
    13. "authorizationPolicy": null,
    14. "corsPolicy": null,
    15. "metadata": null,
    16. "transforms": [{
    17. "PathRemovePrefix": "/usersvc"
    18. }]
    19. }, {
    20. "routeId": "DEFAULT_GROUP@@ordersvc-route",
    21. "match": {
    22. "methods": null,
    23. "hosts": null,
    24. "path": "/ordersvc/{**catch-all}",
    25. "queryParameters": null,
    26. "headers": null
    27. },
    28. "order": null,
    29. "clusterId": "DEFAULT_GROUP@@ordersvc",
    30. "authorizationPolicy": null,
    31. "corsPolicy": null,
    32. "metadata": null,
    33. "transforms": [{
    34. "PathRemovePrefix": "/ordersvc"
    35. }]
    36. }],
    37. "clusters": [{
    38. "clusterId": "DEFAULT_GROUP@@usersvc",
    39. "loadBalancingPolicy": "RoundRobin",
    40. "sessionAffinity": null,
    41. "healthCheck": null,
    42. "httpClient": null,
    43. "httpRequest": null,
    44. "destinations": {
    45. "http://10.15.69.27:9002": {
    46. "address": "http://10.15.x.x:9002",
    47. "health": null,
    48. "metadata": {}
    49. }
    50. },
    51. "metadata": null
    52. }, {
    53. "clusterId": "DEFAULT_GROUP@@ordersvc",
    54. "loadBalancingPolicy": "RoundRobin",
    55. "sessionAffinity": null,
    56. "healthCheck": null,
    57. "httpClient": null,
    58. "httpRequest": null,
    59. "destinations": {
    60. "http://10.15.69.27:9001": {
    61. "address": "http://10.15.x.x:9001",
    62. "health": null,
    63. "metadata": {}
    64. }
    65. },
    66. "metadata": null
    67. }],
    68. "changeToken": {
    69. "hasChanged": false,
    70. "activeChangeCallbacks": true
    71. }
    72. }

    4.2 通过网关访问访问服务

     

    Yarp官网:YARP Documentation 

  • 相关阅读:
    Spring修炼之路(5)整合MyBatis和事务
    3D MINS 多模态影像导航系统
    数据库技术基础--数据模型
    L1-002 打印沙漏分数 20
    二、MyBatis 框架 XML 标签总结
    LCD1602液晶显示屏介绍和程序开发
    Playwright 简明入门教程:录制自动化测试用例,结合 Docker 使用
    JAVA计算机毕业设计自由教学平台Mybatis+源码+数据库+lw文档+系统+调试部署
    Oracle 清空/删除数据库全部的表
    数据结构之散列表
  • 原文地址:https://blog.csdn.net/lenkty/article/details/126679533