本文档主要工作在于配置各项业务服务器的必要流程和操作以及相关配置,不再重点描述项目的创建过程以及业务代码等。
Eureka服务配置
POM.xml配置
针对注册中心,主要pom.xml中需要引入以下结果依赖
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<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-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
由于创建springboot时我们选择的基础版本为2.3.1,因为我们选择对应的springcloud版本为Hoxton
- spring-cloud-starter-netflix-eureka-server:为Eureka注册中心的依赖
- spring-boot-starter-security:由于springcloud的Hoxton版本引入了安全认证,所以我们需要配置认证信息
启动类配置
package com.ym.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
主要为需要增加注释**@EnableEurekaServer**
安全认证配置
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;
/**
* Eureka安全配置
*/
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//关闭csrf
http.csrf().disable();
//开启认证
super.configure(http);
}
}
通过注释**@EnableWebSecurity**表名此配置为提供Eureka安全认证服务
YML配置
USER: eureka
PASSWORD: com.ym
server:
port: 8081 #服务注册中心端口号
spring:
application:
name: study-eureka
security:
user:
name: ${USER}
password: ${PASSWORD}
eureka:
instance:
hostname: 127.0.0.1 #服务注册中心IP地址
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
client:
registerWithEureka: true #是否向服务注册中心注册自己
fetchRegistry: true #是否检索服务
serviceUrl: #服务注册中心的配置内容,指定服务注册中心的位置
defaultZone: http://${USER}:${PASSWORD}@${eureka.instance.hostname}:${server.port}/eureka/
以下几点需要注意配置
- security.user.name/password为配置本注册中心登录的用户名和密码,后续连接此注册中心时,都需要此认证,包括登录网页端如下
- eureka.instance.hostname 显示为注册中心的IP地址,如果不指定则默认显示为主机名称
- prefer-ip-address 使用IP地址代替主机名称显示
- eureka.client.serviceUrl.defaultZone 由于我们需要使用安全认证,因此 需要使用http://user:password@host:port/eureka格式配置。如果配置多个,则使用“,”隔开显示,即http://user:password@host1:port/eureka,http://user:password@host2:port/eureka
其中host1和host2分别为自己和其他注册中心的地址
业务服务配置
POM.xml配置
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
- 使用与注册中心同样版本的springcloud
- 引入eureka的客户端依赖
启动类配置
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ManageApplication {
public static void main(String[] args) {
SpringApplication.run(ManageApplication.class, args);
}
}
- 使用注释**@EnableDiscoveryClient**标记此服务为客户端
YML配置
eureka:
client:
service-url:
defaultZone: http://${USER}:${PASSWORD}@127.0.0.1:8081/eureka/
instance:
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
与注册中心类似,需要配置注册中心的地址,如果有多个则用“,”隔开
项目启动
先启动Eureka注册中心服务,再启动业务服务,登录
http://localhost:8081/
输入上面配置的用户名和密码之后,即可访问Eureka信息页面
如果同一个服务开启了多个实例,则选择部分会展示多个服务主机地址和端口