eclipse创建子工程eureka(cloud搭建二)

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指定为客户端

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值