SpringCloud Hoxton.SR3 服务注册中心 Eureka

服务注册中心 Eureka

Eureka,是 Spring Cloud Netflix 组件之一。 包含服务治理(Eureka)包括服务注册、服务发现和服务检测监控等。
在Spring Cloud Netflix 整合技术栈中,Eureka既可以作为服务注册中心也可以用于服务发现对整个微服务架构起着最核心的整合作用。
那啥是服务注册中心?
举个例子:某某洗浴中心则作为服务注册中心。
那啥是服务注册?
举个例子:某某洗浴中心有N个PLMM来上班将信息登记到上面去
那啥是服务发现?
举个例子:某男子来到了洗浴中心翻了翻了人员信息找到了某项洗浴服务选了一位对应的PLMM,然后就干了些该干的事情然后就走了。
在微服务架构中,由于每一个服务的粒度相对传统SOA来说要小的多,所以服务的数量会成倍增加。这时如果有效管理服务的注册信息就尤为重要。
在分布式系统中,我们不仅仅是需要在注册中心找到服务和服务地址的映射关系这么简单,我们还需要考虑更多更复杂的问题:

  1. 服务注册后,如何被及时发现
  2. 服务宕机后,如何及时下线
  3. 服务如何有效的水平扩展
  4. 服务发现时,如何进行路由
  5. 服务异常时,如何进行降级
  6. 注册中心如何实现自身的高可用
    这些问题解决都依赖服务注册中心。
    注册中心是微服务架构非常重要的一个组件,管理各种服务功能包括服务的注册、发现、熔断、负载、降级等。可以说是微服务架构中的”通讯录“,它记录了服务和服务地址的映射关系。在分布式架构中,服务会注册到这里,当服务需要调用其它服务时,就到这里找到服务的地址,进行调用。
工程版本信息
  1. SpringCloud Hoxton.SR3
  2. SpringBoot 2.2.1.RELEASE
  3. java1.8
父工程依赖管理
 <dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Hoxton.SR3</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-dependencies</artifactId>
			<version>2.2.1.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>
SpringCloud Eureka 服务注册中心搭建

创建名称为eureka-server的子工程pom信息如下:

<dependencies>
		<!-- Web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- Test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- eureka服务端依赖jar包 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
</dependencies>

创建SpringBoot启动类EurekaServerApplication内容如下:


/**
 * Eureka服务端
 * author: SimpleWu
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}
}

创建application.properties内容如下:

spring.application.name=eureka-server
server.port=23001
#是否向服务注册中心注册自己 
eureka.client.register-with-eureka=false
#是否检索服务
eureka.client.fetch-registry=false
#eureka.client.service-url.defaultZone= http://${spring.security.user.name}:${spring.security.user.password}@localhost:${server.port}/eureka
eureka.client.service-url.defaultZone= http://localhost:${server.port}/eureka

到了这里直接启动EurekaServerApplication main方法。
启动成功后可以访问:http://localhost:23001/ 就能够查看到Eureka Web界面。

Eureka增加安全认证

增加依赖:

<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-security</artifactId>
</dependency>

增加properties配置:

#spring security 用户密码
spring.security.user.name=root
spring.security.user.password=123456

关闭csrf:

/**
 * 关闭 csrf
 * @author SimpleWu
 */
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        //http.authorizeRequests().anyRequest().permitAll().and().logout().permitAll();
        super.configure(http);
    }
    
}

然后我们重新访问的时候就需要输入账号与密码才能够放文档Eureka服务。
客户端连接Eureka需要携带账号密码properties配置可以使用

eureka.client.service-url.defaultZone= http://${spring.security.user.name}:${spring.security.user.password}@localhost:${server.port}/eureka
Eureka集群模式

3个服务组成一组通用的高可用服务分别为23001端口,23002端口,23003端口
配置Host三个实例服务映射:
127.0.0.1 eureka001
127.0.0.1 eureka002
127.0.0.1 eureka003
然后分别启动3个Eureka服务,配置分别如下:
eureka001:

server.port=23001
eureka.instance.hostname=eureka001
eureka.client.service-url.defaultZone= http://eureka002:23002/eureka,http://eureka003:23003/eureka

eureka002:

server.port=23002
eureka.instance.hostname=eureka002
eureka.client.service-url.defaultZone= http://eureka001:23001/eureka,http://eureka003:23003/eureka

eureka003:

server.port=23003
eureka.instance.hostname=eureka003
eureka.client.service-url.defaultZone= http://eureka001:23001/eureka,http://eureka003:23003/eureka

客户端(服务提供方配置):

eureka.client.service-url.defaultZone= http://eureka001:23001/eureka,http://eureka002:23002/eureka,http://eureka003:23003/eureka

到这里我们Eureka高可用集群就搭建成功了,可以客户端只向Eureka001注册服务,euereka002,eureka003中也会同步到euerka001的服务节点数据。

Eureka监听事件
  1. EurekaInstanceCanceledEvent 服务下线事件
  2. EurekaInstanceRegisteredEvent 服务注册事件
  3. EurekaInstanceRenewedEvent 服务续约事件
  4. EurekaRegistryAvailableEvent Eureka注册中心启动事件
  5. EurekaServerStartedEvent Eureka Server启动事件
    只需要在方法上标注@EventListener注解就可以监听到以上事件案例如下:
@EventListener
public void listen(EurekaInstanceCanceledEvent  event) {
	logger.info("服务{}已下线", event.getAppName());
	logger.info("server地址信息{}", event.getServerId());
	//邮箱通知给管理员发送警告
}
posted @ 2019-07-12 17:05  日落西风又在吹  阅读(4727)  评论(1编辑  收藏  举报