说明:本系列文章主要用于总结相关组件的核心配置文件和问题笔记,详细的配置,参考通用的教程或者SpringCloud及SpringBoot文章。
1.Eureka Server注册中心
1.1单机注册中心
pom依赖中添加:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
配置文件:application.yml
spring:
application:
name: eureka-server#指明springboot项目名称
server:
port: 8761#springboot 项目端口
eureka:
instance:
hosename: localhost#指明主机名
client:
service-url:
defaultZone: http://${eureka.instance.hosename}:${server.port}/eureka/#等价于http://localhost:8761/eureka/ 多个用逗号分隔;用于指定注册中心的API
register-with-eureka: false#默认是true,向注册中心发起注册
fetch-registry: false#默认是true,改成false表示不向使用调用API
server:
enable-self-preservation: false#默认是true false表示注册中心不缓存
切记在SpringBoot启动类上方添加@EnableEurekaServer
标签开启服务
访问 http://localhost:8761/
查看注册中心
1.2 普通Eureka Client
pom依赖中添加:
<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>
</dependency>
配置文件application.yml
spring:
application:
name: eureka-client
server:
port: 8081
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: true
fetch-registry: true
SpringBoot启动类中添加@EnableEurekaClient
。
启动后可以在注册中心界面中查看到本服务
2.分布式Eureka Server注册中心
2.1 开启两个(也可以是多个)注册中心
申明注册中心API分别为http://replica1:8761/eureka/,http://replica2:8762/eureka/
.其中replica1,replica2为域名或者IP或者改hosts文件设置为localhost
配置文件为application-replica1.yml
(pom依赖同上)
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://replica2:8762/eureka/ #这里注意是指向其他的注册中心
register-with-eureka: true#可以理解为向其他注册中心注册服务
fetch-registry: true#同上
server:
enable-self-preservation: false
instance:
hostname: replica1
配置文件application-replica2.yml
spring:
application:
name: eureka-server
server:
port: 8762
eureka:
client:
service-url:
defaultZone: http://replica1:8761/eureka/#同上
register-with-eureka: true
fetch-registry: true
server:
enable-self-preservation: false
instance:
hostname: replica2
同时运行这两个注册中心
2.2 对应于多注册中心的消费者
配置文件修改为application-replica.yml
spring:
application:
name: eureka-client
server:
port: 8081
eureka:
client:
service-url:
defaultZone: http://replica1:8761/eureka/,http://replica2:8762/eureka/#指明多个注册中心
register-with-eureka: true
fetch-registry: true
其余不变
3. Eureka Server 集成 Security
即访问注册中心的服务或者向注册中心API访问需要验证信息,一般可以是用户名密码的方式认证。
3.1 EurekaServerSecurity注册中心(以单点为例)
pom中在前文的EurekaServer基础上添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
配置文件application-security.yml
spring:
application:
name: eureka-server-security
security: #此处配置验证的账号和密码
user:
name: admin
password: 123456
server:
port: 8763
eureka:
instance:
hostname: localhost
client:
#此处service-url 未配置即默认的
fetch-registry: false#分布式时对应修改
register-with-eureka: false
server:
enable-self-preservation: false
启动配置中心访问配置中心会跳转到输入账号密码界面登陆后即可正常访问配置中心
但是spring-boot-starter-security
默认开启了跨域访问csrf配置类过滤时必不可少的。以下是一种方法仅供参考
添加配置类
package cn.iminqin.springboot.springcloud.learn.eurekaserversecurity.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;
@EnableWebSecurity //Annotation 必不可少
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**"); //指定过滤的APi uri
super.configure(http);
}
}
3.2 对应的Eureka Client Security
主要是配置文件有变化:application-security.yml
spring:
application:
name: eureka-client
server:
port: 8081
eureka:
client:
service-url:
defaultZone: http://admin:123456@localhost:8763/eureka/
register-with-eureka: true
fetch-registry: true
Security
中要求的注册中心url格式为
http://{username}:{password}@{hostname or ip}:{port}/eureka/
#(后面的‘/eureka/’对应于注册中心暴露的API即可)
启动后Client可以通过内置的认证去注册访问注册中心
4 maven 使用问题
4.1 aliyun mirror
指定的settings.xml
配置文件中配置(一般在软件根目录的/conf文件夹)
<mirrors>
<mirror>
<id>aliyunmaven</id>
<mirrorOf>*</mirrorOf>
<name>阿里云公共仓库</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
之后运行maven时添加以下运行指令
对应于IDEA
设置中的Importing
中的VM Options
-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true
对应于IDEA
设置中的Runner
中的VM Options
-Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true
4.2 本地仓库
settings.xml
文件中添加:
<localRepository>/Path/to/Local/Repository</localRepository>
设置本地仓库,发生依赖文件错误,建议网络正常时间充裕且难以简单解决时,删除(清空)本地仓库内容,重新刷新maven。