百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

SpringCloud之Eureka

bigegpt 2024-08-07 17:38 8 浏览

前面的话】SpringCloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它配置简单,上手快,而且生态成熟,便于应用。但是它对SpringBoot有很强的依赖,需要有一定基础,但是SpringBoot俩小时就可以入门。另外对于“微服务架构” 不了解的话,可以通过搜索引擎搜索“微服务架构”了解下。另外这是SpringCloud的版本为Greenwich.SR2,JDK版本为1.8,SpringBoot的版本为2.1.7.RELEASE

壹、新建父工程

  • 新建一个Maven父工程lovincloud,便于版本管理,然后删除src文件夹
  • 添加pom依赖和SpringCloud和SpringBoot的版本
  • ~~~pom
  • org.springframework.boot
  • spring-boot-starter-parent
  • 2.1.7.RELEASE
 <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**作为服务的注册中心
~~~pom
<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注解:
~~~java
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:
  • ~~~yaml
  • 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作为服务的注册中心
  • ~~~pom
  • lovincloud
  • com.eelve.lovincloud
  • 1.0-SNAPSHOT
  • 4.0.0
 <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注解:
~~~java
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);
  • }
  • }
  • ~~~
  • 然后我们需要连接到服务端,具体配置如下
  • ~~~yaml
  • server:
  • port: 8801 # 服务端口号
  • spring:
  • application:
  • name: lovineurkaclient # 服务名称
  • eureka:
  • client:
  • serviceUrl:
  • defaultZone: http://localhost:8881/eureka/ # 注册到的eureka服务地址
  • ~~~
  • 新建一个Controller写一个测试接口
  • ~~~java
  • 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查看结果
![注册中心](https://eelve.com/upload/2019/8/192201-a1bae0c08fdc4e848f41093d643ff0cb.png)
到这里我们可以已经看到已经成功将客户端注册到服务端了,然后我们访问测试接口
![192202](https://eelve.com/upload/2019/8/192202-b3bb342983db481f9ad86239e8859e41.png)
可以看到已经访问成功,至此Eureka的搭建已经完成。
# 伍、加入安全配置
在互联网中我们一般都会考虑安全性,尤其是管理服务的注册中心,所以我们可以用**spring-boot-starter-security**来做安全限制
- 给**lovin-eureka-server**添加**spring-boot-starter-security**的pom依赖
~~~pom
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-security</artifactId>
</dependency>
~~~
- 修改配置文件
~~~yaml
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配置
~~~java
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依赖
~~~pom
<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-security</artifactId>
</dependency>
~~~
- 修改yaml配置文件
~~~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配置
~~~java
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**
![192203](https://eelve.com/upload/2019/8/192203-4b74b03a3b3647f5a2c607c61e3a089e.png)
- 然后为了区分是哪个节点的请求我们可以添加获取端口
~~~java
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 {
  • 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的配置已经全部成功了。
  • https://github.com/lovinstudio/lovincloud

相关推荐

方差分析简介(方差分析通俗理解)

介绍方差分析(ANOVA,AnalysisofVariance)是一种广泛使用的统计方法,用于比较两个或多个组之间的均值。单因素方差分析是方差分析的一种变体,旨在检测三个或更多分类组的均值是否存在...

正如404页面所预示,猴子正成为断网元凶--吧嗒吧嗒真好吃

吧嗒吧嗒,绘图:MakiNaro你可以通过加热、冰冻、水淹、模塑、甚至压溃压力来使网络光缆硬化。但用猴子显然是不行的。光缆那新挤压成型的塑料外皮太尼玛诱人了,无法阻挡一场试吃盛宴的举行。印度政府正...

Python数据可视化:箱线图多种库画法

概念箱线图通过数据的四分位数来展示数据的分布情况。例如:数据的中心位置,数据间的离散程度,是否有异常值等。把数据从小到大进行排列并等分成四份,第一分位数(Q1),第二分位数(Q2)和第三分位数(Q3)...

多组独立(完全随机设计)样本秩和检验的SPSS操作教程及结果解读

作者/风仕在上一期,我们已经讲完了两组独立样本秩和检验的SPSS操作教程及结果解读,这期开始讲多组独立样本秩和检验,我们主要从多组独立样本秩和检验介绍、两组独立样本秩和检验使用条件及案例的SPSS操作...

方差分析 in R语言 and Excel(方差分析r语言例题)

今天来写一篇实际中比较实用的分析方法,方差分析。通过方差分析,我们可以确定组别之间的差异是否超出了由于随机因素引起的差异范围。方差分析分为单因素方差分析和多因素方差分析,这一篇先介绍一下单因素方差分析...

可视化:前端数据可视化插件大盘点 图表/图谱/地图/关系图

前端数据可视化插件大盘点图表/图谱/地图/关系图全有在大数据时代,很多时候我们需要在网页中显示数据统计报表,从而能很直观地了解数据的走向,开发人员很多时候需要使用图表来表现一些数据。随着Web技术的...

matplotlib 必知的 15 个图(matplotlib各种图)

施工专题,我已完成20篇,施工系列几乎覆盖Python完整技术栈,目标只总结实践中最实用的东西,直击问题本质,快速帮助读者们入门和进阶:1我的施工计划2数字专题3字符串专题4列表专题5流程控制专题6编...

R ggplot2常用图表绘制指南(ggplot2绘制折线图)

ggplot2是R语言中强大的数据可视化包,基于“图形语法”(GrammarofGraphics),通过分层方式构建图表。以下是常用图表命令的详细指南,涵盖基本语法、常见图表类型及示例,适合...

Python数据可视化:从Pandas基础到Seaborn高级应用

数据可视化是数据分析中不可或缺的一环,它能帮助我们直观理解数据模式和趋势。本文将全面介绍Python中最常用的三种可视化方法。Pandas内置绘图功能Pandas基于Matplotlib提供了简洁的绘...

Python 数据可视化常用命令备忘录

本文提供了一个全面的Python数据可视化备忘单,适用于探索性数据分析(EDA)。该备忘单涵盖了单变量分析、双变量分析、多变量分析、时间序列分析、文本数据分析、可视化定制以及保存与显示等内容。所...

统计图的种类(统计图的种类及特点图片)

统计图是利用几何图形或具体事物的形象和地图等形式来表现社会经济现象数量特征和数量关系的图形。以下是几种常见的统计图类型及其适用场景:1.条形图(BarChart)条形图是用矩形条的高度或长度来表示...

实测,大模型谁更懂数据可视化?(数据可视化和可视化分析的主要模型)

大家好,我是Ai学习的老章看论文时,经常看到漂亮的图表,很多不知道是用什么工具绘制的,或者很想复刻类似图表。实测,大模型LaTeX公式识别,出乎预料前文,我用Kimi、Qwen-3-235B...

通过AI提示词让Deepseek快速生成各种类型的图表制作

在数据分析和可视化领域,图表是传达信息的重要工具。然而,传统图表制作往往需要专业的软件和一定的技术知识。本文将介绍如何通过AI提示词,利用Deepseek快速生成各种类型的图表,包括柱状图、折线图、饼...

数据可视化:解析箱线图(box plot)

箱线图/盒须图(boxplot)是数据分布的图形表示,由五个摘要组成:最小值、第一四分位数(25th百分位数)、中位数、第三四分位数(75th百分位数)和最大值。箱子代表四分位距(IQR)。IQR是...

[seaborn] seaborn学习笔记1-箱形图Boxplot

1箱形图Boxplot(代码下载)Boxplot可能是最常见的图形类型之一。它能够很好表示数据中的分布规律。箱型图方框的末尾显示了上下四分位数。极线显示最高和最低值,不包括异常值。seaborn中...