Spring Cloud【Finchley】-03将微服务注册到Eureka Server上 + 为Eureka Server添加用户认证

文章目录

  • 概述
  • 将用户微服务micorservice-provider-user注册到Eureka Server上
    • pom中增加 spring-cloud-starter-netflix-eureka-client 依赖
    • 启动类添加@EnableDiscoveryClient注解
    • 配置文件增加配置
    • 测试
  • 将电影微服务micorservice-consumer-movie注册到Eureka Server上
  • 为Eureka Server添加用户认证
    • Eureka Server 添加认证
      • pom添加依赖
      • 配置文件增加认证信息
    • 将微服务注册到需要认证的Eureka Server上
    • 测试
  • 遗留问题
  • Github代码

在这里插入图片描述

概述

Spring Cloud-02服务发现与服务注册Eureka + Eureka Server的搭建中我们介绍了Eureka以及如何搭建Eureka Server端。 那我们这些微服务如何注册到Eureka Server上呢? 这里我们来一起学习下


将用户微服务micorservice-provider-user注册到Eureka Server上

Finchley版本的官方指导文档: https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi__service_discovery_eureka_clients.html


pom中增加 spring-cloud-starter-netflix-eureka-client 依赖

在这里插入图片描述

	<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

因为我在maven父工程中增加了这个依赖,所以这里不用指定spring-cloud-starter-netflix-eureka-client的版本

Spring Cloud【Finchley】-03将微服务注册到Eureka Server上 + 为Eureka Server添加用户认证_第1张图片


启动类添加@EnableDiscoveryClient注解

package com.artisan.microservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication 
@EnableDiscoveryClient
public class MicorserviceSimpleProviderUserApplication {

	public static void main(String[] args) {
		SpringApplication.run(MicorserviceSimpleProviderUserApplication.class, args);
	}
}

在Spring Cloud中服务发现组件还可以选择Consul、Zookeeper等。 @EnableDiscoveryClient为各种组件提供了支持 。 这里也可以使用@EnableEurekaClient代替,表明是Eureka的Client。 当Eureka在项目的classpath中时,两个注解没有区别。


配置文件增加配置

Spring Cloud【Finchley】-03将微服务注册到Eureka Server上 + 为Eureka Server添加用户认证_第2张图片

Spring Cloud【Finchley】-03将微服务注册到Eureka Server上 + 为Eureka Server添加用户认证_第3张图片

spring:
  application:
    name: microservice-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名字

更多请看 https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi__service_discovery_eureka_clients.html#_changing_the_eureka_instance_id


测试

首先启动Eureka Server所在的微服务 microservice-discovery-eureka,然后启动micorservice-provider-user微服务,

成功后,访问 http://localhost:8761/

观察Instances currently registered with Eureka 的信息如下
在这里插入图片描述


将电影微服务micorservice-consumer-movie注册到Eureka Server上

重复如上步骤,测试步骤同上,

在这里插入图片描述

可知,电影微服务和用户微服务都已经注册到Eureka Server上了。


为Eureka Server添加用户认证

官方指导手册:https://cloud.spring.io/spring-cloud-static/Finchley.SR2/multi/multi_spring-cloud-eureka-server.html#_securing_the_eureka_server

Spring Cloud【Finchley】-03将微服务注册到Eureka Server上 + 为Eureka Server添加用户认证_第4张图片

Eureka Server 添加认证

pom添加依赖

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

配置文件增加认证信息

microservice-discovery-eureka 这个微服务的application.yml增加如下信息

Spring Cloud【Finchley】-03将微服务注册到Eureka Server上 + 为Eureka Server添加用户认证_第5张图片

其实经过验证,增加这个就可以完成认证访问了,但是官网上说还要加个

package com.artisan.microservice.eureka;

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);
    }
}

将微服务注册到需要认证的Eureka Server上

micorservice-provider-user 和 micorservice-consumer-movie 微服务

只需要将defaultZone修改为
defaultZone: http://artisan:artisan123@localhost:8761/eureka

测试

启动 microservice-discovery-eureka
然后启动 micorservice-provider-user 和 micorservice-consumer-movie 微服务
访问 http://localhost:8761

Spring Cloud【Finchley】-03将微服务注册到Eureka Server上 + 为Eureka Server添加用户认证_第6张图片

可以看到
在这里插入图片描述


遗留问题

遗留问题,硬编码的问题还是没有解决呢? 接下来就要看Ribbon的了。


Github代码

https://github.com/yangshangwei/SpringCloudMaster/tree/master/micorservice-provider-user

你可能感兴趣的:(【Spring,Cloud,Finchley】,Spring,Cloud,手札,Eureka,Server,Eureka,认证,Spring,Cloud)