一. Eureka注册中心搭建
-
步骤:
-
第一步:创建项目
-
第二步:添加注解 @EnableEurekaServer
-
第三步:增加配置application.yml
-
第四步:访问注册中心页面
-
-
另:maven镜像仓库地址: https://www.cnblogs.com/sword-successful/p/6408281.html
项目中的pom文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Application启动类:
@EnableEurekaServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
.web(true).run(args);
}
}
application.yml:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
#声明自己是个服务端
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
application.properties
spring.application.name=eureka-server
server.port=1001
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
二. 创建“服务提供方”
-
步骤:
-
新版本
-
添加服务注册和发现依赖
-
添加配置文件applicationNew.yml
-
-
老版本
-
添加服务注册和发现依赖
-
添加注解 @EnableDiscoveryClient
-
添加配置文件applicationOld.yml
-
-
pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
applicationOld.yml:
spring:
application:
name: zl-data-warning-center
cloud:
#指定配置中心的服务
config:
discovery:
enabled: true
serviceId: zl-configer-center
profile: dev
server:
port: 8082
eureka:
server:
address: 47.94.223.26
port: 8761
client:
serviceUrl:
defaultZone: http://${eureka.server.address}:${eureka.server.port}/eureka/
application.properties
spring.application.name=eureka-client
server.port=2001
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
applicationNew.yml:
server:
port: 8771
#指定注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#服务的名称
spring:
application:
name: product-service
三. Spring Cloud Consul
-
官方文档: https://www.consul.io/
-
简介:Consul是一个分布式高可用的系统,它包含多个组件,但是作为一个整体,在微服务架构中为我们的基础设施提供服务发现和服务配置的工具。
-
特性:
-
服务发现
-
健康检查
-
Key/Value存储
-
多数据中心
-
-
使用:
-
将基于springBoot的微服务注册到Consul上
-
步骤:
-
将eureka-client的pom.xml中的eurka依赖改为consul-descovery依赖
-
修改application.properites,将consul配置加入
-
-
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
application.properites(yml相同)
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
-
注意:
-
-
不需要创建类似eureka-server的服务端吗?
-
由于Consul自身提供了服务端,所以我们不需要像之前实现Eureka的时候创建服务注册中心,直接通过下载consul的服务端程序就可以使用。
-
-
使用命令可以启动consul开发模式
-
命令可以启动consul开发模式:
$consul agent -dev
==> Starting Consul agent...
==> Starting Consul agent RPC...
==> Consul agent running!
Version: 'v0.7.2'
Node name: 'Lenovo-zhaiyc'
Datacenter: 'dc1'
Server: true (bootstrap: false)
Client Addr: 127.0.0.1 (HTTP: 8500, HTTPS: -1, DNS: 8600, RPC: 8400)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Gossip encrypt: false, RPC-TLS: false, TLS-Incoming: false
Atlas: <disabled>
==> Log data will now stream in as it occurs:
2017/06/22 07:50:54 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:127.0.0.1:8300 Address:127.0.0.1:8300}]
2017/06/22 07:50:54 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc 127.0.0.1
2017/06/22 07:50:54 [INFO] consul: Adding LAN server Lenovo-zhaiyc (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2017/06/22 07:50:54 [INFO] serf: EventMemberJoin: Lenovo-zhaiyc.dc1 127.0.0.1
2017/06/22 07:50:54 [INFO] consul: Adding WAN server Lenovo-zhaiyc.dc1 (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2017/06/22 07:51:01 [ERR] agent: failed to sync remote state: No cluster leader
2017/06/22 07:51:02 [WARN] raft: Heartbeat timeout from "" reached, starting election
2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
2017/06/22 07:51:02 [DEBUG] raft: Votes needed: 1
2017/06/22 07:51:02 [DEBUG] raft: Vote granted from 127.0.0.1:8300 in term 2. Tally: 1
2017/06/22 07:51:02 [INFO] raft: Election won. Tally: 1
2017/06/22 07:51:02 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
2017/06/22 07:51:02 [INFO] consul: cluster leadership acquired
2017/06/22 07:51:02 [INFO] consul: New leader elected: Lenovo-zhaiyc
2017/06/22 07:51:02 [DEBUG] consul: reset tombstone GC to index 3
2017/06/22 07:51:02 [INFO] consul: member 'Lenovo-zhaiyc' joined, marking health alive
2017/06/22 07:51:02 [INFO] agent: Synced service 'consul'
2017/06/22 07:51:02 [DEBUG] agent: Node info in sync
四. eureka和consul控制台
-
eureka控制台
-
-
注意事项:
-
eureka管理后台出现一串红色字体:是警告,说明有服务上线率低:EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.
-
为什么只加一个注册中心地址,就可以注册:By having spring-cloud-starter-netflix-eureka-client on the classpath, your application automatically registers with the Eureka Server. Configuration is required to locate the Eureka server, as shown in the following example:
-
-