CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),三者不可兼得。java
https://baike.baidu.com/item/CAP%E5%8E%9F%E5%88%99/5712863?fr=aladdinmysql |
Eureka是Netflix的一个子模块,也是核心模块之一。Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移。服务注册与发现对于微服务架构来讲是很是重要的,有了服务发现与注册,只须要使用服务的标识符,就能够访问到服务,而不须要修改服务调用的配置文件了。功能相似于dubbo的注册中心,好比Zookeeper。git
https://github.com/Netflix/eurekagithub |
Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现(请对比Zookeeper)。web
Eureka 采用了 C-S 的设计架构。Eureka Server 做为服务注册功能的服务器,它是服务注册中心。算法
而系统中的其余微服务,使用 Eureka Client(Eureka客户端)链接到 Eureka Server并维持心跳链接。这样系统的维护人员就能够经过 Eureka Server 来监控系统中各个微服务是否正常运行。SpringCloud 的一些其余模块(好比Zuul)就能够经过 Eureka Server 来发现系统中的其余微服务,并执行相关的逻辑。spring
Eureka和Dubbo的架构对比sql
Eureka包含两个组件:Eureka Server和Eureka Client,Eureka Server提供服务注册服务;各个节点启动后,会在EurekaServer中进行注册,这样urekaServer中的服务注册表中将会存储全部可用服务节点的信息,服务节点的信息能够在界面中直观的看到。数据库
EurekaClient是一个Java客户端,用于简化Eureka Server的交互,客户端同时也 具有一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。若是Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)。apache
Eureka Server 提供服务注册和发现。
Service Provider服务提供方将自身服务注册到Eureka,从而使服务消费方可以找到。
Service Consumer服务消费方从Eureka获取注册服务列表,从而可以消费服务。
总父工程
通用模块api
服务提供者Provider
服务消费者Consumer
microservicecloud-eureka-7001 eureka服务注册中心Module
<?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"> <parent> <artifactId>microservicecloud</artifactId> <groupId>com.hosystem</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-eureka-7001</artifactId> <dependencies> <!--eureka-server服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- 修改后当即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
</project> |
server: port: 7001
eureka: instance: hostname: localhost #eureka服务端的实例名称 client: register-with-eureka: false #false表示不向注册中心注册本身。 fetch-registry: false #false表示本身端就是注册中心,个人职责就是维护服务实例,并不须要去检索服务 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都须要依赖这个地址。 |
package com.hosystem.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication @EnableEurekaServer //EurekaServer服务器端启动类,接受其它微服务注册进来 public class EurekaServer7001_App { public static void main(String[] args) { SpringApplication.run(EurekaServer7001_App.class, args); } } |
http://localhost:7001/ |
No application available(没有服务被发现),由于没有注册服务进来固然不可能有服务被发现.
microservicecloud-provider-dept-8001将已有的部门微服务注册进eureka服务中心。
新添加的部分:
<!-- 将微服务provider侧注册进eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> |
完整的部分:
<?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"> <parent> <artifactId>microservicecloud</artifactId> <groupId>com.hosystem</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-provider-dept-8001</artifactId>
<dependencies> <dependency><!-- 引入本身定义的api通用包,可使用Dept部门Entity --> <groupId>com.hosystem</groupId> <artifactId>microservicecloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 修改后当即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
<!-- 将微服务provider侧注册进eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies>
</project> |
新添加的部分:
eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7001/eureka |
完整的部分:
server: port: 8001
mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.hosytem.springcloud.entities # 全部Entity别名类所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件
#name spring.application.name=microservicecloud-dept 很重要很重要很重要 spring: application: name: microservicecloud-dept datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操做类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://192.168.188.188:3306/cloudDB01 # 数据库名称 username: root password: 123456 dbcp2: min-idle: 5 # 数据库链接池的最小维持链接数 initial-size: 5 # 初始化链接数 max-total: 5 # 最大链接数 max-wait-millis: 200 # 等待链接获取的最大超时时间 eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7001/eureka |
package com.hosystem.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication @EnableEurekaClient //本服务启动后会自动注册进eureka服务中 public class DeptProvider8001_App { public static void main(String[] args) { SpringApplication.run(DeptProvider8001_App.class, args); } } |
须要先启动EurekaServer在启动provider
Application中MICROSERVICECLOUD-DEPT如何获得.
如何去掉主机名称
修改microservicecloud-provider-dept-8001的application.yml文件。
修改部分:
eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7001/eureka instance: instance-id: microservicecloud-dept8001 |
完整部分:
server: port: 8001
mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.hosytem.springcloud.entities # 全部Entity别名类所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件
#name spring.application.name=microservicecloud-dept 很重要很重要很重要 spring: application: name: microservicecloud-dept datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操做类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://192.168.188.188:3306/cloudDB01 # 数据库名称 username: root password: 123456 dbcp2: min-idle: 5 # 数据库链接池的最小维持链接数 initial-size: 5 # 初始化链接数 max-total: 5 # 最大链接数 max-wait-millis: 200 # 等待链接获取的最大超时时间
eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7001/eureka instance: instance-id: microservicecloud-dept8001 |
如何设置访问信息有IP信息提示
修改microservicecloud-provider-dept-8001的application.yml文件。
修改部分:
eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7001/eureka instance: instance-id: microservicecloud-dept8001 prefer-ip-address: true #访问路径能够显示IP地址 |
完整代码
server: port: 8001
mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.hosytem.springcloud.entities # 全部Entity别名类所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件
#name spring.application.name=microservicecloud-dept 很重要很重要很重要 spring: application: name: microservicecloud-dept datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操做类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://192.168.188.188:3306/cloudDB01 # 数据库名称 username: root password: 123456 dbcp2: min-idle: 5 # 数据库链接池的最小维持链接数 initial-size: 5 # 初始化链接数 max-total: 5 # 最大链接数 max-wait-millis: 200 # 等待链接获取的最大超时时间
eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7001/eureka instance: instance-id: microservicecloud-dept8001 prefer-ip-address: true #访问路径能够显示IP地址 |
如何配置服务报告info
修改microservicecloud-provider-dept-8001的Pom文件
修改部分:
<!-- actuator监控信息完善 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> |
完整部分:
<?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"> <parent> <artifactId>microservicecloud</artifactId> <groupId>com.hosystem</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-provider-dept-8001</artifactId>
<dependencies> <dependency><!-- 引入本身定义的api通用包,可使用Dept部门Entity --> <groupId>com.hosystem</groupId> <artifactId>microservicecloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 修改后当即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
<!-- 将微服务provider侧注册进eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
<!-- actuator监控信息完善 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
</project> |
总的父工程microservicecloud修改pom.xml添加构建build信息。
修改部分:
<!--构建的build信息--> <build> <finalName>microservicecloud</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <delimiters> <delimit>$</delimit> </delimiters> </configuration> </plugin> </plugins> </build> |
完整部分:
<?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.hosystem</groupId> <artifactId>microservicecloud</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <!--注:这行最开始建立的时候是不存在的--> <modules> <module>microservicecloud-api</module> <module>microservicecloud-provider-dept-8001</module> <module>microservicecloud-consumer-dept-80</module> <module>microservicecloud-eureka-7001</module> </modules>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>4.12</junit.version> <log4j.version>1.2.17</log4j.version> <lombok.version>1.16.18</lombok.version> </properties>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.4</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.31</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> </dependencyManagement>
<!--构建的build信息--> <build> <finalName>microservicecloud</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <delimiters> <delimit>$</delimit> </delimiters> </configuration> </plugin> </plugins> </build>
</project> |
修改microservicecloud-provider-dept-8001的yml文件。
修改部分:
info: app.name: hosystem-microservicecloud company.name: www.hosystem.com build.artifactId: $project.artifactId$ build.version: $project.version$ |
完整部分:
server: port: 8001
mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.hosytem.springcloud.entities # 全部Entity别名类所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件
#name spring.application.name=microservicecloud-dept 很重要很重要很重要 spring: application: name: microservicecloud-dept datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操做类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://192.168.188.188:3306/cloudDB01 # 数据库名称 username: root password: 123456 dbcp2: min-idle: 5 # 数据库链接池的最小维持链接数 initial-size: 5 # 初始化链接数 max-total: 5 # 最大链接数 max-wait-millis: 200 # 等待链接获取的最大超时时间
eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7001/eureka instance: instance-id: microservicecloud-dept8001 prefer-ip-address: true #访问路径能够显示IP地址 info: app.name: hosystem-microservicecloud company.name: www.hosystem.com build.artifactId: $project.artifactId$ build.version: $project.version$ |
简单来讲,某时刻某个微服务不可用,eureka不会马上清理,依旧会对该微服务的信息进行保存。
默认状况下,若是EurekaServer在必定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。可是当网络分区故障发生时,微服务与EurekaServer之间没法正常通讯,以上行为可能变得很是危险了——由于微服务自己实际上是健康的,此时本不该该注销这个微服务。Eureka经过“自我保护模式”来解决这个问题——当EurekaServer节点在短期内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进入该模式,EurekaServer就会保护服务注册表中的信息,再也不删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式。
在自我保护模式中,Eureka Server会保护服务注册表中的信息,再也不注销任何服务实例。当它收到的心跳数从新恢复到阈值以上时,该Eureka Server节点就会自动退出自我保护模式。它的设计哲学就是宁肯保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。
综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁肯同时保留全部微服务(健康的微服务和不健康的微服务都会保留),也不盲目注销任何健康的微服务。使用自我保护模式,可让Eureka集群更加的健壮、稳定。
在Spring Cloud中,可使用eureka.server.enable-self-preservation = false 禁用自我保护模式。
microservicecloud-provider-dept-8001服务发现Discovery。
对于注册进eureka里面的微服务,能够经过服务发现来得到该服务的信息
修改microservicecloud-provider-dept-8001工程的DeptController
修改部分:
//注:若是Autowired显示 could not autowired.there is more than one bean of 'DiscoveryClient' type; 使用一下两种方法都行 //方法1:那么只须要添加@Qualifier("discoveryClient")便可 //方法2::固然也能够修改为 private DiscoveryClient discoveryClient; 可是咱们也须要修改discovery()方法中client为discoveryClient @Qualifier("discoveryClient") @Autowired private DiscoveryClient client;
@RequestMapping(value = "/dept/discovery", method = RequestMethod.GET) public Object discovery() { List<String> list = client.getServices(); System.out.println("**********" + list);
List<ServiceInstance> srvList = client.getInstances("MICROSERVICECLOUD-DEPT"); for (ServiceInstance element : srvList) { System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t" + element.getUri()); } return this.client; } |
完整部分:
package com.hosystem.springcloud.controller;
import com.hosystem.springcloud.entities.Dept; import java.util.List;
import com.hosystem.springcloud.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
@RestController public class DeptController { @Autowired private DeptService service;
//注:若是Autowired显示 could not autowired.there is more than one bean of 'DiscoveryClient' type; 使用一下两种方法都行 //方法1:那么只须要添加@Qualifier("discoveryClient")便可 //方法2::固然也能够修改为 private DiscoveryClient discoveryClient; 可是咱们也须要修改discovery()方法中client为discoveryClient @Qualifier("discoveryClient") @Autowired private DiscoveryClient client;
@RequestMapping(value = "/dept/add", method = RequestMethod.POST) public boolean add(@RequestBody Dept dept) { return service.add(dept); }
@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET) public Dept get(@PathVariable("id") Long id) { return service.get(id); }
@RequestMapping(value = "/dept/list", method = RequestMethod.GET) public List<Dept> list() { return service.list(); }
@RequestMapping(value = "/dept/discovery", method = RequestMethod.GET) public Object discovery() { List<String> list = client.getServices(); System.out.println("**********" + list);
List<ServiceInstance> srvList = client.getInstances("MICROSERVICECLOUD-DEPT"); for (ServiceInstance element : srvList) { System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t" + element.getUri()); } return this.client; }
} |
package com.hosystem.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication @EnableEurekaClient //本服务启动后会自动注册进eureka服务中 @EnableDiscoveryClient public class DeptProvider8001_App { public static void main(String[] args) { SpringApplication.run(DeptProvider8001_App.class, args); } } |
修改microservicecloud-consumer-dept-80工程的DeptController_Consumer
修改部分:
//测试@EnableDiscoveryClient,消费端能够调用服务发现 @RequestMapping(value="/consumer/dept/discovery") public Object discovery() { return restTemplate.getForObject(REST_URL_PREFIX+"/dept/discovery", Object.class); } |
完整部分:
package com.hosystem.springcloud.controller;
import com.hosystem.springcloud.entities.Dept; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;
@RestController //注:这里必定不能忘记注解@RestController 不然出现404; public class DeptController_Consumer {
/** * 使用restTemplate访问restful接口很是的简单; * (url, requestMap, ResponseBean.class)三个参数表明REST请求地址、请求参数、HTTP响应转换被转换成的对象类型 */ private static final String REST_URL_PREFIX = "http://localhost:8001";
@Autowired private RestTemplate restTemplate;
@RequestMapping(value="/consumer/dept/add") public boolean add(Dept dept) { return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add", dept, Boolean.class); }
@RequestMapping(value="/consumer/dept/get/{id}") public Dept get(@PathVariable("id") Long id) { return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id, Dept.class); }
//@SuppressWarnings("unchecked"):压制警告,由于咱们使用了过时的方法 @SuppressWarnings("unchecked") @RequestMapping(value="/consumer/dept/list") public List<Dept> list() { return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list", List.class); }
//测试@EnableDiscoveryClient,消费端能够调用服务发现 @RequestMapping(value="/consumer/dept/discovery") public Object discovery() { return restTemplate.getForObject(REST_URL_PREFIX+"/dept/discovery", Object.class); } } /** * JDBC Spring JDBCTemplate * Spring RestTemplate */ |
上图是来自eureka的官方架构图,这是基于集群配置的eureka;
- 处于不一样节点的eureka经过Replicate进行数据同步
- Application Service为服务提供者
- Application Client为服务消费者
- Make Remote Call完成一次服务调用
服务启动后向Eureka注册,Eureka Server会将注册信息向其余Eureka Server进行同步,当服务消费者要调用服务提供者,则向服务注册中心获取服务提供者地址,而后会将服务提供者地址缓存在本地,下次再调用时,则直接从本地缓存中取,完成一次调用。
当服务注册中心Eureka Server检测到服务提供者由于宕机、网络缘由不可用时,则在服务注册中心将服务置为DOWN状态,并把当前服务提供者状态向订阅者发布,订阅过的服务消费者更新本地缓存。
服务提供者在启动后,周期性(默认30秒)向Eureka Server发送心跳,以证实当前服务是可用状态。Eureka Server在必定的时间(默认90秒)未收到客户端的心跳,则认为服务宕机,注销该实例。
新建microservicecloud-eureka-7002/microservicecloud-eureka-7003
配置microservicecloud-eureka-7002的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>microservicecloud</artifactId> <groupId>com.hosystem</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-eureka-7002</artifactId> <dependencies> <!--eureka-server服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- 修改后当即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
</project> |
配置microservicecloud-eureka-7002的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>microservicecloud</artifactId> <groupId>com.hosystem</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-eureka-7003</artifactId> <dependencies> <!--eureka-server服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- 修改后当即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
</project> |
package com.hosystem.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication @EnableEurekaServer //EurekaServer服务器端启动类,接受其它微服务注册进来 public class EurekaServer7002_App { public static void main(String[] args) { SpringApplication.run(EurekaServer7002_App.class, args); } } |
package com.hosystem.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication @EnableEurekaServer //EurekaServer服务器端启动类,接受其它微服务注册进来 public class EurekaServer7003_App { public static void main(String[] args) { SpringApplication.run(EurekaServer7003_App.class, args); } } |
127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com |
server: port: 7001
eureka: instance: hostname: eureka7001.com #eureka服务端的实例名称 client: register-with-eureka: false #false表示不向注册中心注册本身。 fetch-registry: false #false表示本身端就是注册中心,个人职责就是维护服务实例,并不须要去检索服务 service-url: #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都须要依赖这个地址(单机)。 defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ |
server: port: 7002
eureka: instance: hostname: eureka7002.com #eureka服务端的实例名称 client: register-with-eureka: false #false表示不向注册中心注册本身。 fetch-registry: false #false表示本身端就是注册中心,个人职责就是维护服务实例,并不须要去检索服务 service-url: #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都须要依赖这个地址。 defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/ |
server: port: 7003
eureka: instance: hostname: eureka7003.com #eureka服务端的实例名称 client: register-with-eureka: false #false表示不向注册中心注册本身。 fetch-registry: false #false表示本身端就是注册中心,个人职责就是维护服务实例,并不须要去检索服务 service-url: #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都须要依赖这个地址。 defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/ |
配置microservicecloud-provider-dept-8001的applicaiton.yml文件
server: port: 8001
mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.hosytem.springcloud.entities # 全部Entity别名类所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件
#name spring.application.name=microservicecloud-dept 很重要很重要很重要 spring: application: name: microservicecloud-dept datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操做类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://192.168.188.188:3306/cloudDB01 # 数据库名称 username: root password: 123456 dbcp2: min-idle: 5 # 数据库链接池的最小维持链接数 initial-size: 5 # 初始化链接数 max-total: 5 # 最大链接数 max-wait-millis: 200 # 等待链接获取的最大超时时间
eureka: client: #客户端注册进eureka服务列表内 service-url: # defaultZone: http://localhost:7001/eureka defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: instance-id: microservicecloud-dept8001 prefer-ip-address: true #访问路径能够显示IP地址 info: app.name: hosystem-microservicecloud company.name: www.hosystem.com build.artifactId: $project.artifactId$ build.version: $project.version$ |
在IDEA的Run Dashboard中运行eureka700一、eureka700二、eureka7003
做为服务注册中心,Eureka比Zookeeper好在哪里。
CAP:https://baike.baidu.com/item/CAP%E5%8E%9F%E5%88%99/5712863?fr=aladdin
参考文档:https://baike.baidu.com/item/CAP%E5%8E%9F%E5%88%99/5712863?fr=aladdin