Skip to content

How to Use

jialianglinjl edited this page Jun 25, 2018 · 36 revisions

Contents

"Hello world"

The following is a basic “Hello World” implementation of a resource definition:

public void helloWorld() {
	Entry entry = null;
	try {
		// Resource name that can identify the protected logic
		entry = SphU.entry("HelloWorld");
		/**
		 * BIZ Logic that need protection
		 */
	} catch (BlockException e) {
		/**
		 * Fail to enter, blocked by rules
		 */
	} finally {
		if (entry != null) {
			entry.exit();
		}
	}
}

or we can also use 'if' and 'else' to define resource:

public void helloWorld(){
  if(SphO.entry("HelloWorld")){
    try {
      /**
      *  BIZ Logic that need protection
      */
    } finally {
      SphO.exit();
    }
  }else{
   /**
	 * Fail to enter, blocked by rules
   */
  }
}

Demos can be found in: demo

Resource

Resource is the key concept in sentinel. It can be anything in java code, a method, a service, or even a code snippet. Once a block is wrapped by API SphO.entry(Resource name) and entry.exit() or SphO.entry("HelloWorld") and 'entry.exit()', a resource is created. For resources, sentinel provides real time metrics, and dynamic rules.

Besides hard code API, Sentinel also provides proxies or AOP for most popular frameworks, you can use it without too much invasion.

Clone this wiki locally