1.创建springcloud-eureka maven项目

pom.xml

<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>
<!-- 父项目yilai -->
<parent>
<groupId>com.pupeiyuan.springcloud</groupId>
<artifactId>spring-Cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springcloud-eureka</artifactId>
<packaging>jar</packaging>
<dependencies>
<!-- eureka server 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
</project>

2.启动类EurekaServerApplication.java

package com.spring.pupeiyuan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}

3.创建application-peer1.properties向peer2注册中心注册,把peer1注册中心当做一个client注册上去

application-peer1.properties

spring.application.name=spring-cloud-eureka
server.port=8000
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@peer2:8001/eureka/
eureka.environment=eureka-test1
eureka.datacenter=eureka-data1
eureka.server.enable-self-preservation=false
eureka.server.eviction-interval-timer-in-ms=10000 security.basic.enabled=true
security.user.name=root
security.user.password=123456

创建application-peer2.properties向peer1注册中心注册,把peer2注册中心当做一个client注册上去

application-peer2.properties

spring.application.name=spring-cloud-eureka
server.port=8001
eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://${security.user.name}:${security.user.password}@peer1:8000/eureka/
eureka.environment=eureka-test2
eureka.datacenter=eureka-data2
eureka.server.enable-self-preservation=false
eureka.server.eviction-interval-timer-in-ms=4000 security.basic.enabled=true
security.user.name=root
security.user.password=123456

创建application.properties指定启动时应用的配置文件

application.properties

spring.profiles.active=peer2

4.目前版本加入安全验证spring-boot-starter-security时,会发生注册中心无法注册的问题,解决如下

添加WebSecurityConfig.java配置文件到项目中,关闭csrf

WebSecurityConfig.java

package com.spring.pupeiyuan.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;
import org.springframework.security.config.http.SessionCreationPolicy; @EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
http.csrf().disable(); // 关闭csrf
http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); // 开启认证
}
}

结果演示

spring cloud Eureka注册中心集群搭建的更多相关文章

  1. Spring Cloud Eureka 注册中心集群搭建,Greenwich 最新版!

    Spring Cloud 的注册中心可以由 Eureka.Consul.Zookeeper.ETCD 等来实现,这里推荐使用 Spring Cloud Eureka 来实现注册中心,它基于 Netfl ...

  2. springCloud系列教程01:Eureka 注册中心集群搭建

    springCloud系列教程包含如下内容: springCloud系列教程01:Eureka 注册中心集群搭建 springCloud系列教程02:ConfigServer 配置中心server搭建 ...

  3. IDEA 创建Spring cloud Eureka 注册中心

    IDEA 创建Spring cloud Eureka 注册中心 一. 首先创建一个maven project Next之后填好groupId与artifactId,Next之后填好项目名与路径,点击F ...

  4. 一个线上问题的思考:Eureka注册中心集群如何实现客户端请求负载及故障转移?

    前言 先抛一个问题给我聪明的读者,如果你们使用微服务SpringCloud-Netflix进行业务开发,那么线上注册中心肯定也是用了集群部署,问题来了: 你了解Eureka注册中心集群如何实现客户端请 ...

  5. SpringCloud Eureka(注册中心集群)

    多个注册中心,其实用不同的配置对应 不同的端口号注册就行了. 注册中心自己也是个服务,看看之前的单个注册中心是怎么样的呢? server: port: 8888 # 服务端口eureka: insta ...

  6. Spring Cloud Eureka 注册中心 服务消费者 服务提供者之间的关系以及高可用之间的联系

    注册中心:提供服务的注册与查询(发现) 服务提供者:服务的提供方,提供服务的一方. 服务消费者:服务的消费方,使用服务的一方. 我们没有注册中心,服务提供者与服务消费者同样可以调用,通过spring中 ...

  7. eureka注册中心集群

    注册中心集群: 思想:让一个eureka注册中心 注册到 另一个eureka注册中心上去即A注册到B,B注册到A 注册中心配置: 1:端口号不同2:应用名称相同3:相互注册

  8. Spring Cloud Eureka注册中心(快速搭建)

    Spring Cloud 详解Eureka注册中心@(微服务)[java|spring-cloud|eureka] Eureka 注册中心是入门Spring Cloud微服务架构的必学组件,是学习所有 ...

  9. 2、eureka注册中心集群

    1. Eureka作为spring cloud的服务发现与注册中心,在整个的微服务体系中,处于核心位置.单机模式下的eureka服务,显然不能满足高可用的实际生产环境,这就要求配置一个能够应对各种突发 ...

随机推荐

  1. 20165325 2017-2018-2 《Java程序设计》第七周学习总结

    一.教材学习笔记 ch11 1.连接数据库 2.条件与排序查询: where子语句 一般格式:select 字段 from 表名 where 条件 排序:用order by子语句对记录排序 3.更新. ...

  2. mysql进制之间的转换

    1.十进制转换成二进制 select bin(5); 2.十进制转换成八进制 select oct(5); 3.十进制转换成十六进制 select hex(5); 4.二进制转换成十进制 select ...

  3. cocos开发学习记录

    场景的创建和切换 https://blog.csdn.net/lin453701006/article/details/56334578

  4. AIX系统下sed的用法与实例——查询/打印/替换字符串并生成文件/删除

    sed是AIX中非常重要的文本流编辑器,它对输入的文本进行查询/打印/替换/删除等操作,并将结果写到标准输出.sed 命令包含很多功能,用于选择要修改的行,并只对选择的行作更改. 首先,使用sed命令 ...

  5. 题解-hdu2866 Special Prime

    Problem hdu-2866 题意:求区间\([2,L]\)有多少素数\(p\)满足\(n^3+pn^2=m^3\),其中\(n,m\)属于任意整数 Solution 原式等价于\(n^2(p+n ...

  6. mingw zlib-1.2.11 win32-static.patch

    diff --git a/Makefile.in b/Makefile.in index 1852192..c88f5b5 100644 --- a/Makefile.in +++ b/Makefil ...

  7. useful tips for python

    import module; help(module.function) import module; help(module.class)

  8. Mvc 批量图片上传

    首先导入文件(官网上下载 kindeditor ): <link href="~/kindeditor-4.1.11-zh-CN/kindeditor/themes/default/d ...

  9. Android之androidmainfest.xml配置文件详解

    写配置文件的时候我们会不知道把那些配置项放在哪里,请看: 配置文件样例: <?xml version="1.0" encoding="utf-8"?> ...

  10. Codeforces 1045G AI robots [CDQ分治]

    洛谷 Codeforces 简单的CDQ分治题. 由于对话要求互相看见,无法简单地用树套树切掉,考虑CDQ分治. 按视野从大到小排序,这样只要右边能看见左边就可以保证互相看见. 发现\(K\)固定,那 ...