文本 Spring Cloud是一个微服务框架,相比Dubbo等RPC框架, Spring Cloud提供的全套的分布式系统解决方案。java
Spring Cloud对微服务基础框架Netflix的多个开源组件进行了封装,同时又实现了和云端平台以及和Spring Boot开发框架的集成。web
Spring Cloud为微服务架构开发涉及的配置管理,服务治理,熔断机制,智能路由,微代理,控制总线等spring
Eureka是Netflix的一个核心子模块,Eureka是基于的Rest服务,用于定位服务。服务的注册与发现对于微服务架构来讲是很是重要的,通过配置以后访问服务只需提供服务标识便可。其功能相似于dubbo的注册中心zookeeper。apache
分布式系统中通常须要遵循CAP原则:CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),三者不可兼得(摘自百度百科)。安全
Eureka和zookeeper不一样点在于,zookeeper是基于CP原则构建,而Eureka是基于AP原则构建。即任什么时候刻zookeeper的访问请求都能获得一致性的结果,可是其并不保证服务的可用性,zookeeper的选举机制在节点多了以后会很是耗时。在Eureka平台中不会出现相似于zookeeper的选举leader的过程,若是某台服务器宕机,Eureka会自动切换到新的Eureka节点,当服务器恢复以后,Eureka会从新将其归入服务器集群管理当中。bash
开发环境:
开发工具:Spring Tool Suite 4
JDK版本:1.8
SpringBoot版本:2.0.9.RELEASE
SpringCloud版本:Finchley.SR3
复制代码
File-->New-->Maven Project,选中Create a simple project(skip archetype selection) 点击Next,输入相应的groupId和ArtifactId,packaging修改成pom,点击完成,建立成功。此parent项目为整个springcloud的父项目,主要用于版本控制和公共jar包引入。服务器
<?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.hewl</groupId>
<artifactId>springcloud-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR3</spring-cloud.version>
</properties>
<dependencies>
<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>
复制代码
右键cloud-demo-parent项目-->Maven-->New Maven Module Project,Module Name输入cloud-demo-eureka-server,点击Finish完成建立,PS:若是项目出现红叉,请Maven-->update project架构
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hewl</groupId>
<artifactId>cloud-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>cloud-demo-eureka-server</artifactId>
<name>cloud-demo-eureka-server</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency> -->
</dependencies>
</project>
复制代码
server:
port: 8761 # 你的端口
spring:
# security:
# user:
# name: admin
# password: admin
application:
name: service-registry
eureka:
instance:
hostname: localhost # 你的地址
instance-id: ${spring.application.name}:${server.port}:@project.version@
prefer-ip-address: true
client:
registerWithEureka: false # 表示是否注册自身到eureka服务器,由于当前这个应用就是eureka服务器,不必注册自身,因此这里是false
fetchRegistry: false # fetchRegistry表示是否从eureka服务器获取注册信息
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
复制代码
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hewl</groupId>
<artifactId>cloud-demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>cloud-demo-eureka-client</artifactId>
<name>cloud-demo-eureka-client</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
复制代码
spring:
application:
name: service-eureka-client #服务名称
server:
port: 8800 #端口
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #服务中心地址
复制代码
package com.hewl.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${server.port}")
private String port;
@GetMapping("/test")
public String testMethod(@RequestParam("name") String name) {
return "hello world!!! 端口为:" + port + "名字为:" + name;
}
}
复制代码
像eureka server的这种管理页面,若是随便登陆是很是不安全的,那么怎么办呢?咱们这里引入这个jar包,给eureka server管理页面增长一个登陆验证。app
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
复制代码
server:
port: 8761 # 你的端口
spring:
security:
user:
name: admin
password: admin
application:
name: service-registry
eureka:
instance:
hostname: localhost # 你的地址
client:
registerWithEureka: false # 表示是否注册自身到eureka服务器,由于当前这个应用就是eureka服务器,不必注册自身,因此这里是false
fetchRegistry: false # fetchRegistry表示是否从eureka服务器获取注册信息
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
复制代码
spring:
application:
name: service-eureka-client #服务名称
server:
port: 8800 #端口
eureka:
client:
service-url:
defaultZone: http://admin:admin@localhost:8761/eureka/ #服务中心地址
复制代码