您的位置:首页 > 编程语言 > Java开发

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

2018-12-02 22:20 701 查看
版权声明:【show me the code ,change the world】 https://blog.csdn.net/yangshangwei/article/details/84728086

文章目录

  • 将电影微服务micorservice-consumer-movie注册到Eureka Server上
  • 为Eureka Server添加用户认证
  • 将微服务注册到需要认证的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的版本

    启动类添加@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:
    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信息。

    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

    Eureka Server 添加认证

    pom添加依赖

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

    配置文件增加认证信息

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

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

    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

    可以看到

    遗留问题

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

    Github代码

    https://github.com/yangshangwei/SpringCloudMaster

    阅读更多
    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: 
    相关文章推荐