1、安裝cmake (可能需要安裝 yum install gcc-c++)
2、安裝yum install ncurses-devel -y
3.創(chuàng)建用戶和組 groupadd mysql
useradd mysql -s /sbin/nologin -M -g mysql
4、tar xf mysql-5.5.32.tar.gz
5、進(jìn)入MySQL目錄,(可能需要 yum install bison)執(zhí)行
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1 -DWITH_FAST_MUTEXES=1 -DWITH_ZLIB=bundled -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DWITH_DEBUG=0 -DMYSQL_TCP_PORT=3306
6、make && make install
7、cp mysql-5.5.32/support-files/my-small.cnf /etc/my.cnf
8、添加環(huán)境變量 export PATH=/usr/local/mysql/bin:$PATH
9、授權(quán) chown -R mysql.mysql /usr/local/mysql/data/
10、chmod -R 1777 /tmp/
11、在MySQL的安裝目錄下的scripts文件夾下,執(zhí)行./mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql
12、負(fù)責(zé)MySQL啟動(dòng)命令 cp support-files/mysql.server /etc/init.d/mysqld
13、授權(quán)chmod +x /etc/init.d/mysqld
14、啟動(dòng)MySQL /etc/init.d/mysqld start
15、查看運(yùn)行的進(jìn)程 netstat -lntup|grep 3306
16、刪除無用的用戶和host,select user,host from mysql.user ;
刪除test數(shù)據(jù)庫(kù)
17、刪除root用戶,添加別的數(shù)據(jù)庫(kù)管理員
delete from mysql.user;
grant all privileges on *.* to system@'localhost' identified by 'yjw' with grant option;
/usr/local/mysql//bin/mysqladmin -u root password 'new-password' --設(shè)置密碼
/usr/local/mysql//bin/mysqladmin -u root -h bogon password 'root' --修改密碼
linux下GTK+的一鍵安裝和配置:(fedora16和centos下配置成功)
必要組件:
yum install gtk2 gtk2-devel gtk2-devel-docs
可選組件:
yum install gnome-devel gnome-devel-docs
A Java Runtime Environment (JRE) or Java Development Kit (JDK)
must be available in order to run Eclipse. No Java virtual machine
was found after searching the following locations:
/home/injavawetrust/program/eclipse/jre/bin/java
java in your current PATH
解決辦法是在終端進(jìn)入你的eclipse目錄,然后輸入:
mkdir jre
cd jre
ln -s 你的JDK目錄/bin bin
方法一:
如果你是用命令行提交的,可以用以下命令設(shè)置臨時(shí)環(huán)境變量GIT_SSL_NO_VERIFY。
Windows下:
set GIT_SSL_NO_VERIFY=true git push
Linux下:
env GIT_SSL_NO_VERIFY=true git push
設(shè)置好之后,然后用Git提交。
當(dāng)然,你也可以把GIT_SSL_NO_VERIFY設(shè)置成非臨時(shí)環(huán)境變量,這樣就不用每次提交都要執(zhí)行上面的命令了。
方法二:
你也可以在命令行執(zhí)行以下命令,之后再提交。
git config --global http.sslVerify false
以上兩個(gè)方法,親測(cè)有效,建議第二個(gè),直接去掉git的ssl驗(yàn)證
1、不在啟動(dòng)類同級(jí)的包目錄中新建ribbon配置類
@Configuration
public class TestConfiguration {
@Autowired
IClientConfig config;
@Bean
public IRule ribbonRule(IClientConfig config) {
return new RandomRule();
}
}
在啟動(dòng)類中添加注解@RibbonClient
@SpringBootApplication
@EnableEurekaClient //針對(duì)Eureka服務(wù)注冊(cè)使用
//@EnableDiscoveryClient //可以對(duì)其他服務(wù)注冊(cè)軟件使用
@RibbonClient(name="a-microservice-provider-user",configuration=TestConfiguration.class)
public class ConsumerMovieRibbonApplication {
@Bean
@LoadBalanced//客戶端負(fù)載均衡,先把服務(wù)提供這所有的節(jié)點(diǎn)讀取到ribbon注冊(cè)表中,默認(rèn)輪詢請(qǐng)求服務(wù)
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerMovieRibbonApplication.class, args);
}
}
3.在controller中添加方法
@GetMapping("/movie/{userid}")
public TUser test2(@PathVariable(name="userid") String userId) {
//服務(wù)的自動(dòng)發(fā)現(xiàn),不用配置死的IP和端口,只有在RestTemplate添加了@LoadBalanced接口,才能使用應(yīng)用名稱訪問
return restTemplate.getForObject("http://a-microservice-provider-user/users/"+userId, TUser.class);
}
4、啟動(dòng)服務(wù)發(fā)現(xiàn)服務(wù)eureka和服務(wù)提供類,調(diào)用目標(biāo)方法,可以成功 調(diào)用。
一、服務(wù)端的搭建
1、在pom文件中添加eureka服務(wù)依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、編寫application.yml 配置
security:
basic:
enabled: true
user:
name: user
password: password123
server:
port: 8761
eureka:
client:
register-with-eureka: false #只把此服務(wù)當(dāng)成eurekaservice,不要當(dāng)成client
fetch-registry: false #只把此服務(wù)當(dāng)成eurekaservice,不要當(dāng)成client
service-url:
defaultZone: http://user:password123@localhost:8761/eureka
3、在啟動(dòng)類上添加注解
@SpringBootApplication
@EnableEurekaServer
就可以啟動(dòng)服務(wù)發(fā)現(xiàn)的服務(wù)端程序了。
二、客戶端的搭建
1、在pom文件中添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
2、編寫application.yml 配置
server:
port: 7901
session-timeout: 30
tomcat.max-threads: 0
tomcat.uri-encoding: UTF-8
spring:
application:
name: a-microservice-consumer-movie
logging:
level:
root: INFO
com.example.demo: debug
eureka:
client:
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
instance: #eureka管理頁(yè)面客戶端服務(wù)的地址顯示實(shí)際IP
prefer-ip-address: true #默認(rèn)是false
3、在啟動(dòng)類添加注解
@SpringBootApplication
@EnableEurekaClient //針對(duì)Eureka服務(wù)注冊(cè)使用
//@EnableDiscoveryClient //可以對(duì)其他服務(wù)注冊(cè)軟件使用
這樣客戶端配置完畢,先啟動(dòng)服務(wù)端,再啟動(dòng)客戶端,服務(wù)端就可以自動(dòng)發(fā)現(xiàn)客戶端服務(wù)了。
springboot的應(yīng)用打包默認(rèn)是打成jar包,并且如果是web應(yīng)用的話,默認(rèn)使用內(nèi)置的tomcat充當(dāng)servlet容器,但畢竟內(nèi)置的tomcat有時(shí)候并不滿足我們的需求,如有時(shí)候我們想集群或者其他一些特性優(yōu)化配置,因此我們需要把springboot的jar應(yīng)用打包成war包,并能夠在外部tomcat中運(yùn)行。
很多人會(huì)疑問,你直接打成war包并部署到tomcat的webapp下不就行了么?No,springboot的如果在類路徑下有tomcat相關(guān)類文件,就會(huì)以內(nèi)置tomcat啟動(dòng)的方式,經(jīng)過你把war包扔到外置的tomcat的webapp文件下啟動(dòng)springBoot應(yīng)用也無事于補(bǔ)。
要把springboot應(yīng)用轉(zhuǎn)至外部tomcat的操作主要有以下三點(diǎn):
1、把pom.xml文件中打包結(jié)果由jar改成war,如下:
- <modelVersion>4.0.0</modelVersion>
- <groupId>spring-boot-panminlan-mybatis-test</groupId>
- <artifactId>mybatis-test</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
2、添加maven的war打包插件如下:并且給war包起一個(gè)名字,tomcat部署后的訪問路徑會(huì)需要,如:http:localhost:8080/myweb/****
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <configuration>
- <warSourceExcludes>src/main/resources/**</warSourceExcludes>
- <warName>myweb</warName>
- </configuration>
- </plugin>
3、排除org.springframework.boot依賴中的tomcat內(nèi)置容器,這是很重要的一步
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <exclusions>
- <exclusion>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-tomcat</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
4、添加對(duì)servlet API的依賴
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- </dependency>
5、繼承SpringBootServletInitializer
,并覆蓋它的 configure
方法,如下圖代碼,為什么需要提供這樣一個(gè)SpringBootServletInitializer子類并覆蓋它的config方法呢,我們看下該類原代碼的注釋:
/**Note that a WebApplicationInitializer is only needed if you are building a war file and
* deploying it. If you prefer to run an embedded container then you won't need this at
* all.
如果我們構(gòu)建的是wai包并部署到外部tomcat則需要使用它,如果使用內(nèi)置servlet容器則不需要,外置tomcat環(huán)境的配置需要這個(gè)類的configure方法來指定初始化資源。
//@ServletComponentScan
public class JobManagementApplication extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(JobManagementApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(JobManagementApplication.class, args);
}
}
經(jīng)過以上配置,我們把構(gòu)建好的war包拷到tomcat的webapp下,啟動(dòng)tomcat就可以訪問啦
在pom文件中加入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
第一種配置方法:
在實(shí)體類中加入格式化屬性的注解
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String username;
private Date birthday;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@JSONField(format="yyyy-MM-dd hh:MM:ss")
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
2.在啟動(dòng)類中繼承類
@SpringBootApplication
public class Demo1Application2 extends WebMvcConfigurerAdapter{
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.configureMessageConverters(converters);
FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
converters.add(fastConverter);
}
public static void main(String[] args) {
SpringApplication app=new SpringApplication(Demo1Application2.class);
ConfigurableApplicationContext context = app.run( args);
//context.close();
}
}
3.訪問頁(yè)面,請(qǐng)求方法,得到結(jié)果
{ "age":11, "birthday":"2018-03-15 10:03:55", "id":1, "username":"dddd" }第二種配置方法:在啟動(dòng)類加入一個(gè)bean@SpringBootApplication
public class Demo1Application3 {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converters=fastConverter;
return new HttpMessageConverters(converters);
}
public static void main(String[] args) {
SpringApplication app=new SpringApplication(Demo1Application3.class);
ConfigurableApplicationContext context = app.run( args);
//context.close();
}
}
1、aop的簡(jiǎn)單實(shí)現(xiàn)
public class User implements InitializingBean,DisposableBean{
public String toString() {
System.out.println("userdddd");
return "444";
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println(" user afterPropertiesSet");
}
@Override
public void destroy() throws Exception {
System.out.println("user destroy");
}
}
public class LogUser extends User{
public String toString() {
System.out.println("log start");
super.toString();
System.out.println("log end");
return "";
}
}
public interface MyApplicationContextAware {
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
@Component
public class Dog4 implements MyApplicationContextAware{
ApplicationContext applicationContext;
public ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext=applicationContext;
}
}
@Component
public class MyBeanPostProcessor implements BeanPostProcessor{
@Autowired
ApplicationContext applicationContext;
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization=="+beanName);
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization=="+beanName);
if(bean instanceof User) {
return new LogUser();
}
if(bean instanceof MyApplicationContextAware) {
MyApplicationContextAware my=(MyApplicationContextAware) bean;
my.setApplicationContext(applicationContext);
}
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
}
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext aa=new AnnotationConfigApplicationContext("com.yjw");
Dog4 dog = aa.getBean(Dog4.class);
System.out.println(dog.getApplicationContext());
User user = aa.getBean(User.class);
System.out.println(user.toString());
aa.close();
}
}