創建項目cbj-config(統一配置中心)
新建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>
<parent>
<groupId>com.funtl</groupId>
<artifactId>cbj-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../cbj-dependencies/pom.xml</relativePath>
</parent>
<artifactId>cbj-config</artifactId>
<packaging>jar</packaging>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Cloud End -->
<!-- Spring Boot Admin Begin -->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<!-- Spring Boot Admin End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.funtl.cbj.config.ConfigApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<url>http://192.168.0.15:8081/repository/maven-public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</project>
新建Docker目錄
在Docker目錄下新建Dockerfile和docker-compose.yml
FROM openjdk:8-jre
MAINTAINER yuyingliang <1079226090@qq.com>
RUN mkdir /app
COPY cbj-config-1.0.0-SNAPSHOT.jar /app/app.jar
ENTRYPOINT [ "java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/app.jar","--spring.profiles.active=test"]
EXPOSE 8888
docker-compose.yml
version: "3.1"
services:
cbj-config:
restart: always
image: 192.168.0.39:5000/cbj-config(docker registry 倉庫鏡像)
container_name: cbj-config
ports:
- 8888:8888
創建一個response目錄放所有服務的配置文件
cbj-admin-dev.yml
spring:
application:
name: cbj-admin
zipkin:
base-url: http://localhost:9411
server:
port: 8084
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-admin-test.yml
spring:
application:
name: cbj-admin
zipkin:
base-url: http://192.168.0.27:9411
server:
port: 8084
eureka:
client:
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-admin-prod.yml
spring:
application:
name: cbj-admin
zipkin:
base-url: http://192.168.0.27:9411
server:
port: 8084
eureka:
client:
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-api-admin-dev.yml
spring:
application:
name: cbj-api-admin
boot:
admin:
client:
url: http://localhost:8084
zipkin:
base-url: http://localhost:9411
thymeleaf:
cache: false
mode: LEGACYHTML5
encoding: UTF-8
servlet:
content-type: text/html
server:
port: 8601
feign:
hystrix:
enabled: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-api-admin-test.yml
spring:
application:
name: cbj-api-admin
boot:
admin:
client:
url: http://192.168.0.27:8084
zipkin:
base-url: http://192.168.0.27:9411
thymeleaf:
cache: false
mode: LEGACYHTML5
encoding: UTF-8
servlet:
content-type: text/html
server:
port: 8601
feign:
hystrix:
enabled: true
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-api-admin-prod.yml
spring:
application:
name: cbj-api-admin
boot:
admin:
client:
url: http://192.168.0.27:8084
zipkin:
base-url: http://192.168.0.27:9411
thymeleaf:
cache: false
mode: LEGACYHTML5
encoding: UTF-8
servlet:
content-type: text/html
server:
port: 8601
feign:
hystrix:
enabled: true
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-eureka-dev.yml
spring:
application:
name: cbj-eureka
boot:
admin:
client:
url: http://localhost:8084
zipkin:
base-url: http://localhost:9411
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-eureka-test.yml
spring:
application:
name: cbj-eureka
boot:
admin:
client:
url: http://192.168.0.27:8084
zipkin:
base-url: http://192.168.0.27:9411
server:
port: 8761
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-eureka-prod.yml
spring:
application:
name: cbj-eureka
boot:
admin:
client:
url: http://192.168.0.27:8084
zipkin:
base-url: http://192.168.0.27:9411
server:
port: 8761
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-service-admin-dev.yml
spring:
application:
name: cbj-service-admin
boot:
admin:
client:
url: http://localhost:8084
zipkin:
base-url: http://localhost:9411
datasource:
druid:
url: jdbc:mysql://192.168.0.38:3306/cbj-service-admin?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
initial-size: 1
min-idle: 1
max-active: 20
test-on-borrow: true
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: com.funtl.cbj.service.admin.domain
mapper-locations: classpath:mapper/*.xml
server:
port: 8501
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-service-admin-test.yml
spring:
application:
name: cbj-service-admin
boot:
admin:
client:
url: http://192.168.0.27:8084
zipkin:
base-url: http://192.168.0.27:9411
datasource:
druid:
url: jdbc:mysql://192.168.0.38:3306/cbj-service-admin?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
initial-size: 1
min-idle: 1
max-active: 20
test-on-borrow: true
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: com.funtl.cbj.service.admin.domain
mapper-locations: classpath:mapper/*.xml
server:
port: 8501
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-service-admin-prod.yml
spring:
application:
name: cbj-service-admin
boot:
admin:
client:
url: http://192.168.0.27:8084
zipkin:
base-url: http://192.168.0.27:9411
datasource:
druid:
url: jdbc:mysql://192.168.0.38:3306/cbj-service-admin?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
initial-size: 1
min-idle: 1
max-active: 20
test-on-borrow: true
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: com.funtl.cbj.service.admin.domain
mapper-locations: classpath:mapper/*.xml
server:
port: 8501
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-zipkin-dev.yml
spring:
application:
name: cbj-zipkin
boot:
admin:
client:
url: http://localhost:8084
zipkin:
base-url: http://localhost:9411
server:
port: 9411
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
metrics:
web:
server:
auto-time-requests: false
cbj-zipkin-test.yml
spring:
application:
name: cbj-zipkin
boot:
admin:
client:
url: http://192.168.0.27:8084
zipkin:
base-url: http://192.168.0.27:9411
server:
port: 9411
eureka:
client:
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
metrics:
web:
server:
auto-time-requests: false
cbj-zipkin-dev.yml
spring:
application:
name: cbj-zipkin
boot:
admin:
client:
url: http://192.168.0.27:8084
zipkin:
base-url: http://192.168.0.27:9411
server:
port: 9411
eureka:
client:
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
metrics:
web:
server:
auto-time-requests: false
cbj-zuul-dev.yml
spring:
application:
name: cbj-zuul
boot:
admin:
client:
url: http://localhost:8084
zipkin:
base-url: http://localhost:9411
server:
port: 8769
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-zuul-test.yml
spring:
application:
name: cbj-zuul
boot:
admin:
client:
url: http://192.168.0.27:8084
zipkin:
base-url: http://192.168.0.27:9411
server:
port: 8769
eureka:
client:
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
cbj-zuul-prod.yml
spring:
application:
name: cbj-zuul
boot:
admin:
client:
url: http://192.168.0.27:8084
zipkin:
base-url: http://192.168.0.27:9411
server:
port: 8769
eureka:
client:
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
創建src/main/java目錄
創建com.funtl.cbj.config包
創建ConfigApplication啓動類
package com.funtl.cbj.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
在main目錄下創建resources目錄
爲啥用boostrap而不用 application.yml(自己百度去,這裏就不詳細說明了)
bootstrap.yml
spring:
application:
name: cbj-config
boot:
admin:
client:
url: http://localhost:8084
cloud:
config:
label: master
server:
git:
uri: http://192.168.0.22/cbj/cbj-config.git
search-paths: respo
username: 1079226090@qq.com
password: 12345678
zipkin:
base-url: http://localhost:9411
server:
port: 8888
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
bootstrap-test.yml
spring:
application:
name: cbj-config
boot:
admin:
client:
url: http://192.168.0.27:8084
cloud:
config:
label: master
server:
git:
uri: http://192.168.0.22/cbj/cbj-config.git
search-paths: respo
username: 1079226090@qq.com
password: 12345678
zipkin:
base-url: http://192.168.0.27:9411
server:
port: 8888
eureka:
client:
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
bootstrap-prod.yml
spring:
application:
name: cbj-config
boot:
admin:
client:
url: http://192.168.0.27:8084
cloud:
config:
label: master
server:
git:
uri: http://192.168.0.22/cbj/cbj-config.git
search-paths: respo
username: 1079226090@qq.com
password: 12345678
zipkin:
base-url: http://192.168.0.27:9411
server:
port: 8888
eureka:
client:
serviceUrl:
defaultZone: http://192.168.0.27:8761/eureka/,http://192.168.0.27:8861/eureka/,http://192.168.0.27:8961/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
在項目下面新建.gitlab-ci.yml
#步驟
stages:
- build
- push
- run
- clean
#構建
build:
stage: build
script:
- /usr/local/maven/apache-maven-3.5.3/bin/mvn clean package
- cp target/cbj-config-1.0.0-SNAPSHOT.jar docker
- cd docker
- docker build -t 192.168.0.39:5000/cbj-config .
#推送
push:
stage: push
script:
- docker push 192.168.0.39:5000/cbj-config
#啓動服務
run:
stage: run
script:
- cd docker
- docker-compose up -d
#刪除多餘鏡像
clean :
stage: clean
script:
- docker rmi $(docker images -q -f dangling=true)