一来先抛两个异常再说:

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

这是我目前遇到的问题,百度了一番,说是security的问题,可是我没有引入springsecurity啊,我以前搭建eureka demo也没有出现过这个问题啊。
于是去看了下依赖,好家伙server里面居然引入了spring-security
在这里插入图片描述
于是找到一篇文章,copy他的配置过来:
eureka服务添加security验证之后,client注册失败
eureka-server》application.yml

server:
  port: 1111

spring:
  application:
    name: eureka-server

  security:
    user:
      name: admin
      password: 123456

eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    #是否向注册中心注册注册自己
    register-with-eureka: false
    #是否开启检索服务
    fetch-registry: false
    #注册服务地址
    serviceUrl:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:1111/eureka/

eureka_client》application.yml

server:
  port: 7777

spring:
  application:
    name: eureka_client

  profiles:
    active: native

  security:
    user:
      name: admin
      password: 123456

  cloud:
    config:
      server:
        native:
          searchLocations: classpath:/config

eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:1111/eureka/

启动server不报错了,还可以,以为成功了,启动client,访问web:
在这里插入图片描述
在这里插入图片描述
这是怎么了?????
再百度:eureka client为什么不能成功注册到server
o,soga!!!!
修改依赖:
eureka_server.pom.xml

    <artifactId>eureka_server</artifactId>
    <properties>
        <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
    </properties>
    <dependencies>
       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

eureka_client.pom.xml

<artifactId>eureka_client</artifactId>

    <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

再次重启。
在这里插入图片描述
ok,没毛病!!!
总结几点:

  1. 依赖用spring-cloud-starter-netflix-eureka-server和spring-cloud-starter-netflix-eureka-client,idea自动引入的时候注意不要引错。
  2. 高版本的cloud有所改變:http:// s p r i n g . s e c u r i t y . u s e r . n a m e : {spring.security.user.name}: spring.security.user.name:{spring.security.user.password}@${eureka.instance.hostname}:1111/eureka/
  3. 去看看springcloud文档,收获可能会颇多。
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐