eclipse工具:
1.1.File—>new—>Other…—>Maven —>Maven Module 然后点击 Next。
2.操作如下,点击next
3.组名编辑
4.父工程的pom文件会生成,同时新的子工程生成
5.eureka工程修改,子工程的pom文件修改,修改如下:
6.在eureka项目的src/main/java/包根目录下创建启动类如下:
package com.fsp.eurekaServer;
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);
}
}
7.在eureka项目的src/main/resources根目录下创建application.yml配置文件:
eclipse创建其他格式文件方法:File—>new—>Other…—>File 然后点击 Next,保存文件名 XX.yml即可。
#服务名称
spring:
application:
name: eureka-server
#服务端口
server:
port: 8761
#eureka服务的相关配置
eureka:
instance:
#应用实例主机名
hostname: 127.0.0.1
client:
#是否向注册中心注册自己
register-with-eureka: false
#是否从注册中心查询服务
fetch-registry: false
#注册中心地址
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
8.eureka服务器密码保护
pom文件引入security
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!-- spring-cloud-starter-eureka-server 2.0以上弃用-->
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- spring boot security安全认证启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
修改applicaiton.yml
spring:
#服务名称
application:
name: fsp-eureka
#开启基于http basic的安全认证
security:
user:
name: administrator
password: 123456
#服务端口
server:
port: 8761
#eureka服务的相关配置
eureka:
instance:
#应用实例主机名
hostname: 127.0.0.1
client:
#是否向注册中心注册自己
register-with-eureka: false
#是否从注册中心查询服务
fetch-registry: false
#注册中心地址
service-url:
#http://用户名:密码@地址:端口/eureka/
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
配置WebSecurityConfig
//只需通过Spring-boot-starter-security将Spring Security添加到服务路径中,
//就可以保护Eureka服务。默认情况下,当Spring Security位于类路径上时,它将要求在每次向应用程序发送请求时都发送一个有效的CSRF令牌。
//Eureka客户机通常不会拥有一个有效的跨站点请求伪造令牌(CSRF),您需要禁用/ Eureka /**端点的这个请求
package com.fsp.eurekaServer.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
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
//只需通过Spring-boot-starter-security将Spring Security添加到服务路径中,
//就可以保护Eureka服务。默认情况下,当Spring Security位于类路径上时,它将要求在每次向应用程序发送请求时都发送一个有效的CSRF令牌。
//Eureka客户机通常不会拥有一个有效的跨站点请求伪造令牌(CSRF),您需要禁用/ Eureka /**端点的这个请求
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
9.eureka的客户端配置
创建子工程后,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>
</dependency>
</dependencies>
application.yml文件修改
spring:
#服务名称
application:
name: fsp-test
#服务端口
server:
port: 8081
eureka:
client:
service-url:
defaultZone: http://administrator:123456@10.0.199.19:8761/eureka/
//指定在eureka服务器上显示ip和端口号
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
启动文件上加注解@EnableDiscoveryClient指定为客户端