Spring Cloud-03将微服务注册到Eureka Server上 + 为Eureka Server添加用户认证
文章目录
- 概述
- 将用户微服务micorservice-provider-user注册到Eureka Server上 < 4000 ul>
- pom中增加 spring-cloud-starter-netflix-eureka-client 依赖
- 启动类添加@EnableDiscoveryClient注解
- 配置文件增加配置
- 测试
概述
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名字
测试
首先启动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添加用户认证
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代码
阅读更多- Spring Cloud-03将微服务注册到Eureka Server上 + 为Eureka Server添加用户认证
- SpringCloud(第 004 篇)用户服务类(添加服务注册,将用户微服务注册到 EurekaServer 中)
- SpringCloud(第 004 篇)用户服务类(添加服务注册,将用户微服务注册到 EurekaServer 中)
- SpringCloud 微服务注册与发现 – 为Eureka Server添加用户认证
- spring cloud-给Eureka Server加上安全的用户认证
- SpringCloud---Eureka添加用户认证
- 学习Spring Cloud第六课(将微服务注册到Eureka Server上)
- 学习Spring Cloud第六课(将微服务注册到Eureka Server上)
- [Spring Cloud-01] eureka server 服务注册与发现
- springcloud 学习-eureka搭建-为eureka添加认证
- springcloud(二):spring cloud eureka 注册中心server 启动
- SpringCloud服务注册到Eureka Server
- 服务注册和服务发现-Eureka的服务认证和集群--Spring Cloud Netflix
- [bigdata-105] spring-cloud-02 服务注册 eureka server 双机高可用
- SpringCloud(第 004 篇)用户服务类(添加服务注册,将用户微服务注册到 EurekaServer 中)
- SpringCloud的EurekaClient : 客户端应用访问注册的微服务(有断路器场景)
- spring-cloud-eureka-server 注册中心启动不显示页面而是xml问题
- 《Spring Cloud Netflix》-- 服务注册和服务发现-Eureka的服务认证和集群
- (2-2)SpringCloud-服务注册到Eureka Server集群并消费
- spring-cloud-netflix eureka服务添加基本用户验证