Docker Integrated Deployment (II) Gitlab+spring boot+config Unified Configuration Center
1. Build config project on gitlab
Once established, use git clone to pull it locally
2. Establishment of spring config project by IDEA
1. Choose to add the following dependencies
<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-client</artifactId> </dependency>
2. Add the @EnableConfigServer annotation to the entry class
@SpringBootApplication @EnableConfigServer @EnableDiscoveryClient public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); } }
3 Configure the application.yml file
spring: application: name: wx-config //project name cloud: config: label: master //Branches on gitlab server: git: username: gitlab Account number uri: https://https address pulled up on gitlab.com/wx2019/config.git//gitlab search-paths: resp //New folder for configuration files password: gitlab Password
-
Because the config module is placed on the remote server independently, the configuration of the eureka registry is as follows. Since the config service registers with the registry using the private address of the remote server, rather than the public address, it can be configured
ip-address: xxx.xxx.xxx.xxx
hostname: ${eureka.instance.ip-address}
To register the address to the registry using the public network address (the IP address you specify)eureka: client: register-with-eureka: true #Register with the Registry service-url: defaultZone: http://Xxxxxx: XXXX / Eureka / // address of registry instance: prefer-ip-address: true #Access paths can display Ip addresses lease-expiration-duration-in-seconds: 90 #Tell the server that if I don't give you a heartbeat within 90 seconds, it means I'm "dead" and kick me out. lease-renewal-interval-in-seconds: 30 #Send a heartbeat to the server every 30 seconds to prove that you are still alive ip-address: xxx.xxx.xxx.xxx hostname: ${eureka.instance.ip-address}
5. Establish resp folder, put yaml configuration files of each distributed module into it, and push Git to resp folder of remote Gitlab
The corresponding resp folder on gitlab:
6 Packaging project, deploy to remote server, complete startup.
Modules requiring configuration files in the configuration center
For example, my file module service needs to use the Configuration Center's Unified Configuration File to introduce config dependencies into the project.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
2 Then configure the bootstrap.yml file with the highest priority and choose to use the corresponding configuration file.
spring: cloud: config: uri: http://xxx.xx.89.101:8502//IP address of remote server deploying config service module name: wx2019-file-service //To use the corresponding configuration file name on gitlab label: master //Which branch of gitlab is config on profile: dev //Configuration file using dev suffix version
Note that wx2019-file-service should use the configuration file name on gitlab as follows
Packaging and deploying the service file module of config client
1. Use IDEA's maven tool to generate executable jar files
2. Configure the Dockefile file and build the image of the file module as follows
FROM java:8 MAINTAINER 60kg xxxxx@qq.com VOLUME /tmp ADD file-service-0.0.1-SNAPSHOT-exec.jar file-service.jar RUN bash -c 'touch /file-service.jar' ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/file-service.jar"]
3. Deploy the dockerfile file file and jar package to the same folder directory of the remote server, and use the following commands to build the image
docker build -t wx-file-service .
4 Configure docker-compose startup configuration class docker-compose-service.yml as follows
version: "3.1" services: spring_file_service: image: wx-file-service //Generated mirror name ports: - "10003:10003" //Use the ports on the server and the corresponding external ports environment: - "spring.profiles.active=dev" //Configure the jar package to use the dev version of the configuration file - EUREKA_INSTANCE_IP-ADDRESS=47.102.97.30 //Register the service with the corresponding IP address in the registry - SERVER_PORT=10000 volumes: - ./wx-file/static:/static //Configuration data volume
It should be noted that if the jar package project configuration file has different configuration files such as test, prod and dev, you need to configure environment: option, and select the corresponding yaml file to start at boot time.
Finally, use docker-compose command to start the project, finish!
docker-compose -f docker-compose-service.yml up -d