版本环境:
JDK:1.8
maven:3.5.2
springboot:2.4.4
springcloud:2020.0.2
创建一个空的maven项目并配置pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dongyue</groupId>
<artifactId>springcloud-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>springcloud-demo</name>
<description>springcloud-demo</description>
<!--设置版本号-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-boot.version>2.4.4</spring-boot.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
</properties>
<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>
</project>
Euraka是Spring Cloud集合中一个组件,它是对Euraka的集成,用于服务注册和发现。Eureka是Netflix中的一个开源框架。它和 zookeeper、Consul一样,都是用于服务注册管理的。
Eureka基于REST的服务,主要用于AWS云中的定位服务,以实现中间层服务器的负载平衡和故障转移
在 Spring Cloud 微服务架构中通常用作注册中心
我们称这个服务为 Eureka Server,还有一个与之交互的客户端称之为 Eureka Client
建立Eureka-server模块
添加pom:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
配置properties
application.properties
server.port=7070
spring.profiles.active=dev
spring.application.name=@pom.artifactId@
#自我保护机制
eureka.server.enable-self-preservation=false
eureka.instance.hostname=localhost
#表示是否将自己注册到Eureka Server
eureka.client.registerWithEureka=false
#表示是否从Eureka Server获取注册信息
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
启动中加入@EnableEurekaServer
至此euraka的服务已搭建完毕,接下来搭建客户端client.
客户端client 提供真正服务的角色的配置, 它提供服务 在 服务注册方server (注册中心)进行注册
创建服务模块
添加pom:
<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>
配置properties
application.properties
server.port=7071
spring.profiles.active=dev
spring.application.name=@pom.artifactId@
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:7070/eureka/
启动添加@EnableEurekaClient
访问配置的eureka路径出现配置的客户端,至此客户端添加完成
设置密码需要用到spring-boot-starter-security,服务模块pom中添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
properties中加入配置
#是否开启密码服务
spring.security.basic.enabled=true
#用户
spring.security.user.name=user
#密码
spring.security.user.password=springclouddemo
修改properties中eureka.client.serviceUrl.defaultZone地址
eureka.client.serviceUrl.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka
修改properties
#用户
spring.security.user.name=user
#密码
spring.security.user.password=springclouddemo
eureka.client.serviceUrl.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka
访问路径
至此密码创建完成
Spring Cloud 2.0 以上的security默认启用了csrf检验,要在eureka server端配置security的csrf检验为false。
在服务端添加代码
@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.csrf().disable();
}
}
如图
虽然大部分使用Zabbix的用户是放在内网,但是该掌握的还是需要的,万一要用到了...
[Thistopicispre-releasedocumentationandissubjecttochangeinfuturereleases.Bl...
代码实现方式: 复制代码 代码如下: !DOCTYPE html head title New Document /ti...
昨天在网上看到一个防采集软件,说采集只访问当前网页,不会访问网页的图片、JS...
依赖注入简单介绍: 依赖注入(Dependency injection , DI)是一种实现对象及其...
首先将以下代码加入网页中: spanid="Text"/span functionCurrentPosition(){ if...
本文实例讲述了tp5.1 框架查询表达式用法。分享给大家供大家参考,具体如下: 查...
前言 在2.1之后,有不少新东西,其中HttpClientFactory算是一个。HttpClientFact...
第1部分 硬件介绍 1.1 硬件清单 序号 品名 数量 1 SimpleFOCShield V2.0.2 主板 ...
mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该...