好久没写博客了,哈哈。最近在想搞搞集群。搞了三台服务器慢慢玩。
言归正传,首先我们先准备两个服务,一个 eureka 服务端、一个eureka 客户端 方便验证
1.服务端配置
pom.xml 引用
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-security</artifactId>
-
</dependency>
yml 配置文件
-
server:
-
port: 9001
-
eureka:
-
server:
-
eviction-interval-timer-in-ms: 10000 #设置清理的间隔时间,而后这个时间使用的是毫秒单位(默认是60秒)
-
enable-self-preservation: false #设置为false表示关闭保护模式
-
client:
-
fetch-registry: false
-
register-with-eureka: false
-
service-url:
-
defaultZone: http://admin:123456@127.0.0.1:9001/eureka
-
logging:
-
level:
-
com.xiongyc.eureka: DEBUG #INFO
-
spring:
-
security:
-
user:
-
name: admin
-
password: 123456
2.客户端配置
pom.xml 引用
-
<!-- eureka 客户端 -->
-
<dependency>
-
<groupId>org.springframework.cloud</groupId>
-
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
-
</dependency>
yml 配置文件
-
server:
-
port: 8080
-
servlet:
-
context-path: /product
-
eureka:
-
client:
-
service-url:
-
defaultZone: http://admin:123456@127.0.0.1:9001/eureka
-
instance:
-
instance-id: product-service
-
prefer-ip-address: true
-
mybatis:
-
type-aliases-package: com.xiongyc.product.bean
-
mapper-locations: # 所有的mapper映射文件
-
- classpath:mapper/*.xml
-
api:
-
swagger:
-
basePackage: com.xiongyc.product
-
title: 产品服务
-
description: 这是一个声明
-
contact: YouCai.Xiong,www.baidu.com,764340703@qq.com
-
version: 1.0
-
spring:
-
application:
-
name: product-service
-
datasource:
-
type: com.alibaba.druid.pool.DruidDataSource # 配置当前要使用的数据源的操作类型
-
driver-class-name: com.mysql.cj.jdbc.Driver # 配置MySQL的驱动程序类\
-
url: jdbc:mysql://172.1.10.24:3306/afterSale?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&allowMultiQueries=true
-
username: root # 数据库用户名
-
password: 123456 # 数据库连接密码
-
redis:
-
port: 6379
-
host: 47.107.158.20
-
password: edo9567
-
logging:
-
level:
-
com.xiongyc.product: DEBUG #INFO
2.在服务端增加配置类(spring boot 2.X 版本必加,不然会客户端注册到eureka的时候会报错 Cannot execute request on any known server )
-
package com.xiongyc.eureka.config;
-
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;
-
/**
-
*
-
* @author YouCai.Xiong
-
* @Date 2020年4月10日 - 下午2:50:01
-
* @Info 初始版本 eureka 安全验证放行
-
* @Version 1.0
-
*/
-
@EnableWebSecurity
-
public class EurekaSecurityConfig extends WebSecurityConfigurerAdapter {
-
@Override
-
protected void configure(HttpSecurity http) throws Exception {
-
http.csrf().disable();
-
// http.csrf().ignoringAntMatchers("/eureka/**");
-
super.configure(http);
-
}
-
}
然后我们打开eureka 的效果如下
可以看到已经成功注册上去了。