org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
server:
port: 8762 # 指定该Eureka实例的端口
eureka:
client:
registerWithEureka: false
fetchRegistry: false
spring:
application:
name: eurekaserver
创建一个dockerfile文件,把下面的内容复制进去
FROM java:8
VOLUME /usr/test
ADD simple-eureka-0.0.1-SNAPSHOT.jar eureka.jar
EXPOSE 8762
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/eureka.jar"]
这一步如果报错
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
是因为daemon没启动,请先启动daemon
systemctl daemon-reload
systemctl restart docker.service
docker build -f mydockerfile/eurekadockerfile -t test/eureka .
注意最后有个空格+. 别漏了。
这一步要是报错
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
执行下面指令。
systemctl daemon-reload
systemctl restart docker.service
docker run -d -p 8762:8762 test/eureka
重启一下docker
systemctl restart docker
firewall-cmd --zone=public --add-port=8762/tcp --permanent
firewall-cmd --reload
vim /usr/lib/systemd/system/docker.service
在打开的文件的 ExecStart=/usr/bin/dockerd 后面加上一句
-H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
保存,退出
注 : 端口 2375 就是docker remoteApi的 端口,确保此端口linux没有被占用。
执行 重启 docker 命令 docker重新读取配置文件,并重新启动docker服务 命令
sudo systemctl daemon-reload && systemctl restart docker
server:
port: 1111
spring:
application:
name: docker-images-getter
eureka:
client:
service-url:
defaultZone: http://服务器ip:8762/eureka/
instance:
prefer-ip-address: true
@Controller
public class ImagesGetterController {
@GetMapping("/getimages")
public ModelAndView getImages(){
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("redirect:http://47.93.150.204:2375/images/json");
return modelAndView;
}
}
redirect:http://47.93.150.204:2375/images/json 跳转到docker web api接口 。
目前我就会这一种调用方式。
/images/json 查看docker中所有本地镜像
docker的webapi在docker官网看
docker api文档参考
FROM java:8
VOLUME /usr/test
ADD sample-server-provider-0.0.1-SNAPSHOT.jar imagesgetter.jar
EXPOSE 1111
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/imagesgetter.jar"]
docker build -f mydockerfile/imagesgetterdockerfile -t test/imagesgetter .
docker run -d -p 1111:1111 test/imagesgetter
application.yml
spring:
application:
name: microservice-discovery-eureka # 微服务实例的名称
# 第一个 eureka 环境
---
spring:
profiles: peer1
server:
port: 1001 # 开放的端口
eureka:
instance:
hostname: peer1
prefer-ip-address: true
client:
service-url:
defaultZone: http://peer2:1002/eureka/ # 注册到另一个服务实例
register-with-eureka: true
fetch-registry: false
# 第二个 eureka 环境
---
spring:
profiles: peer2
server:
port: 1002 # 开放的端口
eureka:
instance:
hostname: peer2
prefer-ip-address: true
client:
service-url:
defaultZone: http://peer1:1001/eureka/ # 注册到另一个服务实例
register-with-eureka: true
fetch-registry: false
FROM java:8
VOLUME /usr/test
ADD discovery-eureka-0.0.1-SNAPSHOT.jar eureka-01.jar
EXPOSE 1001
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/eureka-01.jar"]
docker build -f mydockerfile/eurekaHighAvaliable-01 -t test/eureka-01 .
curl -L https://github.com/docker/compose/releases/download/1.7.0/docker-compose-`uname -s `-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose -v
version: '2'
services:
peer1:
hostname: peer1
image: test/eureka-01
ports:
- 1001:1001
environment:
- spring.profiles.active=peer1
peer2:
hostname: peer2
image: test/eureka-02
ports:
- 1002:1002
environment:
- spring.profiles.active=peer2
docker-compose up -d
vim etc/hosts
127.0.0.1 peer1
127.0.0.1 peer2
5.开启服务器防火墙端口
6.阿里云官网添加安全组
version: '2'
services:
peer1:
hostname: peer1
image: test/eureka-01
ports:
- 1001:1001
environment:
- spring.profiles.active=peer1
peer2:
hostname: peer2
image: test/eureka-02
ports:
- 1002:1002
environment:
- spring.profiles.active=peer2
provider-2001:
hostname: peer1
image: test/provider
container_name: provider-2001
ports:
- 2001:2001
environment:
- server.port=2001
provider-2002:
hostname: peer2
image: test/provider
container_name: provider-2002
ports:
- 2002:2002
environment:
- server.port=2002
consumer:
image: test/consumer
ports:
- 3001:3001
https://gitee.com/guzhengxi/spring-cloud-config.git