澳门新浦京娱乐场网站-www.146.net-新浦京娱乐场官网
做最好的网站

澳门新浦京娱乐场网站:Aside旁路缓存,监察和控

采用Opserver来监控你的ASP.NET项目系列(三、监控你的服务器状态),opserverasp.net

前言

之前有过2篇关于如何监控ASP.NET core项目的文章,有兴趣的也可以看看. 

  •  ASP.NET Core之跨平台的实时性能监控

  • ASP.NET Core之跨平台的实时性能监控(2.健康检查)

今天我们主要来介绍一下,如何使用Opserver监控我们的服务器状态.

Opserver的功能其实很强大,他可以用于连接任何支持Bosun, Orion, or direct WMI监控数据。

大概效果如下:

 

 

正文

Load data on demand into a cache from a data store. This can improve performance and also helps to maintain consistency between data held in the cache and data in the underlying data store.

Redis监控

  Redis 是目前应用广泛的NoSQL,我做的项目中大部分都是与Redis打交道,发现身边的朋友也更多人在用,相对于memcached 来说,它的优势也确实是可圈可点。在随着业务,数据量等不断的扩大后,对Redis的稳定性,性能等也更在的重视,之前就遇到过Redis导致服务器内存不足,做持久化的时候CPU飙高,Redis连接数过多等问题,这时候我们就需要有一个监控工具,能够看到各个指标的变化,方便做跟踪分析,这里先列举下目前接触过几个监控工具。

本文转自:

阅读目录:

1.通过Windows自带的WMI监控服务器

首先,我们找到相关的配置文件,修改配置文件的名称 DashboardSettings.json.example 为 DashboardSettings.json

然后修改DashboardSettings.json设置连接地址 providers,这里我们以WMI为例,如下:

{

  "providers": {

    "wmi": {

      "nodes": [ "USER-20170316IW" ], // List of nodes to monitor

      "staticDataTimeoutSeconds": 300, // (Optional) How long to cache static data (node name, hardware, etc.) - defaults to 5 minutes

      "dynamicDataTimeoutSeconds": 5, // (Optional) How long to cache dynamic data (utilizations, etc.) - defaults to 30 seconds

      "historyHours": 2, // (Optional) How long to retain data (in memory) - defaults to 24 hours

      "username": "Administrator"

    }

}

解释一下参数说明:

  • nodes 可以有多台,可以填 服务器名称 或是 IP地址

  • StaticDataTimeoutSeconds 缓存静态数据(节点名称、硬件等)要多长时间-默认为5分钟

  • DynamicDataTimeoutSeconds 缓存动态数据(CPU,加载时间等)要多长时间-默认为30秒

  • HistoryHours 保留记录的时长,默认为 2 小时

  • Username 远程服务器的账户名

  • Password 远程服务器的密码(PS,我这里因为连接的是本机 所以就不需要了.)

设置完连接之后,我们可以设置我们的警戒值,如下:

設定警戒值

key 說明
cpuWarningPercent cpu 用量警戒(黄色)
cpuCriticalPercent cpu 用量危险(红色)
memoryWarningPercent 内存用量警戒(黄色)
memoryCriticalPercent 内存用量危险(红色)
diskWarningPercent 磁盘空间 用量警戒(黄色)
diskCriticalPercent 磁盘空间 用量危险(红色)

JSON配置直接添加在providers节点下即可.

如下:

{

"providers":{

 "cpuWarningPercent": 50,

  "cpuCriticalPercent": 60,

  "memoryWarningPercent": 90,

  "memoryCriticalPercent": 95, 

  "diskWarningPercent": 85,

  "diskCriticalPercent": 95

}

}

多台服务器的情况下,服务器的配置不一定相同,所以需要根据服务器配置 单独设置报警值,我们可以添加categories节点,并配置如下:

"categories": [

    {

     "name": "第一台", // Name for this group of servers

     "pattern": "-sql",

     "cpuWarningPercent": 20, 

     "cpuCriticalPercent": 60,

      "memoryWarningPercent": 98, 

     "memoryCriticalPercent": 99.2

 },

     "name": "第二台", 

     "pattern": "-sql", 

     "cpuWarningPercent": 20, 

     "cpuCriticalPercent": 60,

      "memoryWarningPercent": 98,

     "memoryCriticalPercent": 99.2

 }

]

这样,我们就完成了我们使用WMI采集信息,并展示在OpServer上的全部工作.

当然,官方推荐的是使用bosun scollector OpServer的形式来采集和显示

所以,我们下面就来讲讲(不过我本人并没有实践..资源实在有限)

将数据从存储加载到缓存中。 这可以提高性能,并且还有助于保持缓存中保存的数据与底层数据存储中的数据之间的一致性。

redis-cli 

  这个工具是Redis本身自带的,当我们下载Redis安装包的时候,就会发现,里面自带了这个工具,使用也非常简单,只需要输入INFO 命令就可以获取Redis服务相关的信息,效果如下:

          澳门新浦京娱乐场网站 1

当然,输入Info的话只能监控一次,如果想连续监控,可以使用命令

-r 10 -i 1 INFO

其中-r 是执行的次数,-i是每次执行的间隔时间(秒)

关于INFO 命令的使用和参数说明,可以参考这个地址:

By Rick Anderson and Steve Smith

  1. 基本介绍
  2. 使用配置
  3. 部署实例
  4. 面板属性

2.通过bosun scollector监控服务器

1、bosun 快速安装(当然,也有不使用Docker的..不过说实话..太复杂了..也许就是因为太复杂了,才放到容器里简化安装了吧)

  •      安装docker

      ..具体我就不说了,直接移步安装步骤官网:

  •      在docker中安装bosun

     使用命令“docker run -d -p 4242:4242 -p 8070:8070 stackexchange/bosun”,安装bosun,文件大概200多M,等待完成即可。

  •      检查是否安装成功

     访问网站

 

2、服务器agent数据采集器(scollector)

   可以到    下载scollector.

    以windows 为例,下载“scollector-windows-amd64.exe"后,用管理员身份运行”cmd.exe"。cd 到文件目录,在命令窗口输入“scollector-windows-amd64 -h  docker-server-ip:8070"

    显示如下信息,表示与bosun服务建立连接。命令如下:

C:>scollector-windows-amd64 -h 192.168.1.101:8070
2016/11/18 15:59:19 info: main.go:213: OpenTSDB host: http://192.168.1.101:8070

然后可在bosun items 界面看到此服务器信息。

写在最后

至此我们就完成了监控服务器的所有操作.喜欢的请关注一下,~觉得不错的可以点个推荐,当然也欢迎大神批评指正.~

相关文章: 

  • .NET开源MSSQL、Redis监控产品Opserver之Redis配置

  • Opserver开源的服务器监控系统(ASP.NET)

  • 采用Opserver来监控你的ASP.NET项目系列(二、监控SQL Server与Asp.Net项目)

``

原文地址:


.NET社区新闻,深度好文,欢迎访问公众号文章汇总

Context and problem

Applications use a cache to improve repeated access to information held in a data store. However, it's impractical to expect that cached data will always be completely consistent with the data in the data store. Applications should implement a strategy that helps to ensure that the data in the cache is as up-to-date as possible, but can also detect and handle situations that arise when the data in the cache has become stale.

应用程序使用缓存来改进对数据存储中保存的信息的重复访问。 然而,期望缓存的数据将始终与数据存储中的数据完全一致是不切实际的。 应用程序应实施有助于确保缓存中的数据尽可能最新的策略,并能够检测和处理缓存中的数据变得过时的情况。

redis-live

这是一款 使用python编写 的监控工具,所以运行依赖于python 环境和几个三方库,不过监控的指标有点少,而且在windows上有时候觉得卡卡的,这里提供下相关的资料

官网地址:

Linux下安装使用:

Windows下安装使用:

HTTP is a stateless protocol; the Web server treats each HTTP request as an independent request. The server retains no knowledge of variable values that were used in previous requests. This article discusses various approaches to preserving application and user (session) state between requests.

基本介绍

Opserver是Stack Exchange的一个开源监控系统,基于Net、MVC开发,所以Net程序员可以轻松基于它二次开发。它主要监控:

  • servers
  • SQL clusters/instances
  • redis
  • elastic search
  • exception logs
  • haproxy

Opserver提供详细的面板,用来快速展示被监控系统的总体情况。 下面Opserver的监控UI界面示例,非常详细: 澳门新浦京娱乐场网站 2 澳门新浦京娱乐场网站 3

Solution

Many commercial caching systems provideread-through and write-through/write-behind operations. In these systems, an application retrieves data by referencing the cache. If the data isn't in the cache, it's retrieved from the data store and added to the cache. Any modifications to data held in the cache are automatically written back to the data store as well.
许多商业缓存系统提供读写和写/写操作。 在这些系统中,应用程序通过引用缓存来检索数据。 如果数据不在缓存中,则从数据存储中检索数据,并将其添加到缓存中。 对缓存中保存的数据的任何修改也会自动写回数据存储区。

For caches that don't provide this functionality, it's the responsibility of the applications that use the cache to maintain the data.
对于不提供此功能的缓存,使用缓存的应用程序负责维护数据。

An application can emulate the functionality of read-through caching by implementing the cache-aside strategy. This strategy loads data into the cache on demand. The figure illustrates using the Cache-Aside pattern to store data in the cache.
应用程序可以通过实现旁路缓存策略来模拟直读缓存的功能。 此策略将数据按需加载到缓存中。 下图说明了使用Cache-Aside模式将数据存储在缓存中。

澳门新浦京娱乐场网站 4

If an application updates information, it can follow the write-through strategy by making the modification to the data store, and by invalidating the corresponding item in the cache.
如果应用程序更新信息,则可以通过对数据存储进行修改,并使缓存中的相应项目无效,从而遵循直写策略。

When the item is next required, using the cache-aside strategy will cause the updated data to be retrieved from the data store and added back into the cache.
当下一个项目需要时,使用cache-aside策略将导致更新的数据从数据存储中检索并添加到高速缓存中。

Opserver

  这个是由Stack Overflow的开源监控解决方案,个人感觉是一款强大而非常好的监控工具,为什么呢,因为它是用C# 来写的(啊哈哈,开个玩笑~~)。只要是由于它除了可以监控Reidis,还可以监控Sql,Elasticsearch等,而且监控的功能也很强大。不过个人感觉在使用上有点过于重量级,而且关于它的资料和官方的文档都很少。这里也提供下相关的资料:

  官方地址:

  Windows 下的使用说明:

Application state, unlike session state, applies to all users and sessions.

使用配置

项目地址:

下载后用VS打开或IIS直接部署即可,下面是它的支持监控系统的view目录,结构比较清晰。

澳门新浦京娱乐场网站 5

Issues and considerations

Consider the following points when deciding how to implement this pattern:

Lifetime of cached data. Many caches implement an expiration policy that invalidates data and removes it from the cache if it's not accessed for a specified period. For cache-aside to be effective, ensure that the expiration policy matches the pattern of access for applications that use the data. Don't make the expiration period too short because this can cause applications to continually retrieve data from the data store and add it to the cache. Similarly, don't make the expiration period so long that the cached data is likely to become stale. Remember that caching is most effective for relatively static data, or data that is read frequently.
缓存数据的生命周期。 许多高速缓存实现一个到期策略,使数据无效,如果在指定的时间段内没有访问,则将其从缓存中删除。 为了使缓存有效,请确保到期策略与使用数据的应用程序的访问模式相匹配。 不要使过期期间太短,因为这可能导致应用程序不断从数据存储中检索数据并将其添加到缓存。 同样,不要让到期期限长到缓存的数据可能变得陈旧。 请记住,缓存对于相对静态的数据或经常读取的数据是最有效的。

Evicting data. Most caches have a limited size compared to the data store where the data originates, and they'll evict data if necessary. Most caches adopt a least-recently-used policy for selecting items to evict, but this might be customizable. Configure the global expiration property and other properties of the cache, and the expiration property of each cached item, to ensure that the cache is cost effective. It isn't always appropriate to apply a global eviction policy to every item in the cache. For example, if a cached item is very expensive to retrieve from the data store, it can be beneficial to keep this item in the cache at the expense of more frequently accessed but less costly items.
数据逐出。 与数据来源的数据存储相比,大多数缓存的大小都有限,如果需要,它们将会排除数据。 大多数缓存采用最近最少使用的策略来选择要逐出的项目,但这应该是可定制的。 配置缓存的全局过期属性和其他属性以及每个缓存项的到期属性,以确保缓存具有成本效益。 将全局逐出策略应用于缓存中的每个项目并不总是适合的。 例如,如果缓存项从数据存储中检索非常昂贵,则将该项而不是那些频繁访问但检索成本较低的项保留在缓存中是有益的。

Priming the cache. Many solutions prepopulate the cache with the data that an application is likely to need as part of the startup processing. The Cache-Aside pattern can still be useful if some of this data expires or is evicted.
启动缓存。 许多解决方案使用应用程序可能需要的数据预先填充缓存,作为启动处理的一部分。 如果某些数据到期或被驱逐,Cache-Aside模式仍然有用。

Consistency. Implementing the Cache-Aside pattern doesn't guarantee consistency between the data store and the cache. An item in the data store can be changed at any time by an external process, and this change might not be reflected in the cache until the next time the item is loaded. In a system that replicates data across data stores, this problem can become serious if synchronization occurs frequently.
一致性。 实现Cache-Aside模式不能保证数据存储和缓存之间的一致性。 数据存储中的项目可以随时通过外部进程进行更改,并且在下次加载项目前,此更改可能不会反映在缓存中。 在跨数据存储复制数据的系统中,如果同步频繁发生,则此问题可能会变得严重。

Local (in-memory) caching. A cache could be local to an application instance and stored in-memory. Cache-aside can be useful in this environment if an application repeatedly accesses the same data. However, a local cache is private and so different application instances could each have a copy of the same cached data. This data could quickly become inconsistent between caches, so it might be necessary to expire data held in a private cache and refresh it more frequently. In these scenarios, consider investigating the use of a shared or a distributed caching mechanism.
本地(内存中)缓存。 缓存可以是应用程序实例的本地,并存储在内存中。 如果应用程序重复访问相同的数据,缓存在此环境中可能很有用。 然而,本地缓存是私有的,因此不同的应用程序实例可以具有相同缓存数据的副本。 这些数据可能会在高速缓存之间迅速变得不一致,因此使保存在私有缓存中的数据过期,并更频繁地刷新数据变得非常有必要。 在这些场景下,请考虑调查使用共享或分布式缓存机制。

RedisMonitor(自己造轮子)

   毛爷爷曾经说过“自动动手,丰衣足食”。大概看了下Opserver的源码,看到Opserver也是使用StackExchange.Redis 调用了Redis的INFO来做监控的,之前刚好也看过StackExchange.Redis 的使用,而且StackExchange.Redis 对于获Server信息这块,都已经封装好了,几乎可以直接调用,于是就想干脆自己动手做一个简单的监控。

  对于StackExchange.Redis 的使用和通用类的封装,可以参考我之前写的一篇文章:

 

  而StackExchange.Redis 如何获取Server信息和使用命令操作,可以参看官网的:

  

Session state

Session state is a feature in ASP.NET Core you can enable that allows you to save and store user data while the user browses your web app. Session data is stored in dictionary on the server and persists data across requests from a browser. A session ID is stored on the client in a cookie. The session ID cookie is sent to server with each request, and the server uses the session ID to fetch the session data. The session ID cookie is per browser, you cannot share session across browsers. Session cookies have no specified timeout, they are deleted when the browser session ends. If a cookie is received for an expired session, then a new session is created using the same Session cookie.

Session is retained by the server for a limited time after the last request. The default session timeout is 20 minutes, but you can configure session time out. The session data is backed by a cache. Session is ideal for storing user state that is specific to a particular session but which doesn’t need to be persisted permanently. Data is deleted from the backing store either when you call Session.Clear or when the session expires in the data store. The server does not know when the browser is closed or the Session cookie is deleted.

安全配置

Opserver系统本身后登陆验证,支持3种安全认证方式:

<?xml version="1.0" encoding="utf-8"?>
<SecuritySettings provider="AD">
    <!-- 可选, 下面的网络可以不用验证直接访问 -->
    <InternalNetworks>
        <Network name="SE Internal" cidr="10.0.0.0/8" />
    </InternalNetworks>
</SecuritySettings>

<!-- 
每个人都是管理都可访问
<SecuritySettings provider="alladmin" />
-->

如果使用活动目录验证,可以直接在web.config配置ViewGroups、AdminGroups,也可以单独在每个系统监控json配置文件里面添加ViewGroups、AdminGroups:

"viewGroups": "*",
"adminGroups": "SysAdmins",
"user": "user",
"password": "pass",
"adminUser": "adminuser",
"adminPassword": "adminpass",

When to use this pattern

Use this pattern when:

  • A cache doesn't provide native read-through and write-through operations.
  • Resource demand is unpredictable. This pattern enables applications to load data on demand. It makes no assumptions about which data an application will require in advance.
  • 缓存不提供本机的直读和直写操作。
  • 资源需求是不可预知的。 此模式使应用程序能够按需加载数据。 它不会提前预测应用程序需要哪些数据。

This pattern might not be suitable:

  • When the cached data set is static. If the data will fit into the available cache space, prime the cache with the data on startup and apply a policy that prevents the data from expiring.
  • For caching session state information in a web application hosted in a web farm. In this environment, you should avoid introducing dependencies based on client-server affinity.
  • 当缓存的数据集是静态的。 如果数据将适合可用的缓存空间,请使用启动时的数据引导缓存,并应用防止数据过期的策略。
  • 用于在Web场中托管的Web应用程序中缓存会话状态信息。 在这种环境中,您应该避免根据客户端 - 服务器的亲和性引入依赖关系。

首先是ConnectionMultiplexer对象封装

  ConnectionMultiplexer对象StackExchange.Redis最中枢的对象,几乎所有的操作都是依靠它来使用,代码如下:

public static class RedisConnectionHelp
    {
        private static readonly ConcurrentDictionary<string, ConnectionMultiplexer> ConnectionCache = new ConcurrentDictionary<string, ConnectionMultiplexer>();

        /// <summary>
        /// 缓存获取
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        public static ConnectionMultiplexer GetConnectionMultiplexer(string connectionString)
        {
            if (!ConnectionCache.ContainsKey(connectionString))
            {
                ConnectionCache[connectionString] = GetManager(connectionString);
            }
            return ConnectionCache[connectionString];
        }

        private static ConnectionMultiplexer GetManager(string connectionString)
        {
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new Exception("Redis 连接地址不能为空");
            }
            var connect = ConnectionMultiplexer.Connect(connectionString);

            return connect;
        }
    }

获取服务器信息用到的几个方法如下

Warning

Sensitive data should never be stored in session. You can’t guarantee the client will close the browser and clear their session cookie (and some browsers keep them alive across windows). Consequently, you can’t assume that a session is restricted to a single user, the next user may continue with the same session.

The in-memory session provider stores session data on the server, which can impact scale out. If you run your web app on a server farm, you’ll need to enable sticky sessions to tie each session to a specific server.  Windows Azure Web Sites defaults to sticky sessions (Application Request Routing or ARR). Sticky session can impact scalability and complicate updating your web app. The Redis and SQL Server distributed caches don't require sticky sessions and are the preferred approach to multi-server caching. See Working with a Distributed Cache for more information.

See Configuring Session below for more details.

监控配置

配置监控的地方在/Config/目录,Stack Exchange提供对应系统的配置示例,如图: 如果没有配置任何系统监控文件,浏览OpServer页面时,会报'No Configuration'的警告提示。 这里以Redis为例,监控配置如下:

{
    "allServers": {
        "name": "All",
        "instances": [
              {
                "name": "本地master",
                "port": "6379"
            },
            {
                "name": "本地slave1",
                "port": "6380"
            },
            {
                "name": "本地master2",
                "port": "6382"
            }
        ]

    },
    "Servers": [
        { "name": "127.0.0.1" }
    ]
}

Example

In Microsoft Azure you can use Azure Redis Cache to create a distributed cache that can be shared by multiple instances of an application.
To connect to an Azure Redis Cache instance, call the static Connect method and pass in the connection string. The method returns a ConnectionMultiplexer that represents the connection. One approach to sharing a ConnectionMultiplexer instance in your application is to have a static property that returns a connected instance, similar to the following example. This approach provides a thread-safe way to initialize only a single connected instance.
在Microsoft Azure中,您可以使用Azure Redis Cache创建可由应用程序的多个实例共享的分布式缓存。
要连接到Azure Redis Cache实例,请调用静态Connect方法并传入连接字符串。 该方法返回表示连接的ConnectionMultiplexer。 在应用程序中共享ConnectionMultiplexer实例的一种方法是具有返回连接实例的静态属性,类似于以下示例。 这种方法提供了线程安全的方法来初始化一个连接的实例。

private static ConnectionMultiplexer Connection;
// Redis Connection string info
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
string cacheConnection = ConfigurationManager.AppSettings["CacheConnection"].ToString();
return ConnectionMultiplexer.Connect(cacheConnection);
});
public static ConnectionMultiplexer Connection => lazyConnection.Value;

The GetMyEntityAsync method in the following code example shows an implementation of the Cache-Aside pattern based on Azure Redis Cache. This method retrieves an object from the cache using the read-though approach.
以下代码示例中的GetMyEntityAsync方法显示了基于Azure Redis Cache的Cache-Aside模式的实现。 此方法使用只读方法从缓存中检索对象。

An object is identified by using an integer ID as the key. The GetMyEntityAsync method tries to retrieve an item with this key from the cache. If a matching item is found, it's returned. If there's no match in the cache, the GetMyEntityAsync method retrieves the object from a data store, adds it to the cache, and then returns it. The code that actually reads the data from the data store is not shown here, because it depends on the data store. Note that the cached item is configured to expire to prevent it from becoming stale if it's updated elsewhere.
通过使用整数ID作为关键字来标识对象。
GetMyEntityAsync方法尝试使用此键从缓存中检索项目。 如果找到匹配项,则返回。 如果缓存中没有匹配项,GetMyEntityAsync方法将从数据存储中检索该对象,将其添加到缓存中,然后返回。 实际上从数据存储器读取数据的代码在这里不显示,因为它取决于数据存储。 请注意,缓存的项目被配置为过期,以防止其在其他地方更新。

// Set five minute expiration as a default
private const double DefaultExpirationTimeInMinutes = 5.0;
public async Task<MyEntity> GetMyEntityAsync(int id)
{
// Define a unique key for this method and its parameters.
var key = $"MyEntity:{id}";
var cache = Connection.GetDatabase();
// Try to get the entity from the cache.
var json = await cache.StringGetAsync(key).ConfigureAwait(false);
var value = string.IsNullOrWhiteSpace(json)
? default(MyEntity)
: JsonConvert.DeserializeObject<MyEntity>(json);
if (value == null) // Cache miss
{
// If there's a cache miss, get the entity from the original store and cache it.
// Code has been omitted because it's data store dependent.
value = ...;
// Avoid caching a null value.
if (value != null)
{
// Put the item in the cache with a custom expiration time that
// depends on how critical it is to have stale data.
await cache.StringSetAsync(key, JsonConvert.SerializeObject(value)).ConfigureAwait(false);
await cache.KeyExpireAsync(key, TimeSpan.FromMinutes(DefaultExpirationTimeInMinutes)).ConfigureAwait(false);
}
}
return value;
}

The examples use the Azure Redis Cache API to access the store and retrieve information from the cache. For more information, see Using Microsoft Azure Redis Cache and How to create a Web App with Redis Cache

这些示例使用Azure Redis Cache API来访问存储并从缓存中检索信息。 有关详细信息,请参阅Using Microsoft Azure Redis Cache and
How to create a Web App with Redis Cache

The UpdateEntityAsync method shown below demonstrates how to invalidate an object in the cache when the value is changed by the application. This is an example of a write-through approach. The code updates the original data store and then removes the cached item from the cache by calling the KeyDeleteAsync method, specifying the key.
下面显示的UpdateEntityAsync方法演示了当应用程序更改值时,如何使缓存中的对象无效。 这是一个写通方法的例子。 代码更新原始数据存储,然后通过调用KeyDeleteAsync方法(指定该键)从缓存中删除缓存的项目。

The order of the steps in this sequence is important. If the item is removed before the cache is updated, the client application has a short period of time to fetch the data (because it isn't found in the cache) before the item in the data store has been changed, resulting in the cache containing stale data.

这个顺序的顺序很重要。 如果在缓存更新之前删除该项目,则客户端应用程序在数据存储中的项目已更改之前,具有很短的时间来获取数据(因为它在高速缓存中未找到),从而导致高速缓存包含陈旧的数据。

public async Task UpdateEntityAsync(MyEntity entity)
{
// Invalidate the current cache object
var cache = Connection.GetDatabase();
var id = entity.Id;
var key = $"MyEntity:{id}"; // Get the correct key for the cached object.
await cache.KeyDeleteAsync(key).ConfigureAwait(false);
// Update the object in the original data store
await this.store.UpdateEntityAsync(entity).ConfigureAwait(false);
}

GetServer(hostAndPort)

var server = RedisConnectionHelp.GetConnectionMultiplexer(readWriteHosts).GetServer(hostAndPort)

  这里获取IServer对象,所有服务器相关的方法和信息都封装在这个对象中。

TempData

ASP.NET Core MVC exposes the TempData property on a Controller. TempData can be used for storing transient data that only needs to be available for a single request after the current request. TempData is frequently useful for redirection, when data is needed for more than a single request. TempData is built on top of Session State.

部署实例

认证配置<SecuritySettings provider="alladmin">所有人都是管理员,打开浏览器访问,输入账号admin,密码admin:

澳门新浦京娱乐场网站 6

可以看到有2组实例,其中6380是slave,6379是master,从图表上可以清晰看到层架关系。

澳门新浦京娱乐场网站 7

实例列表:

澳门新浦京娱乐场网站 8

点击单个Redis实例进去看到。

澳门新浦京娱乐场网站 9

Related guidance

The following information may be relevant when implementing this pattern:

  • Caching Guidance. Provides additional information on how you can cache data in a cloud solution, and the issues that you should consider when you implement a cache.

  • Data Consistency Primer. Cloud applications typically use data that's spread across data stores. Managing and maintaining data consistency in this environment is a critical aspect of the system, particularly the concurrency and availability issues that can arise. This primer describes issues about consistency across distributed data, and summarizes how an application can implement eventual consistency to maintain the availability of data.

  • Caching Guidance. 提供有关如何在云解决方案中缓存数据的其他信息,以及实现缓存时应考虑的问题。

  • 澳门新浦京娱乐场网站:Aside旁路缓存,监察和控制你的服务器状态。Data Consistency Primer. 云应用程序通常使用分布在数据存储中的数据。 管理和维护此环境中的数据一致性是系统的一个重要方面,特别是可能出现的并发和可用性问题。 本引言介绍了有关分布式数据一致性的问题,并总结了应用程序如何实现最终的一致性来维护数据的可用性。

Ping()

server.Ping();

  用来测试获取服务器的响应时间

Cookie-based TempData provider (requires ASP.NET Core 1.1.0 and higher)

To enable the  Cookie-based TempData provider, register the CookieTempDataProvider service in ConfigureServices:

Copy

C#

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    // Add CookieTempDataProvider after AddMvc and include ViewFeatures.
    // using Microsoft.AspNetCore.Mvc.ViewFeatures;
    services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();
}

The cookie-based TempData provider does not use Session; data is stored in a cookie on the client. The cookie data is encoded with the Base64UrlTextEncoder. The cookie is encrypted and chunked, so the single cookie size limit does not apply. See CookieTempDataProvider for more information.

The cookie data is not compressed. Using compression with encryption can lead to security problems such as the CRIME) and BREACH) attacks.

面板属性

面板展示的属性都是可以通过redis info命令获取到,opserver做了更清晰的展示。

Ops(/sec)  每秒处理量

memory(used)即used_memory_rss(used_memory)

used_memory_rss : 从操作系统的角度,返回 Redis 已分配的内存总量(俗称常驻集大小)。这个值和 top 、 ps等命令的输出一致。

used_memory_peak : Redis 的内存消耗峰值(以字节为单位)

used_memory : 由 Redis 分配器分配的内存总量,以字节(byte)为单位

 

Summary是总体概览部分。

Memory是内存使用情况,重要。

persistence 是RDB和AOF的状态。

 

keyspace key存储的情况,analyze进去可以查看详细分布。

 

stats  客户端命令的key命中率和处理量

 

clients 查看有哪个ip(或机器名)过来的连接数多,很方便的定位到那台应用端机器长时间没有释放连接,重要。

 

slow command log 服务端接受的命令日志。

 

 

Opserver 算是个比较轻量级的监控系统,部署修改都非常方便,比如增加连接数或者内存报警功能。 

InfoRaw()

server.InfoRaw()

  获取服务器原始内容信息,输出的内容就是我们直接使用info命令看到的一样。

Query strings

State from one request can be provided to another request by adding values to the new request's query string. Query strings should never be used with sensitive data. It is also best used with small amounts of data.

Query strings are useful for capturing state in a persistent manner, allowing links with embedded state to be created and shared through email or social networks. However, no assumption can be made about the user making the request, since URLs with query strings can easily be shared. Care must also be taken to avoid Cross-Site Request Forgery (CSRF)) attacks. An attacker could trick a user into visiting a malicious site while authenticated. CSRF are a major form of vulnerability that can be used to steal user data from your app, or take malicious actions on the behalf of the user. Any preserved application or session state needs to protect against CSRF attacks. See Preventing Cross-Site Request Forgery (XSRF/CSRF) Attacks in ASP.NET Core

Info()

server.Info()

  在一开始介绍的监控工具1的时候,截图中可以看到,info 命令输出的内容中包括了Server,Clients,Memory,Persistence等信息,Info()方法帮我们GroupBy了这些信息,做好了分组,并且所有的指标都封装成了Dictionary的类型,使用起来非常方便。

Post data and hidden fields

Data can be saved in hidden form fields and posted back on the next request. This is common in multi-page forms.  It’s insecure in that the client can tamper with the data so the server must always revalidate it.

ClientList()

server.ClientList()

  获取当前连接的所有客户端信息

  只需要使用这几个方法,然后定时的刷新,将结果显示到页面,就能监控到Redis的信息和状态了这里贴上几个监控的效果图

Cookies

Data can be stored in cookies. Cookies are sent with every request, so the size should be kept to a minimum. Ideally, only an identifier should be used, with the actual data stored somewhere on the server. Cookies are subject to tampering and therefore need to be validated on the server. Cookies are limited by most browsers to 4096 bytes and you have only a limited number of cookies per domain. Although the durability of the cookie on a client is subject to user intervention and expiration, cookies are generally the most durable form of data persistence on the client.

Cookies are often used for personalization, where content is customized for a known user. In most cases, identification is the issue rather than authentication. Thus, you can typically secure a cookie that is used for identification by storing the user name, account name, or a unique user ID (such as a GUID) in the cookie and then use the cookie to access the user personalization infrastructure of a site.

监控列表

澳门新浦京娱乐场网站 10

HttpContext.Items

The Items collection is the good location to store data that is only needed while processing a given request. Its contents are discarded after each request. It is best used as a means of communicating between components or middleware that operate at different points in time during a request and have no direct way to pass parameters. See Working with HttpContext.Items, below.

详情页面

澳门新浦京娱乐场网站 11

Cache

Caching provides a means of efficiently storing and retrieving data. It provides rules for expiring cached items based on time and other considerations. Learn more about Caching.

  • Configuring Session


The Microsoft.AspNetCore.Session package provides middleware for managing session state. Enabling the session middleware requires the following in Startup:

  • Add any of the IDistributedCache memory caches. The IDistributedCache implementation is used as a backing store for session.
  • Call AddSession, which requires NuGet package "Microsoft.AspNetCore.Session".
  • Call UseSession.

The following code shows how to set up the in-memory session provider:

Copy

C#

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        // Adds a default in-memory implementation of IDistributedCache.
        services.AddDistributedMemoryCache();

        services.AddSession(options =>
        {
            // Set a short timeout for easy testing.
            options.IdleTimeout = TimeSpan.FromSeconds(10);
            options.CookieHttpOnly = true;
        });
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseSession();
        app.UseMvcWithDefaultRoute();
    }
}

You can reference Session from HttpContext once it is installed and configured.

Note: Accessing Session before UseSession has been called throwsInvalidOperationException: Session has not been configured for this application or request.

Attempting to create a new Session (that is, no session cookie has been created) after you have already begun writing to the Response stream throws InvalidOperationException: The session cannot be established after the response has started. The exception can be found in the web server log, it will not be displayed in the browser.

简单监控

澳门新浦京娱乐场网站 12

Loading Session asynchronously

The default session provider in ASP.NET Core will only load the session record from the underlying IDistributedCache store asynchronously if the ISession.LoadAsync method is explicitly called before calling the TryGetValue, Set or Remove methods. Failure to call LoadAsync first will result in the underlying session record being loaded synchronously, which could potentially impact the ability of the app to scale.

If applications wish to enforce this pattern, they could wrap the DistributedSessionStore and DistributedSession implementations with versions that throw if the LoadAsync method has not been called before calling TryGetValue, Set or Remove澳门新浦京娱乐场网站,, and register the wrapped versions in the services container.

详细监控

澳门新浦京娱乐场网站 13

Implementation Details

Session uses a cookie to track and identify requests from the same browser. By default this cookie is named ".AspNet.Session" and uses a path of "/". The cookie default does not specify a domain. The cookie default is not made available to client-side script on the page (because CookieHttpOnly defaults to true).

Session defaults can be overridden by using SessionOptions:

Copy

C#

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    // Adds a default in-memory implementation of IDistributedCache.
    services.AddDistributedMemoryCache();

    services.AddSession(options =>
    {
        options.CookieName = ".AdventureWorks.Session";
        options.IdleTimeout = TimeSpan.FromSeconds(10);
    });
}

The IdleTimeout is used by the server to determine how long a session can be idle before its contents are abandoned. IdleTimeout is independent of the cookie expiration. Each request that passes through the Session middleware (read from or written to) will reset the timeout.

Note: Session is non-locking, so if two requests both attempt to modify the contents of session, the last one will win. Session is implemented as a coherent session, which means that all of the contents are stored together. This means that if two requests are modifying different parts of the session (different keys), they may still impact each other.

监控图表

 澳门新浦京娱乐场网站 14

Setting and getting Session values

Session is accessed through the Session property on HttpContext. Session is an ISession implementation.

The following example shows setting and getting an int and a string:

Copy

C#

public class HomeController : Controller
{
    const string SessionKeyName = "_Name";
    const string SessionKeyYearsMember = "_YearsMember";
    const string SessionKeyDate = "_Date";

    public IActionResult Index()
    {
        // Requires using Microsoft.AspNetCore.Http;
        HttpContext.Session.SetString(SessionKeyName, "Rick");
        HttpContext.Session.SetInt32(SessionKeyYearsMember, 3);
        return RedirectToAction("SessionNameYears");
    }
    public IActionResult SessionNameYears()
    {
        var name = HttpContext.Session.GetString(SessionKeyName);
        var yearsMember = HttpContext.Session.GetInt32(SessionKeyYearsMember);

        return Content($"Name: "{name}",  Membership years: "{yearsMember}"");
    }

If you add the following extension methods, you can set and get serializable objects to Session:

Copy

C#

using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;

public static class SessionExtensions
{
    public static void Set<T>(this ISession session, string key, T value)
    {
        session.SetString(key, JsonConvert.SerializeObject(value));
    }

    public static T Get<T>(this ISession session,string key)
    {
        var value = session.GetString(key);
        return value == null ? default(T) : 
                              JsonConvert.DeserializeObject<T>(value);
    }
}

The following sample shows how to set and get a serializable object:

Copy

C#

public IActionResult SetDate()
{
    // Requires you add the Set extension method mentioned in the article.
    HttpContext.Session.Set<DateTime>(SessionKeyDate, DateTime.Now);
    return RedirectToAction("GetDate");
}

public IActionResult GetDate()
{
    // Requires you add the Get extension method mentioned in the article.
    var date = HttpContext.Session.Get<DateTime>(SessionKeyDate);
    var sessionTime = date.TimeOfDay.ToString();
    var currentTime = DateTime.Now.TimeOfDay.ToString();

    return Content($"Current time: {currentTime} - "
                   $"session time: {sessionTime}");
}

连接客户端监控

澳门新浦京娱乐场网站 15

Working with HttpContext.Items

The HttpContext abstraction provides support for a simple dictionary collection of type IDictionary<object, object>, called Items. This collection is available from the start of an HttpRequest and is discarded at the end of each request. You can access it by  assigning a value to a keyed entry, or by requesting the value for a given key.

In the sample below, Middleware adds isVerified to the Items collection:

Copy

C#

app.Use(async (context, next) =>
{
    // perform some verification
    context.Items["isVerified"] = true;
    await next.Invoke();
});

Later in the pipeline, another middleware could access it:

Copy

C#

app.Run(async (context) =>
{
    await context.Response.WriteAsync("Verified request? "   context.Items["isVerified"]);
});

Note: Since keys into Items are simple strings, if you are developing middleware that needs to work across many applications, you may wish to prefix your keys with a unique identifier to avoid key collisions (for example, "MyComponent.isVerified" instead of "isVerified").

  • Application state data


Use Dependency Injection to make data available to all users.

  1. Define a service containing the data (for example, a class named MyAppData).
  • Copy

C#

public class MyAppData
{
    // Declare properties/methods/etc.
}
  1. Add the service class to ConfigureServices (for example services.AddSingleton<MyAppData>();.
  2. Consume the data service class in each controller:
  • Copy

C#

public class MyController : Controller
{
    public MyController(MyAppData myService)
    {
        // Do something with the service (read some data from it, 
        // store it in a private field/property, etc.
    }
    }
}

源码下载

关键的来了,要做一个懂得分享的程序员,源码地址:

Common errors when working with session

  • "Unable to resolve service for type 'Microsoft.Extensions.Caching.Distributed.IDistributedCache' while attempting to activate 'Microsoft.AspNetCore.Session.DistributedSessionStore'."

    Commonly caused by not configuring at least one IDistributedCache implementation. See Working with a Distributed Cache and In memory caching for more information

使用说明

下载源码编译后,只需要修改Config文件夹下面的RedisServiceConfig.json文件,修改成自己需要监控的Redis服务器地址就好了,ServerHost的配置可以参考

https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Configuration.md

 

Additional Resources

  • Sample code used in this document

本文由澳门新浦京娱乐场网站发布于服务器,转载请注明出处:澳门新浦京娱乐场网站:Aside旁路缓存,监察和控