在
微服务
架构中,服务发现(Service Discovery)是关键原则之一。手动配置每个客户端或某种形式的约定是很难做的,并且很脆弱。SpringCloud提供了多种服务发现的实现方式,例如:Eureka、Consul、Zookeeper。
spring Cloud支持得最好的是Eureka,其次是Consul,最次是Zookeeper。
启动工程后,访问:http://loalhost:8761/,如下图。我们会发现此时还没有服务注册到Eureka上面。
创建一个Maven工程(microservice-discovery-eureka),并在pom.xml中加入如下内容:
- <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>com.jb.cloud</groupId>
- <artifactId>microservice-spring-cloud</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- </parent>
- <artifactId>microservice-discovery-eureka</artifactId>
- <packaging>jar</packaging>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka-server</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-security</artifactId>
- </dependency>
- </dependencies>
- </project>
编写SpringBoot启动程序:通过@EnableEurekaServer申明一个注册中心:
- package com.jb.cloud;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
- /**
- * 使用eureka做服务发现
- * @author pangps
- */
- @SpringBootApplication
- @EnableEurekaServer
- public class EurekaApplication {
- public static void main(String[] args) {
- SpringApplication.run(EurekaApplication.class, args);
- }
- }
配置application.yml
- spring:
- profiles:
- active: dev #指定读取的配置文件,默认读取application.yml,如果有指定的会吧默认的配置替换掉
- security:
- basic:
- enabled: true #eureka用户是否认证
- user:
- name: eureka #eureka 用户名
- password: eureka #eureka 密码
- server:
- port: 8761
- eureka:
- instance:
- hostname: localhost #Eureka实例的主机名
- client:
- register-with-eureka: false #在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为
- fetch-registry: false
- service-url:
- defaultZone: http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka
启动工程后,访问:http://loalhost:8761/,如下图。我们会发现此时还没有服务注册到Eureka上面。
按照前文对Eureka的讲解,我们即可构建出一个简单的注册中心。但此时的Eureka是单点的,不适合于生产环境,那么如何实现Eureka的高可用呢?c:/windows/system32/drivers/etc找到hosts文件
- # 添加主机名:
- 7.0.0.1 peer1 peer2 peer3
配置application.yml
- spring:
- application:
- name: EUREKA-HA
- ---
- server:
- port: 7777
- spring:
- profiles: peer1
- eureka:
- instance:
- hostname: peer1
- client:
- serviceUrl:
- defaultZone: http://peer2:8888/eureka/,http://peer3:9999/eureka/
- ---
- server:
- port: 8888
- spring:
- profiles: peer2
- eureka:
- instance:
- hostname: peer2
- client:
- serviceUrl:
- defaultZone: http://peer1:7777/eureka/,http://peer3:9999/eureka/
- ---
- server:
- port: 9999
- spring:
- profiles: peer3
- eureka:
- instance:
- hostname: peer3
- client:
- serviceUrl:
- defaultZone: http://peer1:7777/eureka/,http://peer2:8888/eureka/
访问http://peer1:7777我们会发现已经有peer2、peer3节点了。
如果注册中心是高可用的,那么各个微服务配置只需要将 defaultZone改为如下即可:
- eureka:
- client:
- serviceUrl:
- defaultZone: http://peer1:7777/eureka/,http://peer2:8888/eureka,http://peer3:9999/eureka