eureka服务端配置 请点击 Eureka +security 服务端 配置、调用
Eureka 服务器集群
修改服务域名
在搭建前需要修改 hosts文件,文件地址如下
hosts位置.jpg
编辑改文件 添加 127.0.0.1 peer1,peer2 如下
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 peer1 peer2
注意: #符号 后面的都是被注解掉的代码
创建eureka 集群服务器
第一步 创建spring boot 项目,项目起名为 microservice-discovery-eureka ,修改 pom文件 添加 如下引入
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
......
其他引入包
......
<!-- eureka server 支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- spring security 支持包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<!-- Spring cloud 管理引入 -->
<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>
第二步 修改application.yml配置文件 ->原文件 后缀为 properties 请手动修改为 yml 因本例使用的是 yml
# 配置服务名称(因为这里作为服务中心集群,所以将自己作为服务向其它服务中心注册自己,形成互相注册的服务注册中心)
spring:
application:
name: microservice-discovery-eureka
security:#spring security 的登录名和密码设置
user:
name: user
password: 123456
# 配置端口
server:
port: 8761
eureka:
# 配置本注册中心的hostname
instance:
hostname: peer1
# 本Eureka服务端将自己作为服务将要注册的注册中心的地址
client:
#表示是否将自己注册到Eureka Server,默认为true。由于当前应用就是Eureka server,故而设置为false。
register-with-eureka: true
#表示是否从Eureka server 获取注册信息,默认为true。
#因为这是一个单节点的Eureka Server ,不需要同步其他的 Eureka Server 节点的数据,故而设置为false
fetch-registry: true
serviceUrl:
#将域名注册至 集群上 三个服务或更多服务集群的时候 地址为
#http://user:123456@peer2:8762/eureka/,http://user:123456@peer3:8763/eureka/, ...
# user:123456 对应的是 peer2:8762 配置的 spring security 的 访问时的用户名和 密码 使用@连接url
defaultZone: http://user:123456@peer2:8762/eureka/
第三步 创建 SecurityConfig 文件 代码如下
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;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//spring security csrf (跨域保护)关闭
http.csrf().disable();
super.configure(http);
}
}
第四步 修改 Application 文件 启用 eureka server
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //启用 eureka server
public class MicroserviceDiscoveryEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args);
}
}
第五步 启动服务器
启动后会看到如下错误
控制台错误.png
不要担心 服务现在是好使的 直接访问 ** http://peer1:8761 **可以看到 如下页面 输入配置文件中 的用户名和密码进入
spring security登录页面.jpg
进入后页面如下
eureka主页(第二服务器启动前).jpg
以上单个eureka 服务就算完成了 现在搭建第二个 eureka服务器实现 服务器集群 不要停止服务执行以下操作
重新创建一个项目起名 为 microservice-discovery-eureka-ha ,
重复上述 第一步操作
第二步操作pom文件修改为:
# 配置服务名称(因为这里作为服务中心集群,所以将自己作为服务向其它服务中心注册自己,形成互相注册的服务注册中心)
spring:
application:
name: microservice-discovery-eureka-ha
security:
user:
name: user
password: 123456
# 配置端口
server:
port: 8762
eureka:
# 配置本注册中心的hostname
instance:
hostname: peer2
# 本Eureka服务端将自己作为服务将要注册的注册中心的地址
client:
register-with-eureka: true
#表示是否从Eureka server 获取注册信息,默认为true。因为这是一个单节点的Eureka Server ,不需要同步其他的 Eureka Server 节点的数据,故而设置为false
fetch-registry: true
serviceUrl:
defaultZone: http://user:123456@peer1:8761/eureka/
重复上述第三步与第四步操作
然后启动 服务器 启动后可以看到控制台打印如下
集群后控制台.jpg
切换至 eureka 项目控制台可以看到类似上方图片的信息 只是地址不同
刷新页面可以看到 多出了两条数据 如下图
第二服务器启动后.jpg