Eureka 服务器
配置高可用Eureka服务器
1.配置公用Eureka 服务器application.properties
##定义应用名称
spring.application.name=spring-cloud-eureka-server
##配置端口
##通过启动参数覆盖9090端口
#server.port=9090
##取消向注册中心注册
eureka.client.register-with-eureka=true
##取消向注册中心获取注册信息,实例信息
eureka.client.fetch-registry=true
##解决Peer/集群连接问题
#eureka.instance.hostname=localhost
#eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
2.配置Peer 1 Eureka服务器application-peer1.properties(单机情况相当于profile="peer1")
##peer1完整配置
##配置端口
#peer1端口9090
server.port=9090
#peer2主机:localhost 端口9091
peer2.server.host=localhost
peer2.server.port=9091
#Eureka注册信息
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${peer2.server.host}:${peer2.server.port}/eureka
启动Peer1 Eureka服务器--spring.profiles.active=peer1
,相当于读取了application-peer1.properties和application.properties
3.配置Peer 2 Eureka服务器application-peer2.properties(单机情况相当于profile="peer2")
##peer2完整配置
##配置端口
#peer2端口9090
server.port=9091
#peer1主机:localhost 端口9090
peer1.server.host=localhost
peer1.server.port=9090
##取消向注册中心注册
#eureka.client.register-with-eureka=false
##取消向注册中心获取注册信息,实例信息
#eureka.client.fetch-registry=false
##解决Peer/集群连接问题
#Eureka注册信息
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://${peer1.server.host}:${peer1.server.port}/eureka
启动Peer2 Eureka服务器--spring.profiles.active=peer2
,相当于读取了application-peer2.properties和application.properties
效果如图:
Spring cloud Consul
Consul组件
- 服务发现(Service Discovery)
- 健康检查(Health Check)
- 键值存储(KV Store)
- 多数据中心(Multi Datacenter)
理解Raft协议:http://thesecretlivesofdata.com/raft/ - Agent的启动
我的操作系统是windows所以下面的步骤是windows下安装步骤
1.首先下载consul的windows版本
2.解压到指定文件夹
我的文件夹路径是D:\consul
3.配置环境变量
在path下添加
4.启动consul
cmd下输入consul agent -dev
5.在浏览器中输入localhost:8500
出现下图所示则为正确
consul agent -dev
查看本机信息consul members
创建K/Vconsul kv put abc 123
获取K/Vconsul kv get abc
- UI控制台
localhost:8500/ui
Spring Cloud整合Consul
1.引入依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-lesson-consul-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-lesson-consul-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</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>
2.激活服务发现客户端
使用@EnableDiscoveryClient
注解
package com.example.springcloudlessonconsulclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudLessonConsulClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudLessonConsulClientApplication.class, args);
}
}
利用服务发现API操作
配置应用信息
##应用名称
spring.application.name=spring-cloud-consul
##服务端口
server.port=8080
##配置连接Consul服务器的配置
#Consul主机地址
spring.cloud.consul.host=localhost
##Consul服务端口
spring.cloud.consul.port=8500