记一次搭建Eureka过程出现的问题

server端

1.添加依赖

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
   <version>2.1.0.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.yml配置

server:
  port: 7777
spring:
  application:
    name: eureka-server
  # 安全认证
  security:
    user:
      name: admin
      password: 123456

eureka:
  instance:
    hostname: localhost
  client:
    # 实例是否在eureka服务器上注册自己的信息以供其他服务发现
    register-with-eureka: false
    # 此客户端是否获取eureka服务器注册列表上的注册信息
    fetch-registry: false
    service-url:
      defaultZone: http://admin:123456@${eureka.instance.hostname}:${server.port}/eureka/

3.在启动类上添加注解

@SpringBootApplication
@EnableEurekaServer
public class DemoApplication {

   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

4.如果加了安全认证模块,还需要以下类,不然客户端注册的时候会报没权限。

/**
 * 由于加了Spring Security的安全认证,
 * 而Spring Cloud 2.0 以上的security默认启用了csrf检验,如果不关闭的话会导致Eureka client端注册不上
 * @author admin
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.csrf().disable();
        super.configure(http);
    }
}

client端

1.添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>

2.配置文件

# eureka配置
eureka:
  client:
    service-url:
      # 指定服务注册中心的地址
      defaultZone: http://admin:123456@localhost:7777/eureka/
  instance:
    # 显示ip地址
    preferIpAddress: true

3.启动类加注解

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }

}

配置中遇到的问题

  • 注册中心地址的后面要加eureka,并且服务端和客户端要一致。(第一次配置客户端的时候落下了一个‘/’,死活注册不上
  • pom依赖要加web模块,要不然也会注册不上。

猜你喜欢

转载自www.cnblogs.com/tobyhomels/p/12786867.html