我们知道,从eurake2.0闭源以后,Consul被越来越多的企业作为服务注册与发现的中间件,无论从使用效率还是启动占用内存的方面都是比Eurake要优秀。现在将本人在使用过程中的步骤记录下来:
安装Consul:(centos7 )
wget https://releases.hashicorp.com/consul/1.3.0/consul_1.3.0_linux_amd64.zip
解压:
unzip consul_1.3.0_linux_amd64.zip
启动consul:
./consul agent -server -ui -bootstrap-expect=1 -data-dir=/usr/local/consul -node=devmaster -advertise=192.168.1.104 -bind=0.0.0.0 -client=0.0.0.0
访问:http://192.168.1.104:8500 出现可视化控制台
![在这里
springBoot版本:1.5.3.RELEASE
SpringCloud版本:Edgware.SR3
POM文件:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.admin</groupId>
<artifactId>template-news</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>template-news</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud.version>Edgware.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-eureka</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-eureka-server</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.zhangjianbing</groupId>-->
<!-- <artifactId>feign_interface</artifactId>-->
<!-- <version>1.0-SNAPSHOT</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- 断路器,服务调用者需要使用该组件,来保证其他服务降级时的处理逻辑 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<!-- <version>1.4.7.RELEASE</version>-->
</dependency>
<!-- 网关,对外开放的接收外部请求的服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<!-- <version>1.4.7.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<!--引入consul注册中心相关pom-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
bootstrap.yml配置(使用consul配置放到application.yml中不会生效):
spring:
cloud:
consul:
host: 192.168.1.104
port: 8500
discovery:
service-name: news-service
health-check-path: /health
health-check-url: http://${当前主机ip}:${server.port}/health
application.yml配置文件:
server:
port: 8083
undertow:
direct-buffers: true
io-threads: 4
worker-threads: 160
spring:
application:
name: news-service
############引入consul 注册中心配置
# cloud:
# consul:
# port: 8500
# discovery:
# service-name: news-service
# hostname: 192.168.1.104
# register: true
# host: http://192.168.1.104:8500
# config:
# enabled: true #false禁用Consul配置,默认是true
# format: YAML #表示consul上面文件的格式: 有四种 YANL PROPERTILES KEY-VALUE FILES
# data-key: configuration #表示consul 上面的KEY值() 默认是data
# prefix: #设置配置的基本文件架
# defaultContent: #设置所有应用程序使用的文件夹名称
# profileSeparator: #设置用于使用配置文件在属性源中分割配置文件名称的分隔符的值
#配置redis集群
redis:
cluster:
nodes: 192.168.1.104:7000,192.168.1.104:7001,192.168.1.104:7002,192.168.1.104:7003,192.168.1.104:7004,192.168.1.104:7005
timeout: 5000
#eureka:
# client:
# service-url:
# defaultZone: http://localhost:8088/eureka/
# fetch-registry: true #是否从Server获取接节点信息 如果是单节点不必要开启
# register-with-eureka: true #是否将自己注册到eurakeServer
# instance:
# prefer-ip-address: true
# lease-expiration-duration-in-seconds: 30
# lease-renewal-interval-in-seconds: 10
ribbon:
consul:
enabled: true
feign:
hystrix:
enabled: true
消费者启动类使用 @EnableFeignClients 开启使用Fegin客户端
使用@FeignClient来调用远程服务:
1.注意springCloud的版本问题:(springboot使用1.3.X版本,使用Datlson.REALEASE 服务可以注册成功,但是服务状态处于 Down)
换成 Edgware.SR3 状态ok
2.注意配置信息放到bootstrap.yml中,直接配置到application.yml中识别不到。
调用服务查看调用结果:ok