其他分享
首页 > 其他分享> > springboot+consul配置多地址环境

springboot+consul配置多地址环境

作者:互联网

问题:2台服务器都安装了Consul作为服务发现,在bootstrap.yml配置了项目注册的多个环境,每个环境的Consul地址是不同的,但是当使用某一个profiles配置时,总是2个Consul都被注册成功了;

 

 

原因:服务在测试时,在Consul1服务注册过,在Consul2服务也注册过,当Consul注册服务切换时,没有删除其他Consul中的无用服务的节点,才导致上面问题的产生;Consul的节点如果不删除,则会一直监听192.168.1.231服务器是否启动了common-service服务;

 

 

解决方案:修改docker-compose.yml的启动profiles的时候

- SPRING_ACTIVE=-Dspring.profiles.active=dev 

先将所有Consul的common-service删除,然后在启动common-service服务,就不会出现2个都Consul注册的情况了

环境及配置:

服务器 consul地址 bootstrap.ym配置consul多地址 common-service项目的Dockerfile配置 docker-compose.yml配置 

192.168.1.75

192.168.1.75:8500
--- #75环境配置(---表示多个profiles)
spring:
profiles: dev
cloud:
consul:
host: 192.168.1.75
port: 8500
FROM java:8
ADD target/common-service-1.0.0-SNAPSHOT.jar //
ENV JAVA_OPTS=""
ENV SPRING_ACTIVE=""
CMD java -jar ${JAVA_OPTS} ${SPRING_ACTIVE} common-service-1.0.0-SNAPSHOT.jar
 
version: "3"
services:
zy-common:
restart: always
container_name: zy-common
image: common-service:latest
ports:
- "8011:8011"
network_mode: host
volumes:
- /home/logs:/home/logs
environment:
- JAVA_OPTS=-Xms256m -Xmx256m
   - SPRING_ACTIVE=-Dspring.profiles.active=dev
- CONSUL_TOKEN=6d9a61ae-7e99-ab1b-2951-bf4a29e00b74
deploy:
resources:
limits:
memory: 6G
192.168.1.231 192.168.1.231:8500
--- #231环境配置(---表示多个profiles)
spring:
profiles: test_231
cloud:
consul:
host: 192.168.1.231
port: 8500
   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

bootstrap.yml文件内容:

spring:
  sleuth:
    sampler:
      percentage: 1.0
  cloud:
    consul:
      discovery:
        register: true
        prefer-ip-address: true
        instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
        acl-token: ${CONSUL_TOKEN}
        tags:
          - ${spring.profiles.active}
        health-check-tls-skip-verify: true
      config:
        enabled: true
        format: yaml
        watch:
          enabled: true
        prefix: config
        data-key: data
  profiles:
    active: dev

--- #75环境配置(---表示多个profiles)
spring:
  profiles: dev
  cloud:
    consul:
      host: 192.168.1.75
      port: 8500
--- #231环境配置(---表示多个profiles)
spring:
  profiles: test_231
  cloud:
    consul:
      host: 192.168.1.231
      port: 8500
View Code

Dockerfile文件内容:

FROM java:8
ADD target/common-service-1.0.0-SNAPSHOT.jar //
ENV JAVA_OPTS=""
ENV SPRING_ACTIVE=""
CMD java -jar ${JAVA_OPTS} ${SPRING_ACTIVE} common-service-1.0.0-SNAPSHOT.jar
#ENTRYPOINT ["sh", "-c", "java -jar common-service-1.0.0-SNAPSHOT.jar $JAVA_OPTS $SPRING_ACTIVE"]
View Code

docker-compose.yml文件内容:

version: "3"
services:
zy-common:
  restart: always
  container_name: zy-common
  image: common-service:latest
  ports:
    - "8011:8011"
  network_mode: host
  volumes:
    - /home/logs:/home/logs
  environment:
    - JAVA_OPTS=-Xms256m -Xmx256m
   - SPRING_ACTIVE=-Dspring.profiles.active=dev
    - CONSUL_TOKEN=6d9a61ae-7e99-ab1b-2951-bf4a29e00b74
  deploy:
    resources:
      limits:
        memory: 6G
View Code

 

标签:springboot,service,consul,jar,profiles,地址,common,192.168
来源: https://www.cnblogs.com/tianchao/p/16455747.html