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的版本
启动类添加@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/eurekainstance:prefer-ip-address: trueinstance-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
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 {@Overrideprotected 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/tree/master/micorservice-provider-user
Spring Cloud【Finchley】-03将微服务注册到Eureka Server上 + 为Eureka Server添加用户认证相关推荐
- 【译文】用Spring Cloud和Docker搭建微服务平台
by Kenny Bastani Sunday, July 12, 2015 转自:http://www.kennybastani.com/2015/07/spring-cloud-docker-mi ...
- 用Spring Cloud和Docker搭建微服务平台
This blog series will introduce you to some of the foundational concepts of building a microservice- ...
- spring cloud+.net core搭建微服务架构:Api授权认证(六)
前言 这篇文章拖太久了,因为最近实在太忙了,加上这篇文章也非常长,所以花了不少时间,给大家说句抱歉.好,进入正题.目前的项目基本都是前后端分离了,前端分Web,Ios,Android...,后端也基本 ...
- spring cloud+dotnet core搭建微服务架构:配置中心续(五)
前言 上一章最后讲了,更新配置以后需要重启客户端才能生效,这在实际的场景中是不可取的.由于目前Steeltoe配置的重载只能由客户端发起,没有实现处理程序侦听服务器更改事件,所以还没办法实现彻底实现这 ...
- spring cloud+dotnet core搭建微服务架构:配置中心(四)
前言 我们项目中有很多需要配置的地方,最常见的就是各种服务URL地址,这些地址针对不同的运行环境还不一样,不管和打包还是部署都麻烦,需要非常的小心.一般配置都是存储到配置文件里面,不管多小的配置变动, ...
- spring cloud+dotnet core搭建微服务架构:Api网关(三)
前言 国庆假期,一直没有时间更新. 根据群里面的同学的提问,强烈推荐大家先熟悉下spring cloud.文章下面有纯洁大神的spring cloud系列. 上一章最后说了,因为服务是不对外暴露的,所 ...
- spring cloud+dotnet core搭建微服务架构:服务发现(二)
前言 上篇文章<手把手教你使用spring cloud+dotnet core搭建微服务架构:服务治理(-)>实际上只讲了服务治理中的服务注册,服务与服务之间如何调用呢?传统的方式,服务A ...
- 阿里巴巴开源 Spring Cloud Alibaba,加码微服务生态建设
转载自 阿里巴巴开源 Spring Cloud Alibaba,加码微服务生态建设 本周,Spring Cloud联合创始人Spencer Gibb在Spring官网的博客页面宣布:阿里巴巴开源 S ...
- 基于Spring Cloud及K8S构建微服务应用
摘要 广发证券蔡波斯先生通过三个大方向来为我们分享基于Spring Cloud及K8S构建微服务应用. 内容来源:2017年6月10日,广发证券蔡波斯在"Spring Cloud中国社区技术 ...
- Spring Cloud Sleuth + Zipkin 监控微服务(一)
在之前搭建的服务注册中心的基础上,结合Spring Cloud Sleuth + Zipkin 监控微服务. 1.Zipkin server 从https://dl.bintray.com/openz ...
最新文章
- 一个类可以实现多个接口但是只能实现一个类
- 看了这篇 LeetCode 的刷题心得,再也不用抄别人代码了
- 七丶青龙nvjdc部署教程+短信验证登录对接傻妞
- python开发桌面软件-python适合windows的桌面应用程序开发吗?
- SprinBoot 集成 Flowable/Activiti工作流引擎
- linux日志绕接,Logback配置
- 虚拟机:请问我刚刚回收的对象是干垃圾还是湿垃圾?|文末送书
- php软件开发--redis操作
- Micropython教程之TPYBoard DIY金属探测仪实例演示(萝卜学科编程教育)
- 嵌入式工具——smartmontools
- 如何解读vmlinux.lds.S文件
- Windows10新版本设置卓越性能
- POJ3207,2-sat问题
- YGG 与 StemsDAO 达成合作,为全球音乐创作者提供支持
- git基于某个分支新建分支
- 关于打开ARX项目的一些错误
- 如何利用任意波形发生器创建你想要的波形并输出
- 第三方支付(微信支付)支付流程分析
- 全球人造丝卫生棉条行业调研及趋势分析报告
- 西门子博途1500双驱同步,伺服同步运行程序,3轴码垛博途程序,scl项目,同步轴走PN工艺对象
热门文章
- java 闹钟代码_JAVA可视化闹钟源码
- “AttributeError: ‘str‘ object has no attribute ‘decode‘ “
- TensorFlow 最小二乘法拟合
- python定义一个类和子类_Python定义类、定义子类以及super()函数的使用
- 重温强化学习之马尔可夫决策过程(MDPs)
- 论文笔记:Missing Value Imputation for Multi-view UrbanStatistical Data via Spatial Correlation Learning
- linux实战应用案例: 如何在 Linux 安装 MySQL 8 数据库?(图文详细教程)
- MATLAB实战系列(十二)-如何用人工鱼群算法解决带时间窗车辆路径(CVRP)问题(附MATLAB代码)
- MATLAB从入门到精通系列之如何实现KNN算法
- 强化学习(十二) Dueling DQN