docker管理springcloud的注册中心eureka和配置中心config

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/li1987by/article/details/85290703

一、docker安装
这里先采用在线安装,利用docker hup下载基础镜像
1.环境版本要求

内核版本3.10及其以上
操作系统位数为64位
CPU架构为x86_64或amd64(目前也有别的支持)
内核开启并支持cgroup和命名空间

2.命令检查内核版本,本地环境为centos7

uname -r

3.更新yum

sudo yum update

4.添加Docker的yum源

sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF

5.安装Docker软件包

sudo yum install docker-engine

6.设置Docker服务开机自启

sudo systemctl enable docker.service

7.启动Docker服务

sudo systemctl start docker

8.验证Docker是否安装成功

docker -v

9.查看镜像

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED   

10.删除docker

 sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

二、项目配置
springcloud版本:

<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
spring-boot-starter-parent:2.0.3

1.eureka
1)关键pom

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

2)启动类

@SpringBootApplication
@EnableEurekaServer
public class CommonserviceEurekaApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {

        SpringApplication.run(CommonserviceEurekaApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(CommonserviceEurekaApplication.class);
    }
}

3)配置文件关键参数

eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

2.config
1)关键pom

	 <!--注册发现-->
	 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
          <!--配置中心-->
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

2)启动类

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
@EnableEurekaClient
public class ConfigServerApplication {

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

3)配置文件关键参数

#配置注册服务中心
eureka.client.serviceUrl.defaultZone= http://${eureka-container-name}:${config-service-port}/eureka/
#设置为本地启动的方式,而不是通过git
spring.profiles.active=native
#配置本地配置路径
spring.cloud.config.server.native.search-locations=${local-config-path}

注意:上面的${eureka-container-name}为eureka容器的别名或者id

三、生成镜像
1.项目eureka\config分别打包上传服务器
2.Dockerfile

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD ROOT.jar app.jar
#RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
EXPOSE ${PORT}

注意上面的${PORT} 应替换成eureka\config设置的不同容器声明端口
3.build 生成镜像

docker build -t orginaztion/service-image-name:tag

其中-t 标识镜像tag ,格式为:所属库/服务镜像名:版本

四、运行镜像
其中 l o c a l h o s t p o r t 宿 {localhost-*-port}替换为宿主机开放端口, {container-*-port}替换为容器服务端口
1.启动eureka

docker run --name eureka-service -d -it -p ${localhost-eureka-port}:${container-eureka-port} orginaztion/service-image-name:tag

注意:
并且name对应config中的注册机参数的${eureka-container-name}
2.启动config

docker run --name config-server  --link eureka-server:eureka-server-name-alias -d -it -p ${localhost-config-port}:${container-config-port} -v ${local-config-path}:${contain-config-path} orginaztion/service-image-name:tag

注意:config依赖eureka,用–link链接 ,-v指定本地配置路径链接容器相应路径
3.访问config配置信息

http://${localhost}:${localhost-config-port}/${prefix-config-name}/test|dev|prod

猜你喜欢

转载自blog.csdn.net/li1987by/article/details/85290703