Browse Source

Refactor.

master
yangxiaodong 7 years ago
parent
commit
ea2cf5f95d
10 changed files with 42 additions and 37 deletions
  1. +10
    -12
      README.zh-cn.md
  2. +2
    -2
      build/common.props
  3. +2
    -2
      samples/Sample.Kafka/Controllers/ValuesController.cs
  4. +10
    -4
      src/DotNetCore.CAP.Kafka/CapSubscribeAttribute.cs
  5. +12
    -0
      src/DotNetCore.CAP.RabbitMQ/CapSubscribeAttribute.cs
  6. +0
    -11
      src/DotNetCore.CAP.RabbitMQ/RabbitMQTopicAttribute.cs
  7. +2
    -2
      src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs
  8. +1
    -1
      src/DotNetCore.CAP/ICapSubscribe.cs
  9. +2
    -2
      src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs
  10. +1
    -1
      test/DotNetCore.CAP.Test/ConsumerServiceSelectorTest.cs

+ 10
- 12
README.zh-cn.md View File

@@ -16,32 +16,32 @@ CAP

这是CAP集在ASP.NET Core 微服务架构中的一个示意图:

![](http://images2015.cnblogs.com/blog/250417/201706/250417-20170630143600289-1065294295.png)
![](http://images2015.cnblogs.com/blog/250417/201707/250417-20170705175827128-1203291469.png)

> 图中实线部分代表用户代码,虚线部分代表CAP内部实现。

## Getting Started

### NuGet 暂未发布
### NuGet

你可以运行以下下命令在你的项目中安装 CAP。

如果你的消息队列使用的是 Kafka 的话,你可以:

```
PM> Install-Package DotNetCore.CAP.Kafka
PM> Install-Package DotNetCore.CAP.Kafka -Pre
```

如果你的消息队列使用的是 RabbitMQ 的话,你可以:

```
PM> Install-Package DotNetCore.CAP.RabbitMQ
PM> Install-Package DotNetCore.CAP.RabbitMQ -Pre
```

CAP 默认提供了 Entity Framwork 作为数据库存储:

```
PM> Install-Package DotNetCore.CAP.EntityFrameworkCore
PM> Install-Package DotNetCore.CAP.EntityFrameworkCore -Pre
```

### Configuration
@@ -57,7 +57,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddCap()
.AddEntityFrameworkStores<AppDbContext>()
.AddKafka(x => x.Servers = "localhost:9453");
.AddKafka(x => x.Servers = "localhost:9092");
}

public void Configure(IApplicationBuilder app)
@@ -100,9 +100,7 @@ public class PublishController : Controller

**Action Method**

在Action上添加 Attribute 来订阅相关消息。

如果你使用的是 Kafak 则使用 `[KafkaTopic()]`, 如果是 RabbitMQ 则使用 `[RabbitMQTopic()]`
在 Action 上添加 CapSubscribeAttribute 来订阅相关消息。

```cs
public class PublishController : Controller
@@ -116,7 +114,7 @@ public class PublishController : Controller


[NoAction]
[KafkaTopic("xxx.services.account.check")]
[CapSubscribe("xxx.services.account.check")]
public async Task CheckReceivedMessage(Person person)
{
Console.WriteLine(person.Name);
@@ -129,7 +127,7 @@ public class PublishController : Controller

**Service Method**

如果你的订阅方法没有位于 Controller 中,则你订阅的类需要继承 `IConsumerService`:
如果你的订阅方法没有位于 Controller 中,则你订阅的类需要继承 `ICapSubscribe`:

```cs

@@ -141,7 +139,7 @@ namespace xxx.Service
}


public class SubscriberService: ISubscriberService, IConsumerService
public class SubscriberService: ISubscriberService, ICapSubscribe
{
[KafkaTopic("xxx.services.account.check")]
public void CheckReceivedMessage(Person person)


+ 2
- 2
build/common.props View File

@@ -8,12 +8,12 @@

<PropertyGroup Label="Package">
<Product>CAP</Product>
<Authors>Savorboard;dotnetcore</Authors>
<Authors>savorboard;dotnetcore</Authors>
<RepositoryUrl>https://github.com/dotnetcore/CAP</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageIconUrl>https://avatars2.githubusercontent.com/u/19404084</PackageIconUrl>
<PackageProjectUrl>https://github.com/dotnetcore/CAP</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/dotnetcore/CAP/blob/master/LICENSE</PackageLicenseUrl>
<PackageLicenseUrl>https://github.com/dotnetcore/CAP/blob/master/LICENSE.txt</PackageLicenseUrl>
<PackageTags>aspnetcore;cap;consistency</PackageTags>
<Description>Eventually consistency in distributed architectures.</Description>
</PropertyGroup>


+ 2
- 2
samples/Sample.Kafka/Controllers/ValuesController.cs View File

@@ -8,7 +8,7 @@ using Microsoft.AspNetCore.Mvc;
namespace Sample.Kafka.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller, IConsumerService
public class ValuesController : Controller, ICapSubscribe
{
private readonly ICapProducerService _producer;

@@ -24,7 +24,7 @@ namespace Sample.Kafka.Controllers
}
public string ServerPath => ((IHostingEnvironment)HttpContext.RequestServices.GetService(typeof(IHostingEnvironment))).ContentRootPath;

[KafkaTopic("zzwl.topic.finace.callBack", Group = "test")]
[CapSubscribe("zzwl.topic.finace.callBack", Group = "test")]
public void KafkaTest(Person person)
{
Console.WriteLine(person.Name);


src/DotNetCore.CAP.Kafka/KafkaTopicAttribute.cs → src/DotNetCore.CAP.Kafka/CapSubscribeAttribute.cs View File

@@ -2,15 +2,21 @@

namespace DotNetCore.CAP.Kafka
{
public class KafkaTopicAttribute : TopicAttribute
public class CapSubscribeAttribute : TopicAttribute
{
public KafkaTopicAttribute(string topicName)
public CapSubscribeAttribute(string topicName)
: this(topicName, 0) { }

public KafkaTopicAttribute(string topicName, int partition)
/// <summary>
/// Not support
/// </summary>
public CapSubscribeAttribute(string topicName, int partition)
: this(topicName, partition, 0) { }

public KafkaTopicAttribute(string topicName, int partition, long offset)
/// <summary>
/// Not support
/// </summary>
public CapSubscribeAttribute(string topicName, int partition, long offset)
: base(topicName)
{
Offset = offset;

+ 12
- 0
src/DotNetCore.CAP.RabbitMQ/CapSubscribeAttribute.cs View File

@@ -0,0 +1,12 @@
using DotNetCore.CAP.Abstractions;

namespace DotNetCore.CAP.RabbitMQ
{
public class CapSubscribeAttribute : TopicAttribute
{
public CapSubscribeAttribute(string routingKey) : base(routingKey)
{

}
}
}

+ 0
- 11
src/DotNetCore.CAP.RabbitMQ/RabbitMQTopicAttribute.cs View File

@@ -1,11 +0,0 @@
using DotNetCore.CAP.Abstractions;

namespace DotNetCore.CAP.RabbitMQ
{
public class RabbitMQTopicAttribute : TopicAttribute
{
public RabbitMQTopicAttribute(string routingKey) : base(routingKey)
{
}
}
}

+ 2
- 2
src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs View File

@@ -67,9 +67,9 @@ namespace Microsoft.Extensions.DependencyInjection
foreach (var rejectedServices in services)
{
if (rejectedServices.ImplementationType != null
&& typeof(IConsumerService).IsAssignableFrom(rejectedServices.ImplementationType))
&& typeof(ICapSubscribe).IsAssignableFrom(rejectedServices.ImplementationType))

consumerListenerServices.Add(typeof(IConsumerService), rejectedServices.ImplementationType);
consumerListenerServices.Add(typeof(ICapSubscribe), rejectedServices.ImplementationType);
}

foreach (var service in consumerListenerServices)


src/DotNetCore.CAP/IConsumerService.cs → src/DotNetCore.CAP/ICapSubscribe.cs View File

@@ -3,7 +3,7 @@
/// <summary>
/// An empty interface, which is used to mark the current class have a CAP methods.
/// </summary>
public interface IConsumerService
public interface ICapSubscribe
{
}
}

+ 2
- 2
src/DotNetCore.CAP/Internal/IConsumerServiceSelector.Default.cs View File

@@ -54,11 +54,11 @@ namespace DotNetCore.CAP.Internal
{
var executorDescriptorList = new List<ConsumerExecutorDescriptor>();

var consumerServices = provider.GetServices<IConsumerService>();
var consumerServices = provider.GetServices<ICapSubscribe>();
foreach (var service in consumerServices)
{
var typeInfo = service.GetType().GetTypeInfo();
if (!typeof(IConsumerService).GetTypeInfo().IsAssignableFrom(typeInfo))
if (!typeof(ICapSubscribe).GetTypeInfo().IsAssignableFrom(typeInfo))
{
continue;
}


+ 1
- 1
test/DotNetCore.CAP.Test/ConsumerServiceSelectorTest.cs View File

@@ -58,7 +58,7 @@ namespace DotNetCore.CAP.Test
public interface IFooTest { }
public interface IBarTest { }

public class CandidatesFooTest : IFooTest, IConsumerService
public class CandidatesFooTest : IFooTest, ICapSubscribe
{
[CandidatesTopic("Candidates.Foo")]
public Task GetFoo()


Loading…
Cancel
Save