今天看啥  ›  专栏  ›  rr完美'诺言

SpringCloud+SpringBoot 项目搭建 (一) Eureka

rr完美'诺言  · CSDN  ·  · 2021-04-10 14:16

基础SpringBoot+Eureka

配置环境

版本环境:
	JDK:1.8
	maven:3.5.2
	springboot:2.4.4
	springcloud:2020.0.2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

创建Maven项目

创建一个空的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>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

Eureka

Euraka是Spring Cloud集合中一个组件,它是对Euraka的集成,用于服务注册和发现。Eureka是Netflix中的一个开源框架。它和 zookeeper、Consul一样,都是用于服务注册管理的。
Eureka基于REST的服务,主要用于AWS云中的定位服务,以实现中间层服务器的负载平衡和故障转移
在 Spring Cloud 微服务架构中通常用作注册中心
我们称这个服务为 Eureka Server,还有一个与之交互的客户端称之为 Eureka Client

搭建eureka的服务

建立Eureka-server模块
在这里插入图片描述

添加pom:

	<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

配置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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

启动中加入@EnableEurekaServer
在这里插入图片描述

至此euraka的服务已搭建完毕,接下来搭建客户端client.

搭建eureka的客户端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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

配置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/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

启动添加@EnableEurekaClient
在这里插入图片描述

访问配置的eureka路径出现配置的客户端,至此客户端添加完成
在这里插入图片描述

设置eureka访问密码

设置密码需要用到spring-boot-starter-security,服务模块pom中添加

	<dependency>
       	<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

properties中加入配置

#是否开启密码服务
spring.security.basic.enabled=true
#用户
spring.security.user.name=user
#密码
spring.security.user.password=springclouddemo
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

修改properties中eureka.client.serviceUrl.defaultZone地址

eureka.client.serviceUrl.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka
  • 1
  • 1

同理client模块

	<dependency>
       	<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

修改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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

访问路径
在这里插入图片描述
至此密码创建完成

填坑

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();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

如图
在这里插入图片描述




原文地址:访问原文地址
快照地址: 访问文章快照