Connect with us

0006

XRegExp 0.2: Now With Named Capture

Published

on

Update: A beta version of XRegExp 0.3 is now available as part of the RegexPal download package.

JavaScript’s regular expression flavor doesn’t support named capture. Well, says who? XRegExp 0.2 brings named capture support, along with several other new features. But first of all, if you haven’t seen the previous version, make sure to check out my post on XRegExp 0.1, because not all of the documentation is repeated below.

Highlights

  • Comprehensive named capture support (New)
  • Supports regex literals through the addFlags method (New)
  • Free-spacing and comments mode (x)
  • Dot matches all mode (s)
  • Several other minor improvements over v0.1

Named capture

There are several different syntaxes in the wild for named capture. I’ve compiled the following table based on my understanding of the regex support of the libraries in question. XRegExp’s syntax is included at the top.

Library Capture Backreference In replacement Stored at
XRegExp (<name>…) \k<name> ${name} result.name
.NET (?<name>…)


(?'name'…)
\k<name>


\k'name'
${name} Matcher.Groups('name')
Perl 5.10 (beta) (?<name>…)


(?'name'…)
\k<name>


\k'name'


\g{name}
$+{name} ??
Python (?P<name>…) (?P=name) \g<name> result.group('name')
PHP preg (PCRE) (.NET, Perl, and Python styles) $regs['name'] $result['name']

No other major regex library currently supports named capture, although the JGsoft engine (used by products like RegexBuddy) supports both .NET and Python syntax. XRegExp does not use a question mark at the beginning of a named capturing group because that would prevent it from being used in regex literals (JavaScript would immediately throw an “invalid quantifier” error).

XRegExp supports named capture on an on-request basis. You can add named capture support to any regex though the use of the new “k” flag. This is done for compatibility reasons and to ensure that regex compilation time remains as fast as possible in all situations.

Following are several examples of using named capture:

// Add named capture support using the XRegExp constructor
var repeatedWords = new XRegExp("\\b (<word> \\w+ ) \\s+ \\k<word> \\b", "gixk");

// Add named capture support using RegExp, after overriding the native constructor
XRegExp.overrideNative();
var repeatedWords = new RegExp("\\b (<word> \\w+ ) \\s+ \\k<word> \\b", "gixk");

// Add named capture support to a regex literal
var repeatedWords = /\b (<word> \w+ ) \s+ \k<word> \b/.addFlags("gixk");

var data = "The the test data.";

// Check if data contains repeated words
var hasDuplicates = repeatedWords.test(data);
// hasDuplicates: true

// Use the regex to remove repeated words
var output = data.replace(repeatedWords, "${word}");
// output: "The test data."

In the above code, I’ve also used the x flag provided by XRegExp, to improve readability. Note that the addFlags method can be called multiple times on the same regex (e.g., /pattern/g.addFlags("k").addFlags("s")), but I’d recommend adding all flags in one shot, for efficiency.

Here are a few more examples of using named capture, with an overly simplistic URL-matching regex (for comprehensive URL parsing, see parseUri):

var url = "http://microsoft.com/path/to/file?q=1";
var urlParser = new XRegExp("^(<protocol>[^:/?]+)://(<host>[^/?]*)(<path>[^?]*)\\?(<query>.*)", "k");
var parts = urlParser.exec(url);
/* The result:
parts.protocol: "http"
parts.host: "microsoft.com"
parts.path: "/path/to/file"
parts.query: "q=1" */

// Named backreferences are also available in replace() callback functions as properties of the first argument
var newUrl = url.replace(urlParser, function(match){
	return match.replace(match.host, "yahoo.com");
});
// newUrl: "http://yahoo.com/path/to/file?q=1"

Note that XRegExp’s named capture functionality does not support deprecated JavaScript features including the lastMatch property of the global RegExp object and the RegExp.prototype.compile() method.

Singleline (s) and extended (x) modes

The other non-native flags XRegExp supports are s (singleline) for “dot matches all” mode, and x (extended) for “free-spacing and comments” mode. For full details about these modifiers, see the FAQ in my XRegExp 0.1 post. However, one difference from the previous version is that XRegExp 0.2, when using the x flag, now allows whitespace between a regex token and its quantifier (quantifiers are, e.g., +, *?, or {1,3}). Although the previous version’s handling/limitation in this regard was documented, it was atypical compared to other regex libraries. This has been fixed.

The code

/* XRegExp 0.2.2; MIT License
By Steven Levithan <http://stevenlevithan.com>
----------
Adds support for the following regular expression features:
- Free-spacing and comments ("x" flag)
- Dot matches all ("s" flag)
- Named capture ("k" flag)
 - Capture: (<name>...)
 - Backreference: \k<name>
 - In replacement: ${name}
 - Stored at: result.name
*/

/* Protect this from running more than once, which would break its references to native functions */
if (window.XRegExp === undefined) {
	var XRegExp;
	
	(function () {
		var native = {
			RegExp: RegExp,
			exec: RegExp.prototype.exec,
			match: String.prototype.match,
			replace: String.prototype.replace
		};
		
		XRegExp = function (pattern, flags) {
			return native.RegExp(pattern).addFlags(flags);
		};
		
		RegExp.prototype.addFlags = function (flags) {
			var pattern = this.source,
				useNamedCapture = false,
				re = XRegExp._re;
			
			flags = (flags || "") + native.replace.call(this.toString(), /^[\S\s]+\//, "");
			
			if (flags.indexOf("x") > -1) {
				pattern = native.replace.call(pattern, re.extended, function ($0, $1, $2) {
					return $1 ? ($2 ? $2 : "(?:)") : $0;
				});
			}
			
			if (flags.indexOf("k") > -1) {
				var captureNames = [];
				pattern = native.replace.call(pattern, re.capturingGroup, function ($0, $1) {
					if (/^\((?!\?)/.test($0)) {
						if ($1) useNamedCapture = true;
						captureNames.push($1 || null);
						return "(";
					} else {
						return $0;
					}
				});
				if (useNamedCapture) {
					/* Replace named with numbered backreferences */
					pattern = native.replace.call(pattern, re.namedBackreference, function ($0, $1, $2) {
						var index = $1 ? captureNames.indexOf($1) : -1;
						return index > -1 ? "\\" + (index + 1).toString() + ($2 ? "(?:)" + $2 : "") : $0;
					});
				}
			}
			
			/* If "]" is the leading character in a character class, replace it with "\]" for consistent
			cross-browser handling. This is needed to maintain correctness without the aid of browser sniffing
			when constructing the regexes which deal with character classes. They treat a leading "]" within a
			character class as a non-terminating, literal character, which is consistent with IE, .NET, Perl,
			PCRE, Python, Ruby, JGsoft, and most other regex engines. */
			pattern = native.replace.call(pattern, re.characterClass, function ($0, $1) {
				/* This second regex is only run when a leading "]" exists in the character class */
				return $1 ? native.replace.call($0, /^(\[\^?)]/, "$1\\]") : $0;
			});
			
			if (flags.indexOf("s") > -1) {
				pattern = native.replace.call(pattern, re.singleline, function ($0) {
					return $0 === "." ? "[\\S\\s]" : $0;
				});
			}
			
			var regex = native.RegExp(pattern, native.replace.call(flags, /[sxk]+/g, ""));
			
			if (useNamedCapture) {
				regex._captureNames = captureNames;
			/* Preserve capture names if adding flags to a regex which has already run through addFlags("k") */
			} else if (this._captureNames) {
				regex._captureNames = this._captureNames.valueOf();
			}
			
			return regex;
		};
		
		String.prototype.replace = function (search, replacement) {
			/* If search is not a regex which uses named capturing groups, just run the native replace method */
			if (!(search instanceof native.RegExp && search._captureNames)) {
				return native.replace.apply(this, arguments);
			}
			
			if (typeof replacement === "function") {
				return native.replace.call(this, search, function () {
					/* Convert arguments[0] from a string primitive to a string object which can store properties */
					arguments[0] = new String(arguments[0]);
					/* Store named backreferences on the first argument before calling replacement */
					for (var i = 0; i < search._captureNames.length; i++) {
						if (search._captureNames[i]) arguments[0][search._captureNames[i]] = arguments[i + 1];
					}
					return replacement.apply(window, arguments);
				});
			} else {
				return native.replace.call(this, search, function () {
					var args = arguments;
					return native.replace.call(replacement, XRegExp._re.replacementVariable, function ($0, $1, $2) {
						/* Numbered backreference or special variable */
						if ($1) {
							switch ($1) {
								case "$": return "$";
								case "&": return args[0];
								case "`": return args[args.length - 1].substring(0, args[args.length - 2]);
								case "'": return args[args.length - 1].substring(args[args.length - 2] + args[0].length);
								/* Numbered backreference */
								default:
									/* What does "$10" mean?
									- Backreference 10, if at least 10 capturing groups exist
									- Backreference 1 followed by "0", if at least one capturing group exists
									- Else, it's the string "$10" */
									var literalNumbers = "";
									$1 = +$1; /* Cheap type-conversion */
									while ($1 > search._captureNames.length) {
										literalNumbers = $1.toString().match(/\d$/)[0] + literalNumbers;
										$1 = Math.floor($1 / 10); /* Drop the last digit */
									}
									return ($1 ? args[$1] : "$") + literalNumbers;
							}
						/* Named backreference */
						} else if ($2) {
							/* What does "${name}" mean?
							- Backreference to named capture "name", if it exists
							- Else, it's the string "${name}" */
							var index = search._captureNames.indexOf($2);
							return index > -1 ? args[index + 1] : $0;
						} else {
							return $0;
						}
					});
				});
			}
		};
		
		RegExp.prototype.exec = function (str) {
			var result = native.exec.call(this, str);
			if (!(this._captureNames && result && result.length > 1)) return result;
			
			for (var i = 1; i < result.length; i++) {
				var name = this._captureNames[i - 1];
				if (name) result[name] = result[i];
			}
			
			return result;
		};
		
		String.prototype.match = function (regexp) {
			if (!regexp._captureNames || regexp.global) return native.match.call(this, regexp);
			return regexp.exec(this);
		};
	})();
}

/* Regex syntax parsing with support for escapings, character classes, and various other context and cross-browser issues */
XRegExp._re = {
	extended: /(?:[^[#\s\\]+|\\(?:[\S\s]|$)|\[\^?]?(?:[^\\\]]+|\\(?:[\S\s]|$))*]?)+|(\s*#[^\n\r]*\s*|\s+)([?*+]|{\d+(?:,\d*)?})?/g,
	singleline: /(?:[^[\\.]+|\\(?:[\S\s]|$)|\[\^?]?(?:[^\\\]]+|\\(?:[\S\s]|$))*]?)+|\./g,
	characterClass: /(?:[^\\[]+|\\(?:[\S\s]|$))+|\[\^?(]?)(?:[^\\\]]+|\\(?:[\S\s]|$))*]?/g,
	capturingGroup: /(?:[^[(\\]+|\\(?:[\S\s]|$)|\[\^?]?(?:[^\\\]]+|\\(?:[\S\s]|$))*]?|\((?=\?))+|\((?:<([$\w]+)>)?/g,
	namedBackreference: /(?:[^\\[]+|\\(?:[^k]|$)|\[\^?]?(?:[^\\\]]+|\\(?:[\S\s]|$))*]?|\\k(?!<[$\w]+>))+|\\k<([$\w]+)>(\d*)/g,
	replacementVariable: /(?:[^$]+|\$(?![1-9$&`']|{[$\w]+}))+|\$(?:([1-9]\d*|[$&`'])|{([$\w]+)})/g
};

XRegExp.overrideNative = function () {
	/* Override the global RegExp constructor/object with the XRegExp constructor. This precludes accessing
	properties of the last match via the global RegExp object. However, those properties are deprecated as
	of JavaScript 1.5, and the values are available on RegExp instances or via RegExp/String methods. It also
	affects the result of (/x/.constructor == RegExp) and (/x/ instanceof RegExp), so use with caution. */
	RegExp = XRegExp;
};

/* indexOf method from Mootools 1.11; MIT License */
Array.prototype.indexOf = Array.prototype.indexOf || function (item, from) {
	var len = this.length;
	for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++) {
		if (this[i] === item) return i;
	}
	return -1;
};

You can download it, or get the packed version (2.7 KB).

XRegExp has been tested in IE 5.5–7, Firefox 2.0.0.4, Opera 9.21, Safari 3.0.2 beta for Windows, and Swift 0.2.

Finally, note that the XRE object from v0.1 has been removed. XRegExp now only creates one global variable: XRegExp. To permanently override the native RegExp constructor/object, you can now run XRegExp.overrideNative();

Continue Reading
Click to comment

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注

0006

springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Freign,Hystrix)

这篇文章主要介绍了springboot2.0和springcloud Finchley版项目搭建(包含eureka,gateWay,Freign,Hystrix),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

Published

on

前段时间spring boot 2.0发布了,与之对应的spring cloud Finchley版本也随之而来了,两者之间的关系和版本对应详见我这边文章:spring boot和spring cloud对应的版本关系

项目地址:spring-cloud-demo

spring boot 1.x和spring cloud Dalston和Edgware版本搭建的微服务项目现在已经很流行了,现在很多企业都已经在用了,这里就不多说了。

使用版本说明:

  • spring boot 2.0.x
  • spring cloud Finchley.RELEASE
  • jdk 1.8
  • maven 3.9

Eureka 注册中心

spring cloud Finchley在支持spring 2.0时修改了eureka的jar包,把之前netflix系列的jar引入的时候都加上了netflix

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

新建一个eureka-service注册中心服务,pom.xml 文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.sunvalley</groupId>
  <artifactId>eureka-service</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>eureka-service</name>
  <description>Demo project for Spring Boot</description>
 
  <parent>
   <groupId>com.sunvalley.springcloud</groupId>
   <artifactId>spring-cloud-demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </parent>
 
  <properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
   <java.version>1.8</java.version>
   <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  </properties>
 
  <dependencies>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
   </dependency>
  </dependencies>
 
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
 
  <build>
   <plugins>
     <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
     </plugin>
   </plugins>
  </build>
 
</project>

eureka-service服务的配置文件,application.yml

spring:
 application:
  name: eureka-service
 
server:
 port: 5000
 
eureka:
 instance:
  hostname: localhost
 client:
  registerWithEureka: false
  fetchRegistry: false
  serviceUrl:
   defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

eureka-service服务的配置文件,EurekaServiceApplication

@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {
 
  public static void main(String[] args) {
   SpringApplication.run(EurekaServiceApplication.class, args);
  }
}

使用IDEA启动eureka-service服务,本地访问 http://localhost:5000/ 即可看到注册中心内容。

服务消费者consumer和提供者provider

实际工作中大多数一个服务既是其它服务的消费者又有可能是服务的提供者,所以我们也就不用刻意的取区分开。

新建一个order-service服务,pom.xml文件

注意:除了eureka-client,openfeign等jar包外,记得引入spring-boot-starter-web不然会出现启动报错。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.sunvalley</groupId>
  <artifactId>order-service</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>order-service</name>
  <description>Demo project for Spring Cloud</description>
 
  <parent>
   <groupId>com.sunvalley.springcloud</groupId>
   <artifactId>spring-cloud-demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </parent>
 
  <properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
   <java.version>1.8</java.version>
   <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  </properties>
 
  <dependencies>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter</artifactId>
   </dependency>
 
   <!-- eureka 里面已经包含 ribbon 了, 所以不用单独添加, ribbon依赖, 点击依赖就去看就知道了 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-openfeign</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <optional>true</optional>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
   </dependency>
  </dependencies>
 
  <dependencyManagement>
   <dependencies>
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
     </dependency>
   </dependencies>
  </dependencyManagement>
 
  <build>
   <plugins>
     <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
     </plugin>
   </plugins>
  </build>
 
  <repositories>
   <repository>
     <id>spring-snapshots</id>
     <name>Spring Snapshots</name>
     <url>https://repo.spring.io/snapshot</url>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
   </repository>
   <repository>
     <id>spring-milestones</id>
     <name>Spring Milestones</name>
     <url>https://repo.spring.io/milestone</url>
     <snapshots>
      <enabled>false</enabled>
     </snapshots>
   </repository>
  </repositories>
 
  <pluginRepositories>
   <pluginRepository>
     <id>spring-snapshots</id>
     <name>Spring Snapshots</name>
     <url>https://repo.spring.io/snapshot</url>
     <snapshots>
      <enabled>true</enabled>
     </snapshots>
   </pluginRepository>
   <pluginRepository>
     <id>spring-milestones</id>
     <name>Spring Milestones</name>
     <url>https://repo.spring.io/milestone</url>
     <snapshots>
      <enabled>false</enabled>
     </snapshots>
   </pluginRepository>
  </pluginRepositories>
 
 
</project>

order-service服务的配置文件,application.yml

spring:
 application:
  name: order-service
 
server:
 port: 5100
 
eureka:
 client:
  service-url:
   defaultZone: http://localhost:5000/eureka/
 
 
feign:
 hystrix:
  enabled: true

order-service服务的启动类,OrderServiceApplication

@EnableCircuitBreaker
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class OrderServiceApplication {
 
  public static void main(String[] args) {
   SpringApplication.run(OrderServiceApplication.class, args);
  }
}

order-service服务的接口,OrderController

@RestController
@RequestMapping("/order")
public class OrderController {
  @Value("${server.port}")
  private String port;
 
  /**
   * 获取服务端口号
   * @return
   */
  @GetMapping("/getOrderPort")
  public String getOrderPort() {
    return "order-service port:" + port;
  }
}

新建一个user-service服务,pom.xml文件跟order-service一样的这里就不列出来了

user-service服务的配置文件,application.yml

spring:
 application:
  name: user-service
 
server:
 port: 5200
 
eureka:
 client:
  service-url:
   defaultZone: http://localhost:5000/eureka/
 
feign:
 hystrix:
  enabled: true

user-service服务的启动类也跟order-service一样的

在user-service工程目录下新建一个package,再新建一个OrderRemote接口使用feign调用order-service的方法

@FeignClient(value = "order-service", fallback = OrderRemoteHystrix.class)
public interface OrderRemote {
  @GetMapping("/order/getOrderPort")
  String getOrderPort();
}

熔断机制提示类

@Component
public class OrderRemoteHystrix implements OrderRemote {
  @Override
  public String getOrderPort() {
    return "order service 调用失败!";
  }
} 

user-service服务的接口,UserController

@RestController
@RequestMapping("/user")
public class UserController {
  @Autowired
  OrderRemote orderRemote;
  @Value("${server.port}")
  String port;
 
  /**
   * 获取用户服务的端口
   * @return
   */
  @GetMapping("/getUserPort")
  public String getUserPort() {
    return "user-service port:" + port;
  }
 
  /**
   * 获取订单服务的端口
   * @return
   */
  @GetMapping("/getOrderPort")
  public String getOrderPort() {
    return "user-order-service port:" + orderRemote.getOrderPort();
  }
}

测试,分别启动eureka-service,order-service,user-service就可以在注册中心看到服务

在浏览器访问 http://localhost:5100/order/getOrderPort 调用order-service的接口

 在浏览器访问 http://localhost:5200/user/getOrderPort 通过user-service使用feign声明式调用order-service的接口

关闭order-service,再次调用就会发现熔断机制起了作用

spring cloud gateway

Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代Netflix ZUUL,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。

新建一个gateway-service服务,pom.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.sunvalley</groupId>
  <artifactId>gateway-service</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>gateway-service</name>
  <description>Demo project for Spring Cloud</description>
 
  <parent>
   <groupId>com.sunvalley.springcloud</groupId>
   <artifactId>spring-cloud-demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </parent>
 
  <properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
   <java.version>1.8</java.version>
   <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  </properties>
 
  <dependencies>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-gateway</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
     <scope>runtime</scope>
   </dependency>
   <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <optional>true</optional>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
   </dependency>
  </dependencies>
 
  <dependencyManagement>
   <dependencies>
     <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
     </dependency>
   </dependencies>
  </dependencyManagement>
 
  <build>
   <plugins>
     <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
     </plugin>
   </plugins>
  </build>
 
 
</project>

gateway-service的配置文件,application.yml

spring:
 application:
  name: gateway-service
 cloud:    # spring cloud gateway 路由配置方式
  gateway:
   discovery:   #是否与服务发现组件进行结合,通过 serviceId(必须设置成大写) 转发到具体的服务实例。默认为false,设为true便开启通过服务中心的自动根据 serviceId 创建路由的功能。
    locator:   #路由访问方式:http://Gateway_HOST:Gateway_PORT/大写的serviceId/**,其中微服务应用名默认大写访问。
     enabled: true
   routes:
   - id: 163           #网关路由到网易官网
    uri: http://www.163.com/
    predicates:
     - Path=/163/**
#   - id: ORDER-SERVICE      #网关路由到订单服务order-service
#    uri: lb://ORDER-SERVICE
#    predicates:
#     - Path=/ORDER-SERVICE/**
#   - id: USER-SERVICE      #网关路由到用户服务user-service
#    uri: lb://USER-SERVICE
#    predicates:
#     - Pach=/USER-SERVICE/**
 
server:
 port: 5001
 
 
logging:
 level:
  org.springframework.cloud.gateway: trace
  org.springframework.http.server.reactive: debug
  org.springframework.web.reactive: debug
  reactor.ipc.netty: debug
 
 
eureka:
 client:
  service-url:
   defaultZone: http://localhost:5000/eureka/
 
feign:
 hystrix:
  enabled: true

gateway-service的启动类,Application

package com.sunvalley.demo;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
 
@SpringBootApplication
public class GatewayServiceApplication {
 
  /**
  * spring cloud gateway 配置方式之一,启动主程序配置,还有一种是配置文件配置
  * @param builder
  * @return
  */
  @Bean
  public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
   return builder.routes()
      .route(r -> r.path("/qq/**")
           .and()
           .uri("http://www.qq.com/"))
      .build();
  }
 
  public static void main(String[] args) {
   SpringApplication.run(GatewayServiceApplication.class, args);
  }
}

通过上面我们可以看到,gateway网关路由配置有两种方式:

1.通过@Bean自定义RouteLocator,在启动主类Application中配置

2.在配置文件yml中配置

这两种方式都可以实现网关路由是等价的,但是通常项目开发中会使用配置文件yml方式。

运行测试:

访问 http://localhost:8080/qq,路由转发到 http://www.qq.com
访问http://localhost:8080/163, 路由转发到 http://www.163.com

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本小子。

Continue Reading

0006

Vue3.0结合bootstrap创建多页面应用

这篇文章主要介绍了Vue3.0结合bootstrap创建多页面应用,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

Published

on

本人主要做c#winform应用,刚接触vue,发现用vue做单页面应用的比较多,多页面的资料很少,特别是用vue3.0版本做多页面的资料,更少,所以自己整理一下,结合bootstrap和jquery,也可以给有需要的同行做个参考。

1.首先用 vue --version 命令查一下安装的vue-cli版本,要3.x版本,我原先装的是2.9.6版本的

2.卸载vue-cli,执行命令:cnpm uninstall -g vue-clinpm uninstall -g vue-cli ,执行完就好了

3.安装vue-cli3.x版本:cnpm install -g @vue/cli

4.安装完毕后,查看一下版本:默认装的最新版,3.8.2

5.切换到你的项目文件夹,准备创建项目吧,cmd命令操作就好了

6.执行 vue create <项目名称>创建项目吧,我这里直接用j做测试好了

7.新版的项目创建,会有选项可以配置,这里就不做赘述,百度有哈,一路默认回车也可以

从别人那里看到的配置图,可以参考:

8.项目安装完了会有如下提示

9.执行cd j跳转到项目路径内

10.执行 npm run serve命令,即可启动服务

可以看到有两个地址,第一个是用于本地测试,第二个是用于局域网测试,复制其中一个到浏览器运行即可。

总结

以上所述是小编给大家介绍的Vue3.0结合bootstrap创建多页面应用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本小子网站的支持!

Continue Reading

0006

asp.net Core3.0区域与路由配置的方法

这篇文章主要给大家介绍了关于asp.net Core3.0区域与路由配置的方法,文中通过示例代码介绍的非常详细,对大家学习或者使用asp.net Core3.0具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

Published

on

By

在ASP.NET Core 3.0中路由配置和2.0不一样了

一、MVC 服务注册

ASP.NET Core 3.0 添加了用于注册内部的 MVC 方案的新选项Startup.ConfigureServices。

三个新的顶级扩展方法与 MVC 方案上IServiceCollection可用。 模板使用这些新方法,而不是UseMvc。 但是,AddMvc继续像它已在以前的版本。

下面的示例将添加对控制器和与 API 相关的功能,但不是视图或页面的支持。 API 模板使用此代码:

public void ConfigureServices(IServiceCollection services)
{
 services.AddControllers();
}

下面的示例将添加对控制器、 与 API 相关的功能,和视图,但不是页面的支持。 Web 应用程序 (MVC) 模板使用此代码:

public void ConfigureServices(IServiceCollection services)
{
 services.AddControllersWithViews();
}

下面的示例添加支持 Razor 页面和最小控制器支持。 Web 应用程序模板使用此代码:

public void ConfigureServices(IServiceCollection services)
{
 services.AddRazorPages();
}

此外可以组合的新方法。 下面的示例是等效于调用AddMvcASP.NET Core 2.2 中:

public void ConfigureServices(IServiceCollection services)
{
 services.AddControllers();
 services.AddRazorPages();
}

二、Startup.Configure配置

一般不建议:

添加UseRouting。

如果该应用程序调用UseStaticFiles,将置于UseStaticFiles之前 UseRouting。

如果应用使用身份验证/授权功能,如AuthorizePage或[Authorize],将对UseAuthentication并UseAuthorization后 UseRouting。

如果应用使用CORS功能,如[EnableCors],将放置UseCors下一步。

替换UseMvc或UseSignalR与UseEndpoints。

以下是一种Startup.Configure典型的 ASP.NET Core 2.2 应用中:

public void Configure(IApplicationBuilder app)
{
 ...

 app.UseStaticFiles();
 
 app.UseAuthentication();

 app.UseSignalR(hubs =>
 {
  hubs.MapHub<ChatHub>("/chat");
 });

 app.UseMvc(routes =>
 {
  routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
 });
}

现在的控制器映射内发生UseEndpoints。

添加MapControllers如果应用使用属性路由。 由于路由包括对许多框架在 ASP.NET Core 3.0 或更高版本的支持,添加属性路由的控制器是参加。

将为以下内容:

MapRoute 使用 MapControllerRoute

MapAreaRoute 使用 MapAreaControllerRoute

由于路由现在包括对不止是 MVC 的支持,已更改了术语进行明确说明他们所做的这些方法。 如传统路由MapControllerRoute / MapAreaControllerRoute / MapDefaultControllerRoute它们要添加的顺序应用。 将第一位更具体的路由 (如某一区域的路由)。

如下示例中:

  • MapControllers 添加了对属性路由的控制器支持。
  • MapAreaControllerRoute 将控制器的传统路由添加区域。
  • MapControllerRoute 添加控制器的常规路由。

现在映射 Razor 页面内发生UseEndpoints。

添加MapRazorPages如果应用使用 Razor 页面。 由于终结点路由包括对许多框架的支持添加 Razor 页面现在参加。

更新后asp.netCore3.0中Startup.Configure代码:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
   if (env.IsDevelopment())
   {
    app.UseDeveloperExceptionPage();
   }
   else
   {
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
   }

   app.UseHttpsRedirection();
   app.UseStaticFiles();

   app.UseCookiePolicy();

   app.UseRouting();

   app.UseAuthorization();

   app.UseEndpoints(endpoints =>
   {
    endpoints.MapControllerRoute(
     name: "default",
     pattern: "{controller=Home}/{action=Index}/{id?}");

    endpoints.MapAreaControllerRoute(
     name: "areas", "areas",
     pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
    endpoints.MapRazorPages();
   });
  }

如果要进行分区路由,需要在控制器Controller头加Area和Route标签,否则不能像在asp.netCore2.0中自动路由控制器和Action。示例代码如下:

namespace WebApplication1.Areas.CMS.Controllers
{
 [Area("CMS")]
 [Route("CMS/[controller]/[action]")]
 public class NewsController : Controller
 {
  public IActionResult Index()
  {
   return View();
  }
  public IActionResult List()
  {
   return View();
  }
 }
}

这两句一定要加[Area("CMS")]、[Route("CMS/[controller]/[action]")]

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本小子的支持。

Continue Reading

趋势

Copyright © 2017 Zox News Theme. Theme by MVP Themes, powered by WordPress.