搭建配置中心
導(dǎo)入依賴
<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">
<parent>
<artifactId>sakura</artifactId>
<groupId>com.warm-sun</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
?
<artifactId>service-config</artifactId>
<name>service-config</name>
<packaging>jar</packaging>
<description> 配置中心</description>
<dependencies>
<!--配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--web 模塊-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--排除tomcat依賴-->
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!--undertow容器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
</dependencies>
?
?
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>${project.name}</finalName>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.12</version>
<configuration>
<imageName>${registry.url}/${project.name}:0.0.1</imageName>
<dockerHost>${docker.url}</dockerHost>
<dockerDirectory>${project.basedir}</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<serverId>docker-hub</serverId>
<registryUrl>https://index.docker.io/v1/</registryUrl>
</configuration>
</plugin>
</plugins>
</build>
?
</project>
編寫啟動(dòng)類
package com.warmsun.config;
?
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
?
/**
* 配置中心
*/
@EnableConfigServer
@SpringCloudApplication
public class SakuraConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SakuraConfigApplication.class,args);
}
}
我們來對(duì)比之前的eureka搭建的主類睡陪,發(fā)現(xiàn)這里我們用的是@SpringCloudApplication注解巴碗,而eureka的主類上用的是@SpringBootApplication 注解蛉签,那我們來看一下SpringCloudApplication注解干了什么事情:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public @interface SpringCloudApplication {
}
導(dǎo)入了@SpringBootApplication注解,導(dǎo)入@EnableDiscoveryClient注解,改注解能導(dǎo)入config配置畔咧,不管你是Spring-cloud-config還是其他的例如:Apollo等娶靡,@EnableCircuitBreake 導(dǎo)入一個(gè)斷路器實(shí)現(xiàn)牧牢。@EnableDiscoveryClient和@EnableCircuitBreake 我們可能還不是很了解,先不急姿锭,后面我們會(huì)一一解析
編寫配置
server:
port: 2001
?
spring:
application:
name: service-config
#配置環(huán)境
profiles: dev
cloud:
config:
server:
git:
uri: https://github.com/JamariRay/athena-config.git
#如過你的項(xiàng)目是私有塔鳍,那么在下面配置你的git信息,如果是公開的艾凯,刪除下面的2配置
username: ××××
password: ××××××
?
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://sakura:Sakura*&^999@${eureka.instance.hostname}:${server.port}/eureka/
編寫其他的微服務(wù)配置
在github上 創(chuàng)建一個(gè)athena-config項(xiàng)目献幔,然后克隆到本地,編寫好后趾诗,push到github蜡感,這樣我們的config項(xiàng)目就能讀取到相應(yīng)微服務(wù)的信息了。
application-dev:
# redis 相關(guān)
spring:
redis:
password:
host: localhost
# 暴露監(jiān)控端點(diǎn)
management:
endpoints:
web:
exposure:
include: '*'
?
# feign 配置
feign:
hystrix:
enabled: true
okhttp:
enabled: true
httpclient:
enabled: false
client:
config:
feignName:
connectTimeout: 10000
readTimeout: 10000
compression:
request:
enabled: true
response:
enabled: true
# hystrix If you need to use ThreadLocal bound variables in your RequestInterceptor`s
# you will need to either set the thread isolation strategy for Hystrix to `SEMAPHORE or disable Hystrix in Feign.
hystrix:
command:
default:
execution:
isolation:
strategy: SEMAPHORE
thread:
timeoutInMilliseconds: 60000
shareSecurityContext: true
?
#請(qǐng)求處理的超時(shí)時(shí)間
ribbon:
ReadTimeout: 10000
ConnectTimeout: 10000
加-dev是因?yàn)楫?dāng)前壞境為開發(fā)環(huán)境恃泪,在service-config項(xiàng)目下已經(jīng)配置了profiles:dev所以這里也要對(duì)應(yīng)郑兴,其他的微服務(wù)配置
# 數(shù)據(jù)源
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/sakura?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
好了微服務(wù)注冊(cè)中心就搭好了,更多搭建信息參見spring的官方文檔.
好了搭建微服務(wù)配置中心 就到這里了贝乎,源碼解析我會(huì)在后面分享情连!