01.SpringCloud中Eureka Server及其Eu
2019-12-25 本文已影响0人
LewisZhu
一.创建并且配置Eureka Server
01.可以在创建spring initialize的勾上Eureka Server
02.配置pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
03.配置启动类为Eureka server
@SpringBootApplication
//创建服务注册中心
@EnableEurekaServer
public class StartMain {
...
}
04.application.yml配置
server:
port:7777 //端口号
eureka:
client:
service-url:
defalutZone:http://localhost:7777/eureka/ //若果有多个eureka server就用逗号隔开
fetch-registry: false //禁止两个eureka server互相抓取内容,因为这是一个单点的EurekaServer,不需要同步其它EurekaServer节点的数据,故设为false
register-with-eureka:false //禁止注册eureka server,因为它已经是了 自己注册自己 没有意义,其他的客户端项目可以注册这个eureka server。
二.创建并且配置Eureka Discovery
01.可以在创建spring initialize的勾上Eureka Discovery
02.pom文件的配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
03.配置client启动类
@SpringBootApplication
@EnableEurekaClient
public class StartMain {
...
}
04.application.yml配置
server:
port:8080 //作为一个web项目,端口必须的配置
spring:
application:
name:user-provider //eureka client的名字
eureka:
client:
service-url:
defaultZone:http://localhost:7777/eureka/ //配置注册到eureka server的地址
三.给Eureka server添加用户名密码
01.在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>
02.在yml文件中配置用户名可密码
security:
basic:
enabled: true
user:
name: root
password: 123456
03.给client配置security中的账号和密码 不然client就无法访问server
在client中的yml文件中配置
defaultZone: http://root:123456@localhost:7777/eureka/