SpringCloud之Eureka
【前面的话】SpringCloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它配置简单,上手快,而且生态成熟,便于应用。但是它对SpringBoot有很强的依赖,需要有一定基础,但是SpringBoot俩小时就可以入门。另外对于“微服务架构” 不了解的话,可以通过搜索引擎搜索“微服务架构”了解下。另外这是SpringCloud的版本为Greenwich.SR2,JDK版本为1.8,SpringBoot的版本为2.1.7.RELEASE。
壹、新建父工程
- 新建一个Maven父工程lovincloud,便于版本管理,然后删除src文件夹
- 添加pom依赖和SpringCloud和SpringBoot的版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.eelve.lovincloud</groupId>
<artifactId>lovincloud</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>lovincloud</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
贰、添加一个注册中心
在这里,我们需要用的的组件上Spring Cloud Netflix的Eureka ,eureka是一个服务注册和发现模块。
- 新建一个子工程lovin-eureka-server作为服务的注册中心
<parent>
<artifactId>lovincloud</artifactId>
<groupId>com.eelve.lovincloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lovin-eureka-server</artifactId>
<packaging>jar</packaging>
<name>eurekaserver</name>
<version>0.0.1</version>
<description>eureka服务端</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 然后在启动类上添加@EnableEurekaServer注解:
package com.eelve.lovin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @ClassName LovinEurekaServerApplication
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/15 16:20
* @Version 1.0
**/
@EnableEurekaServer
@SpringBootApplication
public class LovinEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(LovinEurekaServerApplication.class,args);
}
}
- eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml:
spring:
application:
naem: lovineurkaserver # 服务模块名称
server:
port: 8881 # 设置的eureka端口号
eureka:
instance:
hostname: localhost # 设置eureka的主机地址
client:
registerWithEureka: false #表示是否将自己注册到Eureka Server,默认为true。由于当前应用就是Eureka Server,故而设置为false
fetchRegistry: false #表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设置为false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #Eureka server地址,查询服务和注册服务都需要依赖这个地址,多个地址可用逗号(英文的)分割
叁、添加一个服务消费端
- 新建一个子工程lovin-eureka-server作为服务的注册中心
<parent>
<artifactId>lovincloud</artifactId>
<groupId>com.eelve.lovincloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lovin-eureka-client</artifactId>
<packaging>jar</packaging>
<name>eurekaclient</name>
<version>0.0.1</version>
<description>eureka的一个消费端</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
- 然后在启动类上添加@EnableEurekaClient注解:
package com.eelve.lovin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @ClassName LovinEurekaClientApplication
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/15 16:37
* @Version 1.0
**/
@SpringBootApplication
@EnableEurekaClient
public class LovinEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(LovinEurekaClientApplication.class,args);
}
}
- 然后我们需要连接到服务端,具体配置如下
server:
port: 8801 # 服务端口号
spring:
application:
name: lovineurkaclient # 服务名称
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8881/eureka/ # 注册到的eureka服务地址
- 新建一个Controller写一个测试接口
package com.eelve.lovin.controller;
import com.eelve.lovin.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName HelloController
* @Description TDO应用默认访问接口
* @Author zhao.zhilue
* @Date 2019/8/15 16:45
* @Version 1.0
**/
@RestController
public class HelloController {
@Autowired
ServerConfig serverConfig;
@RequestMapping("hello")
public String hello(){
return serverConfig.getUrl()+"###"+ HelloController.class.getName();
}
}
肆、分别启动注册中心的服务端和客户端
访问localhost:8881查看结果
到这里我们可以已经看到已经成功将客户端注册到服务端了,然后我们访问测试接口
可以看到已经访问成功,至此Eureka的搭建已经完成。
伍、加入安全配置
在互联网中我们一般都会考虑安全性,尤其是管理服务的注册中心,所以我们可以用spring-boot-starter-security来做安全限制
- 给lovin-eureka-server添加spring-boot-starter-security的pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- 修改配置文件
spring:
application:
naem: lovineurkaserver # 服务模块名称
security:
basic:
enabled: true
user:
name: lovin
password: ${REGISTRY_SERVER_PASSWORD:lovin}
server:
port: 8881 # 设置的eureka端口号
eureka:
instance:
hostname: localhost # 设置eureka的主机地址
metadata-map:
user.name: ${security.user.name}
user.password: ${security.user.password}
client:
registerWithEureka: false #表示是否将自己注册到Eureka Server,默认为true。由于当前应用就是Eureka Server,故而设置为false
fetchRegistry: false #表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设置为false
serviceUrl:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/ #Eureka server地址,查询服务和注册服务都需要依赖这个地址,多个地址可用逗号(英文的)分割
- 添加security配置
package com.eelve.lovin.config;
import org.springframework.context.annotation.Configuration;
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;
/**
* @ClassName SecurityConfig
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/16 14:13
* @Version 1.0
**/
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests()
.antMatchers("/css/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.httpBasic();
super.configure(http);
}
}
- 给lovin-eureka-client添加spring-boot-starter-security的pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
- 修改yaml配置文件
server:
port: 8801 # 服务端口号
spring:
application:
name: lovineurkaclient # 服务名称
security:
basic:
enabled: true
user:
name: lovin
password: ${REGISTRY_SERVER_PASSWORD:lovin}
eureka:
client:
serviceUrl:
defaultZone: http://lovin:lovin@localhost:8881/eureka/ # 注册到的eureka服务地址
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
user.name: lovin
user.password: lovin
- 添加security配置
package com.eelve.lovin.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
* @ClassName SecurityConfig
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/16 14:13
* @Version 1.0
**/
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
- 另外为了测试多客服端注册,我们可以修改再给客户端新建一个配置文件,然后开启IDEA的多节点运行,如下图所示勾选Allow parallel run
- 然后为了区分是哪个节点的请求我们可以添加获取端口
package com.eelve.lovin.config;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @ClassName ServerConfig
* @Description TDO
* @Author zhao.zhilue
* @Date 2019/8/18 12:03
* @Version 1.0
**/
@Component
public class ServerConfig implements ApplicationListener<WebServerInitializedEvent> {
private int serverPort;
public String getUrl() {
InetAddress address = null;
try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return "http://"+address.getHostAddress() +":"+this.serverPort;
}
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
this.serverPort = event.getWebServer().getPort();
}
}
- 然后我们一次重启服务端和两个客户端,这个时候我们访问http://localhost:8881/
可以看到,这里已经让我们输入用户名和密码了,说明spring-boot-starter-security已经配置成功,这时我们输入配置的用户名:lovin和密码:lovin
这里我们可以看到已经成功了,那么到这里Eureka的配置已经全部成功了。
SpringCloud之Eureka的更多相关文章
- SpringCloud中eureka配置心跳和剔除下线的服务的时间
在默认的springCloud中eureka注册中心在服务下线时表现的非常不灵敏,用惯了dubbo的zk注册中心表示很不习惯,eureka设计的本意是在服务不会频繁上下线和网络稳定的内网,这种设计在生 ...
- springcloud(二) eureka的使用
上一节讲到order微服务是通过rest调用user微服务的地址.但是,user微服务的地址是写死的, 如果user微服务集群的话,那么order微服务该如何调用呢?这个时候注册中心该上场了 演示eu ...
- spring-cloud配置eureka客户端
spring-cloud配置eureka客户端 eureka用来发现其他程序 需要提前配置eureka服务端,具体看 https://www.cnblogs.com/ye-hcj/p/10292944 ...
- 浅谈SpringCloud (二) Eureka服务发现组件
上面学习到了如何由一个程序访问另一个程序,那么如果使用SpringCloud来进行访问,该如何访问呐? 可以借助Eureka服务发现组件进行访问. 可以借助官方文档:https://spring.io ...
- SpringCloud之Eureka:集群搭建
上篇文章<SpringCloud之Eureka:服务发布与调用例子>实现了一个简单例子,这次对其进行改造,运行两个服务器实例.两个服务提供者实例,服务调用者请求服务,使其可以进行集群部署. ...
- SpringCloud学习笔记(三、SpringCloud Netflix Eureka)
目录: 服务发现简介 SpringCloud Netflix Eureka应用 Eureka高可用 Eureka源码分析 >>> Eureka Client初始化(客户端定时获取服务 ...
- springCloud 之 Eureka注册中心高可用配置
springCloud的eureka高可用配置方案思路是:几个服务中心之间相互注册,比如两个注册中心,A注册到B上,B注册到A上,如果是三个注册中心则是:A注册到BC上,B注册到AC上,C注册到AB上 ...
- SpringCloud创建Eureka模块集群
1.说明 本文详细介绍Spring Cloud创建Eureka模块集群的方法, 基于已经创建好的Spring Cloud Eureka Server模块, 请参考SpringCloud创建Eureka ...
- SpringCloud(3)---Eureka服务注册与发现
Eureka服务注册与发现 一.Eureka概述 1.Eureka特点 (1) Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移. (2) Eureka 主管服务 ...
- SpringCloud系列——Eureka 服务注册与发现
前言 Eureka是一种基于REST(具像状态传输)的服务,主要用于AWS云中定位服务,以实现中间层服务器的负载平衡和故障转移.本文记录一个简单的服务注册与发现实例. GitHub地址:https:/ ...
随机推荐
- 012 Android 动画效果(补间动画) +去掉App默认自带的标题+更改应用的图标
1.介绍 补间动画开发者只需指定动画开始,以及动画结束"关键帧", 而动画变化的"中间帧"则由系统计算并补齐! 2.去掉App的标题 (1)将AndroidMa ...
- 基于DNN的推荐算法总结
1.早期的算法 深度学习在CTR预估应用的常见算法有Wide&Deep,DeepFM等. 这些方法一般的思路是:通过Embedding层,将高维离散特征转换为固定长度的连续特征,然后通过多个全 ...
- MongoDB的Shell操作
前言 本文从介绍了MongoShell 的配置.脚本.数据类型和其他指令. MongoShell - 简介 MongoShell是一个互动的JavaScript接口的MongoDB,可以使用Mongo ...
- 简单实现SpringBoot启动
一.准备: IDEA 使用简单手写导包实现spring boot,未使用idea自带的spring创建方法 可以更加简单理解springboot启动过程 二.开始 1.打开idea创建project ...
- 自定义注解扩展springMvc的validation注解
文章目录 前言 自定义校验注解 使用 后记 前言 我们都知道 springMvc 的检验框架使用的是 hibernate 的 validator ,检验数据,是有那么一点小爽快: 但是,validat ...
- sendmail邮箱部署设置
前言:在使用一些shell脚本进行监控时需要通过发送报警邮件来提醒,下面通过部署简单的sendmail来实现简单的邮件发送. 1.安装 mailx 和 sendmail: yum install ma ...
- PAT(B) 1068 万绿丛中一点红(C)
题目链接:1068 万绿丛中一点红 (20 point(s)) 参考博客:1068. 万绿丛中一点红(20) i逆天耗子丶 题目描述 对于计算机而言,颜色不过是像素点对应的一个 24 位的数值.现给定 ...
- Ubuntu中更改默认的root用户密码,以及怎样修改用户密码
新安装的Ubuntu系统中默认的root用户密码是多少?该怎么修改? 如题,相信许多刚接触Ubuntu系统的新手大多会遇到这个问题,那么我们该如何解决这个问题呢?Ubuntu在安装过程中并没有让我们设 ...
- vue cli3 项目优化
vue-cli3 Prefetch (官网内容) <link rel="prefetch"> 是一种 resource hint,用来告诉浏览器在页面加载完成后,利用空 ...
- 括号匹配问题 —— Deque双端队列解法
题目: 给定一个只包括 '(',')','{','}','[',']'?的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空字符串可 ...