• Category Archives: 编程语言

Vue3-cli/Ant-design-pro-vue修改运行在子路径上

版本依赖情况:

PS D:\suidian-yuexi\admin> npm list vue
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
vue-antd-pro@3.0.2 D:\suidian-yuexi\admin
+-- @ant-design-vue/pro-layout@1.0.13
| +-- vue-container-query@0.1.0
| | `-- vue@2.7.3 deduped
| +-- vue-copy-to-clipboard@1.0.3
| | `-- vue@2.7.3 deduped
| `-- vue@2.7.3 deduped
+-- @vue/cli-plugin-babel@4.5.19
| `-- @vue/babel-preset-app@4.5.19
|   +-- @vue/babel-preset-jsx@1.3.0
|   | `-- vue@2.7.3 deduped
|   `-- vue@2.7.3 deduped
+-- @vue/cli-plugin-unit-jest@4.5.19
| `-- vue-jest@3.0.7
|   `-- vue@2.7.3 deduped
+-- @vue/test-utils@1.3.0
| `-- vue@2.7.3 deduped
+-- ant-design-vue@1.7.8
| +-- @ant-design/icons-vue@2.0.0
| `-- vue@2.7.3 deduped
+-- viser-vue@2.4.8
| `-- vue@2.7.3 deduped
+-- vue-svg-component-runtime@1.0.1
| `-- vue@2.7.3 deduped
+-- vue-svg-icon-loader@2.1.1
| +-- vue-svg-component-builder@2.0.3
| | `-- vue@2.7.3 deduped
| `-- vue@2.7.3 deduped
+-- vue@2.7.3
`-- vuex@3.6.2
  `-- vue@2.7.3 deduped

PS D:\suidian-yuexi\admin> npm run serve
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

> vue-antd-pro@3.0.2 serve
> vue-cli-service serve

 

1、修改项目运行路径,打开vue.config.js文件,在vueConfig的json中增加:

publicPath: "/admin2/",

2、修改路由的基础路径,打开index.js文件,在createRouter的new Router对象时传入的json增加:

base: '/admin2',

3、重启服务即可

安装RabbitMQ

一、安装ERLANG

RabbitMQ依赖语言开发包ErLang,到http://www.erlang.org/downloads下载windows版本并安装,安装后需要配置环境变量:

1、增加系统变量:ERLANG_HOME=C:\Program Files\erl-23.0

2、在Path系统变量中增加:%ERLANG_HOME%\bin

然后在CMD中输入指令“erl”即可验证是否配置成功:

111

 

 

二、安装RabbitMQ

到https://www.rabbitmq.com/download.html下载windows版本病安装,安装后需要配置环境变量:

1、增加系统变量:RABBITMQ_SERVER=C:\Program Files\RabbitMQ Server\rabbitmq_server-3.8.3

2、在Path系统变量中增加:%RABBITMQ_SERVER%\sbin

三、激活RabbitMQ的WEB管理界面(可选)

打开CMD执行命令:rabbitmq-plugins.bat enable rabbitmq_management

222

执行成功后重启RabbitMQ服务(可到“服务”中重启,或在任务栏中也能找到相关菜单),然后访问网址:http://localhost:15672/

333

默认用户名/密码:guest/guest

登录后出现如下界面:

444

12306火车购票优先选下铺的方法

1、先在页面空白地方右击“审查元素”

2、在页面代码编辑区域搜索“优先席别”

3、在网页页面出现席别选择,此时选择硬卧,然后右键“硬卧”选项并审查元素

4、在硬卧的span标签后增加以下代码即可:

<select name="passsenger_1_seat-detail_select" id="passenger_1_seat_detail_select" onchange="setseatDetail('1')">
    <option value="0">随机</option>
    <option value="3">上铺</option>
    <option value="2">中铺</option>
    <option value="1">下铺</option>
</select>

 

uni-app使用H5+调起安卓原生Activity

方式一,无需回调结果:

let platform = uni.getSystemInfoSync().platform;
if (platform == 'android') {

	//获取宿主上下文
	var main = plus.android.runtimeMainActivity();

	var openUrl = "sway://openapi:8848/oauthLogin?appKey=" + this.appKey + "&pkgName=" + this
		.pkgName;
	console.log(openUrl);

	//通过反射获取Android的Uri对象
	var Uri = plus.android.importClass("android.net.Uri");
	var uri = Uri.parse(openUrl);

	//通过反射获取Android的Intent对象
	var Intent = plus.android.importClass("android.content.Intent");
	var intent = plus.android.newObject("android.content.Intent", Intent.ACTION_VIEW, uri);

	main.startActivity(intent);

} else {
	uni.showToast({
		title: "仅支持安卓客户端",
		icon: 'none',
		duration: 2000
	});
}

 

方式二,需要回调结果:

let platform = uni.getSystemInfoSync().platform;
if (platform == 'android') {

	//获取宿主上下文
	var main = plus.android.runtimeMainActivity();

	var openUrl = "sway://openapi:8848/oauthLogin?appKey=" + this.appKey + "&pkgName=" + this
		.pkgName;
	console.log(openUrl);

	//通过反射获取Android的Uri对象
	var Uri = plus.android.importClass("android.net.Uri");
	var uri = Uri.parse(openUrl);

	//通过反射获取Android的Intent对象
	var Intent = plus.android.importClass("android.content.Intent");
	var intent = plus.android.newObject("android.content.Intent", Intent.ACTION_VIEW, uri);

	//请求码保证了,开始的新界面和返回的是同一个操作
	var CODE_REQUEST = 1000

	main.startActivityForResult(intent, CODE_REQUEST);

	//设置原生界面返回后的回调操作
	main.onActivityResult = function(requestCode, resultCode, data) {
		if (requestCode == CODE_REQUEST) {
			//alert(requestCode); //这个是正确的 1000  
			//alert(resultCode); //始终都是0  
			//alert(data); //弹出 undefined
			plus.android.importClass(data);
			var bundle = data.getExtras();
			plus.android.importClass(bundle);
			let result = eval('(' + bundle.getString("data") + ')');
		}
	}
} else {
	uni.showToast({
		title: "仅支持安卓客户端",
		icon: 'none',
		duration: 2000
	});
}

 

uni-app使用H5+调用原生的安卓方法

uni-app代码:

//获取当前Activity,使用H5的方式调用android原生方法,从而启动activity
var main = plus.android.runtimeMainActivity();
//invoke: 调用对象(类对象/实例对象)的方法,调用Android原生方法        
let result = plus.android.invoke('cn.com.sway.test.TestActivity', 'test', main);
//弹窗显示返回结果
uni.showToast({
	title: "调用原生activity返回的结果为:\n" + result,
	icon: 'none',
	duration: 2000
});

安卓代码:

package cn.com.sway.test;

import android.app.Activity;
import android.widget.Toast;

import java.util.Random;

public class TestActivity extends Activity {

    public static String test(Activity activity, String text){
        Toast.makeText(activity, "来自uni-app的消息:"+text, Toast.LENGTH_SHORT).show();
        return "来自android的消息:"+(new Random()).nextInt();
    }

}

注意,被调用的安卓方法必须为static静态方法,否则无法被调起。

Javascript:Json对象与Json字符串的转化

1.jQuery插件支持的转换方式

$.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象

 

2.浏览器支持的转换方式(Firefox,chrome,opera,safari,ie9,ie8)等浏览器:

JSON.stringify(obj)将JSON转为字符串。JSON.parse(string)将字符串转为JSON格式;

var a={“name”:”tom”,”sex”:”男”,”age”:”24″}; 
var b='{“name”:”Mike”,”sex”:”女”,”age”:”29″}’; 
var aToStr=JSON.stringify(a); 
var bToObj=JSON.parse(b); 
alert(typeof(aToStr));  //string 
alert(typeof(bToObj));//object

 

3.Javascript支持的转换方式: 
eval(‘(‘ + jsonstr + ‘)’); //可以将json字符串转换成json对象,注意需要在json字符外包裹一对小括号 
注:ie8(兼容模式),ie7和ie6也可以使用eval()将字符串转为JSON对象,但不推荐这些方式,这种方式不安全eval会执行json串中的表达式。

 

4.JSON官方的转换方式: 
http://www.json.org/提供了一个json.js,这样ie8(兼容模式),ie7和ie6就可以支持JSON对象以及其stringify()和parse()方法; 
可以在https://github.com/douglascrockford/JSON-js上获取到这个js,一般现在用json2.js。

SpringBoot2.1.1使用Zuul创建SpringCloud微服务Gateway网关

1、在新的SpringBoot项目中的pom.xml引入如下依赖:

		<!-- 引入Zuul starter -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter</artifactId>
		</dependency>
		<!-- 连接Eureka -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

2、在pom.xml的project中加入SpringCloud的版本管理配置:

	<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>

3、编辑配置文件application.propertites:

spring.application.name = zero4j-zuul-gateway

eureka.client.serviceUrl.defaultZone = http://admin:123456@localhost:8761/eureka/
eureka.instance.instance-id = ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
eureka.instance.prefer-ip-address = true

#只要访问以/api/v1/areas开头的多层目录都可以路由到服务名为zero4j-provider-area的服务上.
zuul.routes.zero4j-provider-area.path = /api/v1/areas
zuul.routes.zero4j-provider-area.service-id = zero4j-provider-area
zuul.routes.zero4j-provider-area.stripPrefix = false

server.port = 4000

4、最后在application启动类中加入@EnableDiscoveryClient和@EnableZuulProxy注解:

package com.zero4j;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableDiscoveryClient
@EnableZuulProxy
@SpringBootApplication
public class Zero4jZuulGatewayApplication {

	public static void main(String[] args) {
		SpringApplication.run(Zero4jZuulGatewayApplication.class, args);
	}

}

 

SpringBoot2.1.1微服务架构引入SpringCloudSecurity安全认证

1、在eureka服务器的pom.xml中引入依赖:

	<!-- Spring Cloud Security 依赖 -->
	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

2、创建密码安全认证密码匹配规则类MyPasswordEncoder.java:

package com.zero4j.config;

import org.springframework.security.crypto.password.PasswordEncoder;

public class MyPasswordEncoder implements PasswordEncoder {

	@Override
    public String encode(CharSequence charSequence) {
        return charSequence.toString();
    }

    @Override
    public boolean matches(CharSequence charSequence, String s) {
        return s.equals(charSequence.toString());
    }
    
}

3、在eureka服务器中创建配置类SecurityConfig.java:

package com.zero4j.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
	@Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }
    
    //@Autowired
    //BCryptPasswordEncoder passwordEncoder;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    	
    	//以前可以不指定PasswordEncoder,但是新的SpringBoot依赖的SpringCloudScurity需要了
    	//auth.inMemoryAuthentication().withUser("admin").password("123456").roles("ADMIN");
    	//这样,密码以明文的方式进行匹配
    	auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("admin").password("123456").roles("ADMIN");
    	//auth.inMemoryAuthentication().withUser("admin").password(passwordEncoder.encode("123456")).roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

4、启动服务,访问eureka服务中心,使用admin:123456进行登录,成功进入eureka控制台

5、对微服务提供者的application.propertites说引用的eureka服务中心地址的前面加入“admin:123456@”,如:http\://admin:123456@localhost\:8761/eureka/ ,然后启动即可~

SpringBoot2.1.1使用SpringCloud的Feign调用Eureka微服务并开启Hystrix熔断机制

1、先在pom.xml中引入如下依赖:

	<!--Netflix Eureka依赖-->
	<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-ribbon</artifactId>
	</dependency>
	<!--springcloud整合的openFeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2、在pom.xml中的<project></project>之间增加以下关于springcloud的版本管理:

	<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>

3、在application.propertites中增加如下配置:

#feign的配置,连接超时及读取超时配置
feign.client.config.default.connectTimeout=5000
feign.client.config.default.readTimeout=5000
feign.client.config.default.loggerLevel=basic
#开启熔断功能
feign.hystrix.enabled=true

4、在application启动入口增加@EnableFeignClients注解:

@EnableFeignClients
@ImportResource("classpath:hibernate.xml")
@SpringBootApplication
public class Zero4jApplication extends SpringBootServletInitializer{
	
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(Zero4jApplication.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(Zero4jApplication.class, args);
	}

}

5、创建FeignClient客户端:

package com.zero4j.controller.api.v1;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value="zero4j-provider-area", fallback=AreasApiHystrixV1.class)
public interface AreasApiFeignClientV1 {

	@PatchMapping(value="api/v1/areas/{id}")
	String update(
			@PathVariable(value="id") String id,
			@RequestParam(value="token",required=false) String token,
			@RequestParam(value="name",required=false) String name,
			@RequestParam(value="parentId",required=false) String parentId,
			@RequestParam(value="hasChildren",required=false) Boolean hasChildren
		);

}

6、编写调用微服务的Controller:

package com.zero4j.controller.api.v1;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.zero4j.model.area.service.AreaService;


@RestController
@RequestMapping("/api/v1/areas")
public class AreasApiControllerV1 {

	@Autowired
	private AreaService areaService;
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@Autowired
	private RestTemplate restTemplate;
	
	@PatchMapping(value="/{id}")
	public ResponseEntity update(HttpServletRequest request,HttpServletResponse response,
			@PathVariable(value="id") String id,
			@RequestParam(required=false) String token,
			@RequestParam(required=false) String name,
			@RequestParam(required=false) String parentId,
			@RequestParam(required=false) Boolean hasChildren
		){
		
		return ResponseEntity.ok((String)areaApiFeignClient.update(id,token,name,parentId,hasChildren));
	}
	

}

7、编写熔断处理方法:

package com.zero4j.controller.api.v1;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

@Component
public class AreasApiHystrixV1 implements AreasApiFeignClientV1 {

	@Override
	public String update(String id, String token, String name, String parentId, Boolean hasChildren) {
		JSONObject out = new JSONObject();
		out.put("status", 400);
		out.put("message", "update服务异常");
		out.put("debug", "update服务异常");
		return out.toString();
	}

}

8、启动服务,访问对应的api,分别在微服务开启、关闭的时候调用API,查看结果即可。

微服务的技术栈

微服务条目 落地技术
服务开发 SpringBoot,Spring,SpringMVC
服务配置与管理 Netflix公司的Archaius、阿里的Diamond等
服务注册与发现 Eureka、Consul、Zookeeper等
服务调用 Rest、RPC、gRPC
服务熔断器 Hystrix、Envoy等
负载均衡 Ribbon、Nginx等
服务接口调用(客户端调用服务的简化工具) Feign等
消息队列 Kafka、RabbitMQ、ActiveMQ等
服务配置中心管理 SpringCloudConfig、Chef等
服务路由(API网关) Zuul等
服务监控 Zabbix、Nagios、Metrics、Specatator等
全链路追踪 Zipkin、Brave、Dapper等
服务部署 Docker、OpenStack、Kubernetes等
数据流操作开发包 SpringCloud Stream(封装与Redis,Rabbit,Kafka等发送接收消息)
事件消息总线 SpringCloud Bus

 

close