Skip to content

Commit

Permalink
bump version to 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
caochaoping committed Oct 9, 2016
0 parents commit 83390a4
Show file tree
Hide file tree
Showing 80 changed files with 1,756 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
/gemfiles/*.lock
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
47 changes: 47 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require: rubocop-rspec

AllCops:
Exclude:
- 'spec/dummy/db/**/*'

Metrics/AbcSize:
Enabled: false

Metrics/CyclomaticComplexity:
Enabled: false

Metrics/ClassLength:
Max: 160

Metrics/LineLength:
Max: 160

Metrics/MethodLength:
Max: 40

Metrics/PerceivedComplexity:
Max: 12

Style/Documentation:
Enabled: false

Style/FrozenStringLiteralComment:
Enabled: false

Style/GlobalVars:
AllowedVariables: ['$user_service_publisher']

Style/ParallelAssignment:
Enabled: false

Style/RedundantReturn:
Enabled: false

RSpec/NamedSubject:
Enabled: false

RSpec/ExampleLength:
Max: 16

RSpec/MultipleExpectations:
Max: 8
2 changes: 2 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-
README.md
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eval_gemfile File.expand_path('../gemfiles/rails50.gemfile', __FILE__)
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Souche Car Service Co., Ltd, HANGZHOU.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
117 changes: 117 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# OnsOnRails

整合阿里云 ONS 到 Rails 项目

## 项目依赖

* Linux/Unix 系统
* Ruby 2.1.5 或以上版本
* Rails 4.1.0 或以上版本

## 如何使用

### 配置 Rails 环境

#### 在 Gemfile 添加依赖规则

```ruby
gem 'ons', group: :linux
gem 'ons_on_rails'
```

#### 在 config/application.rb 添加 require 规则

```ruby
Bundler.require(*Rails.groups)
Bundler.require(RUBY_PLATFORM.match(/(linux|darwin)/)[0].to_sym)
```

#### 在 config/ 目录下添加配置文件 ons\_on\_rails.yml

```yaml
#
# access_key,阿里云官网身份验证访问码
# secret_key,阿里云身份验证密钥
#
# user_service_subscriber,消费者名称,需要与实际定义的类名信息保持一致,具体见下文的消费者章节
# user_service_subscriber#consumer_id,阿里云 MQ 控制台创建的 Consumer ID
# user_service_subscriber#topic,阿里云 MQ 控制台创建的 Topic
# user_service_subscriber#tag,当前消费者订阅的 Topic 下所关注的消息标签表达式
#
# user_service_publisher,生成者名称,具体见下文的生成者章节
# user_service_publisher#producer_id,阿里云 MQ 控制台创建的 Producer ID
# user_service_publisher#topic,阿里云 MQ 控制台创建的 Topic
# user_service_publisher#tag,当前生成者发布的消息所使用的消息标签
#
default: &default
access_key: <%= ENV['ONS_ACCESS_KEY'] %>
secret_key: <%= ENV['ONS_SECRET_KEY'] %>
user_service_subscriber:
consumer_id: <%= ENV['ONS_CONSUMER_ID'] %>
topic: <%= ENV['ONS_TOPIC'] %>
tag: 'user_service'
user_service_publisher:
producer_id: <%= ENV['ONS_PRODUCER_ID'] %>
topic: <%= ENV['ONS_TOPIC'] %>
tag: 'user_service'

development:
<<: *default

test:
<<: *default

production:
<<: *default
```
### 消费者
#### 在 app/subscribers 目录下添加消费者实现文件,比如 user\_service\_subscriber.rb
```ruby
# 注意,类名应当与 config/ons_on_rails.yml 中的配置保持一致
class UserServiceSubscriber
include OnsOnRails::Subscriber

def consume(message)
# do something...
end
end
```

#### 在 daemons/ 目录下添加守护进程定义文件,比如 user\_service\_subscriber\_control.rb

```ruby
require 'rubygems'
require 'ons_on_rails'

APP_PATH = File.expand_path('../..', __FILE__)
OnsOnRails.run_subscriber_as_a_daemon(:user_service_subscriber, APP_PATH)
```

#### 启动或关闭消费者进程,此进程会与阿里云 MQ 建立 TCP 连接,然后在本地消费消息

```bash
$ RAILS_ENV=development bundle exec ruby daemons/user_service_subscriber_control.rb start
$ RAILS_ENV=development bundle exec ruby daemons/user_service_subscriber_control.rb stop
```

### 生产者

#### 在 config/initializers/ 目录下添加初始化文件,比如 ons\_on\_rails.rb

```ruby
# 注意,第一个参数应当与 config/ons_on_rails.yml 中的配置保持一致
$user_service_publisher = OnsOnRails.create_publisher(:user_service_publisher, backend: :tcp)
```

#### 调用 OnsOnRails::Publisher#publish 方法

```ruby
$user_service_publisher.publish(operate: :create, user: { name: '123456lkjhgf' })
```

#### 测试相关

设置 OnsOnRails::Publisher 的 backend 为 :test 方法即可,这样生产者会将消息保存到 OnsOnRails::Publisher.deliveries 数组中
25 changes: 25 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'bundler/setup'

APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
load 'rails/tasks/engine.rake'
load 'rails/tasks/statistics.rake'

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new :spec do
Rails.env = 'test'
Rake::Task['app:db:environment:set'].invoke if Rails::VERSION::MAJOR == 5
Rake::Task['app:db:drop'].invoke
Rake::Task['app:db:create'].invoke
Rake::Task['app:db:schema:load'].invoke
end

task 'spec:all' do
Dir.glob(File.join(__dir__, 'gemfiles', '*.gemfile')).each do |gemfile|
sh "BUNDLE_GEMFILE=#{gemfile} bundle install --quiet"
sh "BUNDLE_GEMFILE=#{gemfile} bin/rake spec"
end
end

task default: 'spec:all'
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require 'bundler/setup'
require 'ons_on_rails'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require 'irb'
IRB.start
17 changes: 17 additions & 0 deletions bin/rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# This file was generated by Bundler.
#
# The application 'rake' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
Pathname.new(__FILE__).realpath)

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('rake', 'rake')
17 changes: 17 additions & 0 deletions bin/rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# This file was generated by Bundler.
#
# The application 'rspec' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
Pathname.new(__FILE__).realpath)

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('rspec-core', 'rspec')
17 changes: 17 additions & 0 deletions bin/rubocop
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# This file was generated by Bundler.
#
# The application 'rubocop' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
Pathname.new(__FILE__).realpath)

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('rubocop', 'rubocop')
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
5 changes: 5 additions & 0 deletions gemfiles/rails41.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem 'rails', '~> 4.1.0'

gemspec path: '..'
5 changes: 5 additions & 0 deletions gemfiles/rails42.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem 'rails', '~> 4.2.0'

gemspec path: '..'
5 changes: 5 additions & 0 deletions gemfiles/rails50.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem 'rails', '~> 5.0.0'

gemspec path: '..'
Loading

0 comments on commit 83390a4

Please sign in to comment.