0. 服务注册的坑
用zuul和consul进行服务发现时,相同服务的端口不能相同,否则consul中的instance-id会相同,即使是不同ip也不能相同。
1. consul 安装
略
2. 配置zuul的application.yml
zuul的application.yml如下
server:
port: 8762
spring:
cloud:
consul:
discovery:
instance-id: ${spring.application.name}:${server.port}
prefer-ip-address: true
health-check-interval: 10s
hostname: ${spring.application.name}
service-name: ${spring.application.name}
enabled: true
register: true
host: 127.0.0.1 #consul-ip
port: 8500 #consul-port
enabled: true
http:
multipart:
location: ${user.dir}/temp
application:
name: rsapm-zuul
#logging:
# level:
# com.netflix: debug
zuul:
# prefix: /XXX
# ignored-services: '*'
sensitiveHeaders:
routes:
XXX-brain:
path: /brain/**
# strip-prefix: false
XXX-mysql-data:
path: /mysql/**
strip-prefix: false
host:
socket-timeout-millis: 60000
connect-timeout-millis: 60000
hystrix:
command:
default:
execution:
timeout:
enabled: false
isolation:
thread:
timeoutInMilliseconds: 60000
ribbon:
ReadTimeout: 60000
ConnectTimeout: 60000
其中,routes 为配zuul转发的路由表;command 为zuul转发http请求的超时时间等配置。
3. zuul的pom.xml (maven)
<?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>
<groupId>com.XXX.XXX</groupId>
<artifactId>XXX</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<!-- 引入spring boot的依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>
<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 添加spring-boot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. 配置被转发应用的application.yml中添加cosul配置
被转发应用的application.yml如下
server:
port: 8101
spring:
cloud:
consul:
discovery:
instance-id: ${spring.application.name}:${server.port}
prefer-ip-address: true
health-check-interval: 10s
hostname: ${spring.application.name}
service-name: ${spring.application.name}
enabled: true
register: true
host: 127.0.0.1 #consul-ip
port: 8500 #consul-port
enabled: true
4. 被转发应用的pom.xml (maven)添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 添加spring-boot的maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>