Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

两个服务之间该怎么调用呢? #39

Closed
daofenglg opened this issue Jan 5, 2018 · 10 comments
Closed

两个服务之间该怎么调用呢? #39

daofenglg opened this issue Jan 5, 2018 · 10 comments

Comments

@daofenglg
Copy link

daofenglg commented Jan 5, 2018

var result = this.GetService().GetMembers(10).Result; 用这个调用本服务是可以的,但是如何调用其他服务的接口呢?
例如我有两个服务一个订单服务,一个库存服务,我要在订单的服务中调用库存的服务,订单服务是不是需要引用订单服务中定义的接口库?

@fanliang11
Copy link
Owner

this.GetService<IUser2Service>().GetUserName(123);

如果是远程调用就引用接口,如果是本地调用就要引用模块的领域服务和接口了。订单服务还需要添加
option.AddRelateService();//添加支持服务代理 ,具体可以看看 #37

@daofenglg
Copy link
Author

代码实现:
` var serviceProxyFactory=this.GetService();

        var userProxy = serviceProxyFactory.CreateProxy<IFactService>();
         Task task= userProxy.GetFacts(10);

`
接口已经引用到项目,在调用时引发了异常:
System.InvalidCastException:“Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.QualifiedNameSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.GenericNameSyntax'.”

@fanliang11
Copy link
Owner

this.GetService<IUser2Service>().GetUserName(123);

这个方法就可以了,不需要创建代理,会自动判断是否远程调用

@daofenglg
Copy link
Author

  1. var result = this.GetService().GetMembers(10).Result; 是本地的接口是可以调用的
  2. this.GetService().GetFacts(10);这个是需要远程调用的接口,还是报上面那个异常。
    以下是远程调用接口的实现:

[ServiceBundle("api/{Service}")]
public interface IFactService:IServiceKey
{
[Command(ExecutionTimeoutInMilliseconds = 1000*5, RequestCacheEnabled = false)]
[Service(Date = "201-01-03", Director = "ligang", Name = "获取品牌数据")]
Task GetFacts(int count);

    [Service(Date = "2018-01-03", Director = "ligang", Name = "测试方法")]
    string GetTst();
}

@fanliang11
Copy link
Owner

option.AddRelateService();添加了吗

@daofenglg
Copy link
Author

class Program
{
    static void Main(string[] args)
    {
        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        var host = new ServiceHostBuilder()
          .RegisterServices(builder =>
          {
              builder.AddMicroService(option =>
              {
                  option.AddServiceRuntime();
                  option.AddRelateService(); ;//添加支持服务代理
                  //option.UseZooKeeperManager(new ConfigInfo("127.0.0.1:2181"));
                  option.UseConsulManager(new ConfigInfo("192.168.1.199:8500"));
                  option.UseDotNettyTransport();
                  option.UseRabbitMQTransport();
                  option.AddRabbitMQAdapt();
                  //option.UseProtoBufferCodec();
                  option.UseMessagePackCodec();
                  builder.Register(p => new CPlatformContainer(ServiceLocator.Current));
              });
          })
          .SubscribeAt()
          .UseLog4net("Configs/log4net.config")
          //.UseServer("127.0.0.1", 98)
          //.UseServer("127.0.0.1", 98,“true”) //自动生成Token
          //.UseServer("127.0.0.1", 98,“123456789”) //固定密码Token
          .UseServer(options =>
          {
              options.Ip = "127.0.0.1";
              options.Port = 11011;
              options.Token = "True";
              options.ExecutionTimeoutInMilliseconds = 30000;
              options.MaxConcurrentRequests = 200;
          })
          .UseStartup<Startup>()
          .Build();
        using (host.Run())
        {
            Console.WriteLine($"服务端启动成功,{DateTime.Now}。");
        }
    }
}

@fanliang11
Copy link
Owner

错误信息贴出来

@daofenglg
Copy link
Author

qq 20180105164900

@fanliang11
Copy link
Owner

不支持直接返回string,请用Task

@daofenglg
Copy link
Author

刚才那个问题解决了,我接口的返回都改成了task,但是又有一个新的问题

qq 2018010516490011

下面是接口的声明
[ServiceBundle("api/{Service}")]
public interface IFactService:IServiceKey
{
[Command(ExecutionTimeoutInMilliseconds = 1000 * 5, RequestCacheEnabled = false)]
[Service(Date = "201-01-03", Director = "ligang", Name = "获取品牌数据")]
Task<List> GetFacts(int count);

    //[Service(Date = "2018-01-03", Director = "ligang", Name = "测试方法")]
    //Task<List<string>> GetTst();
}

下面是通过引用查看看到的临时文件内容
[global::Surging.Core.CPlatform.Runtime.Server.Implementation.ServiceDiscovery.Attributes.ServiceBundleAttribute("api/{Service}")]
public interface IFactService : global::Surging.Core.CPlatform.Ioc.IServiceKey
{
[global::Surging.Core.CPlatform.Support.Attributes.CommandAttribute(ExecutionTimeoutInMilliseconds = 5000, RequestCacheEnabled = false)]
[global::Surging.Core.CPlatform.Runtime.Server.Implementation.ServiceDiscovery.Attributes.ServiceAttribute(Date = "201-01-03", Director = "ligang", Name = "获取品牌数据")]
System.Threading.Tasks.Task<System.Collections.Generic.List<Api.Models.TblFactoryModel>> GetFacts(int count);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants