注意, 使用SpringBoot2.2.5以上版本时, 运行Eureka要使用Hoxton.SR1以上版本的SpringCloudjava
<?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>SpringCloud</artifactId> <groupId>com.wang</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>SpringCloud-Eureka-7001</artifactId> <!--导包--> <dependencies> <!--Eureka server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--热部署工具--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
注意web
server: port: 7001 #Eureka配置 eureka: instance: hostname: localhost #Eureka服务端的实例名称 client: register-with-eureka: false #表示是否向Eureka注册中心注册本身, 因为咱们这里是服务端, 不须要注册本身 fetch-registry: false # 若是 fetch-registry 为false, 则表示本身为注册中心 service-url: #监控页面 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #这里使用${}动态配置, hostname和端口在上面定义了
注意spring
此处在resources下的 application.yaml 中配置apache
url使用${}动态配置架构
url最后必须是/eureka/, 千万不能写错, 不然没法注册app
下面的配置很重要, 是代表本身是注册中心的重要配置maven
client: register-with-eureka: false fetch-registry: false
package com.wang.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication //表示他是一个服务端的启动类, 能够接受别人注册进来 @EnableEurekaServer //启动以后访问 http://localhost:7001/ 就行了 public class EurekaServer_7001 { public static void main(String[] args) { SpringApplication.run(EurekaServer_7001.class, args); } }
注意ide
这里是将provider添加到Eureka中spring-boot
因为consumer是经过RestTemplate访问url的, 所以不用Eureka管理微服务
<!--Eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency>
注意
在provider中的配置文件中, 添加如下配置
#Eureka配置, 配置该服务注册到哪里(与Server中的url地址一致) eureka: client: service-url: defaultZone: http://localhost:7001/eureka/ instance: instance-id: springcloud-provider-dept8001 #修改Eureka上的默认描述信息
注意
url与注册中心保持一致, 这样才能注册到注册中心去
经过 eureka.instance.instance-id = XXX 能够修改Eureka上的默认描述信息
package com.wang.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 //在服务启动后自动将该服务注册到Eureka中 @EnableEurekaClient public class DeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(DeptProvider_8001.class, args); } }
注意
停掉8001端口的服务,过一段时间后Eureka会显示如下内容
这是一种保护机制: 好死不如赖活着
它的架构哲学是宁肯保留错误的注册服务信息, 也不盲目注销任何可能健康的服务实例
可使用 eureka.server.enable-self-preservation = false 来禁用自我保护机制(不推荐关闭!)
为了方便与多人协同开发, 提供一些服务的信息是十分必要的, 这里依旧是对provider进行操做
<!--actuator 完善监控信息--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
在application.yaml中, 添加以下的配置
#监控信息配置 info info: app.name: wang-springcloud company.name: wang.study.com
在controller中, 添加获取服务的内容, 这里为了方便查看, 打印到了控制台
package com.wang.springcloud.controller; import com.wang.springcloud.pojo.Dept; import com.wang.springcloud.service.DeptService; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.*; import java.util.List; //提供RestFul服务! @RestController @ApiModel("Provider Controller") public class DeptController { //注册DiscoveryClient, 注意此时要导入的包是SpringCloud的 //获取一些配置的信息, 获得具体的微服务 @Autowired private DiscoveryClient client; //注册进来的微服务, 得到一些信息(获得配置文件中的info的信息) @ApiOperation("微服务的信息") @GetMapping("/dept/discovery") public Object discovery() { //获取微服务列表的清单 List<String> services = client.getServices(); System.out.println("discovery => services: " + services); //获得一个具体的微服务, 经过具体的微服务ID, applicationName(即为在配置文件中配置的该SpringBoot的名字!) List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT"); for (ServiceInstance instance : instances) { System.out.println( instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri() + "\t" + instance.getServiceId() ); } //返回这个client就能够了 return this.client; } }
注意
要自动装配DiscoveryClient, 要导入的包是SpringCloud的
返回值为当前的DiscoveryClient对象
微服务的ID为在配置文件中配置的 spring.application.name 的名字的大写
info配置后, 点击status会跳转, 页面显示在配置文件中配置的内容
package com.wang.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 //在服务启动后自动将该服务注册到Eureka中 @EnableEurekaClient //服务发现, 这样就能够监控了 @EnableDiscoveryClient public class DeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(DeptProvider_8001.class, args); } }
注意
咱们配置多个Eureka注册中心来注册服务
集群的好处: 若是一个注册中心崩掉, 不会影响整个服务, 只须要切换端口就能够了
这里添加了7002和7003两个端口的注册中心
因为是在单机测试, 要修改hosts文件,理由以下
在单机上测试集群要注意, hostname不能使用同一个localhost, 要在hosts文件中用多个name映射127.0.0.1端口, 这是因为Eureka会把hostname相同的url移除掉
127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com
这里须要将三个注册中心经过url两两关联, 以7001端口为例, 关联7002和7003端口的注册中心
server: port: 7001 #Eureka配置 eureka: instance: hostname: localhost #Eureka服务端的实例名称 client: register-with-eureka: false #表示是否向Eureka注册中心注册本身, 因为咱们这里是服务端, 不须要注册本身 fetch-registry: false # 若是 fetch-registry 为false, 则表示本身为注册中心 service-url: #监控页面 #集群(与其余的Eureka关联) 这里绑定7002和7003端口的Eureka defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
注意
只须要修改provider发布的url便可, 将三个注册中心的url都添加到配置文件中就行了
#Eureka配置, 配置该服务注册到哪里(与Server中的url地址一致) eureka: client: service-url: #向集群发布, 只须要向全部的Eureka发布url就能够了 defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/ instance: instance-id: springcloud-provider-dept8001 #修改Eureka上的默认描述信息
将三个注册中心以及provider服务都启动, 最终能够看到实现了集群的Eureka