服务发现与服务注册Eureka + Eureka Server的搭建
通过配置文件的方式来配置服务提供者的地址,有两个缺点
1、如果服务提供者的ip和端口发生变化,将会影响服务消费者,服务消费者需要也跟着修改
2、高可用需要依赖第三方,通常情况下每个微服务都会部署多个实例,实现负载均衡和容灾
在微服务中,服务发现组件的位置如下

关系如上图
·1、各个微服务在启动时,将自己的网络地址等信息注册到服务发现组件中
2、服务消费者可以从服务发现组件中查询服务提供者的地址,并使用该地址调用服务提供者的提供的接口
3、各个微服务与服务发现组件在使用一定的机制(比如心跳机制)来通信。当服务发现组件长时间无法与某微服务实例通信,将注销该实例
4、微服务网络地址发生变更(比如实例增减或者ip端口发生变化)时,会重新注册到服务发现组件。 使用这种方式,服务消费者就无需人工修改提供者的网络地址。
Eureka概述
Eureka是Netflix开源的服务发现组件,一个基于REST的服务。 包含Server和Client两部分。 Spring Cloud将它集成在子项目Spring Cloud Netflix中,从而实现微服务的注册与发现
https://github.com/Netflix/eureka

由上图可知,Eureka包含 Eureka Server和 Eureka Client
Eureka Server: 提供服务发现的能力,各个微服务启动时,会向Eureka Server注册信息,比如ip、端口、微服务名称等。 Eureka Server会存储这些信息
Eureka Client: Java 客户端,用于简化与Eureka Server的交互
微服务启动后,会周期性(默认30S)向Eureka Server发送心跳以续约自己的“租期”
如果Eureka Server在一定时间内(默认90S)没有接收到某个微服务实例的心跳,Eureka Server将注销该实例。
默认情况下,Eureka Server 同时也是 Eureka Client . 多个Eureka Server之间通过复制的方式来实现服务注册表中数据的同步
Eureka Client会缓存服务注册表中的信息,两个好处 第一,微服务无需每次都请求查询Eureka Server ,降低Server的压力。 第二,即使Eureka Server所有节点都宕掉,服务消费者依然可以使用缓存中的信息找到服务提供者并完成调用。
Maven父子工程的搭建
父类pom.xml
<?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.smart</groupId> <artifactId>eurekademo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <modules> <module>consumer-movie</module> <module>provider-user</module> <module>discovery-eureka</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</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>
discover-eureka子模块的pom.xml
<?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>com.smart</groupId> <artifactId>eurekademo</artifactId> <version>1.0-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.smart</groupId> <artifactId>discovery-eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <name>discovery-eureka</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
启动类增加@EnableEurekaServer注解
package com.smart.discoveryeureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class DiscoveryEurekaApplication { public static void main(String[] args) { SpringApplication.run(DiscoveryEurekaApplication.class, args); } }
配置文件配置Eureka等信息
server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:8761/eureka
eureka.client.register-with-eureka: 是否将自己注册到Eureka Server ,默认为true.因为当前应用是作为Eureka Server用,因此设置为false
eureka.client.fetch-registry:是否从Eureka Server获取注册信息,默认为true, 因为我们这里是个单节点的Eureka Server ,不需要与其他的Eureka Server节点的数据,因此设为false
eureka.client.service-url.defaultZone : 设置与Eureka Server交互的地址,查询服务和注册服务都依赖这个地址,默认为 http://localhost:8761/eureka ,多个地址可使用 , 分隔。
用户微服务micorservice-provider-user注册到Eureka Server上
pom中增加 spring-cloud-starter-netflix-eureka-client 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
启动类添加@EnableDiscoveryClient注解
@SpringBootApplication @EnableDiscoveryClient public class ProviderUserApplication { public static void main(String[] args) { SpringApplication.run(ProviderUserApplication.class, args); } }
在Spring Cloud中服务发现组件还可以选择Consul、Zookeeper等。 @EnableDiscoveryClient为各种组件提供了支持 。 这里也可以使用@EnableEurekaClient代替,表明是Eureka的Client。 当Eureka在项目的classpath中时,两个注解没有区别。
配置文件增加配置
spring: application: name: provider-user # 指定注册到Eureka的应用的名字,建议全部小写 #eureka eureka: client: service-url: defaultZone: http://localhost:8761/eureka instance: prefer-ip-address: true instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
spring.application.name 指定注册到Eureka Server上的应用名字
prefer-ip-address: true, 页面上还不是显示的ip地址,但是将鼠标悬停在这个上面,左下角会显示IP地址,并且点这个链接进去的话,可以在浏览器的地址栏看到ip信息
instance-id : 服务中心现实的eureka instance名字
Eureka Server 添加认证
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
配置文件增加认证信息
微服务的application.yml增加如下信息
server:
port: 8761
spring:
security:
user:
name: Jim
password: land123
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://Jim:land123@localhost:8761/eureka
增加配置文件
package com.smart.discoveryeureka.config; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().ignoringAntMatchers("/eureka/**"); super.configure(http); } }
provider-user 和 consumer-movie 微服务要修改defaultZone
defaultZone: http://Jim:land123@localhost:8761/eureka
使用:http://localhost:8761/login测试
浙公网安备 33010602011771号