您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

.netcore微服务使用rpc实现微服务间的调用

wiki 2022/11/8 17:59:26 dotnet 字数 2426 阅读 2041

.netcore微服务使用rpc实现微服务间的调用

1.微服务A,微服务B 相互之间调用

引用dll:

<!---客户端-->

<PackageReference Include="DotNetCoreRpc.Client" Version="1.0.2" />

<!---服务端-->

<PackageReference Include="DotNetCoreRpc.Server" Version="1.0.2" />


2.先假设A调用B

B为服务端:Startup.cs设置


 public class Startup
    {
        public Startup(IConfiguration configuration, IWebHostEnvironment env)
        {
            Configuration = configuration;
            Env = env;
        }

        /// <summary>
        /// 
        /// </summary>
        public IConfiguration Configuration { get; }

        /// <summary>
        /// 
        /// </summary>
        public IWebHostEnvironment Env { get; }

        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container.
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
           
            GlobalContext.Configuration = Configuration;
            services.AddControllersWithViews().AddControllersAsServices();
            services.AddNacosAspNet(Configuration, "nacos");
        
           
            #region RpcServer

            services.AddSingleton<IParmaterService, ParmaterService>();
            services.AddDotNetCoreRpcServer(options => {
                //*确保添加的契约服务接口事先已经被注册到DI容器中

                //添加契约接口
                //options.AddService<IPersonService>();

                //或添加契约接口以xxx为结尾的
                //options.AddService("*Service");

                //或添加具体名称为xxx的契约接口
                //options.AddService("IService");

                //或扫描具体命名空间下的契约接口
                options.AddNameSpace("Test.XXX.IService");

                //可以添加全局过滤器,实现方式和CacheFilterAttribute一致
                // options.AddFilter<LoggerFilterAttribute>();
            });
            #endregion
        }
 ////// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /////////public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {  
            //添加DotNetCoreRpc中间件既可以
            app.UseDotNetCoreRpc();
 }
 
        

       
    }
}



如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶