栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known serve

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known serve

一、问题描述
(1)背景:
学习SpringCloud
(2)我在干嘛:
集群Eureka server.两个,一个客户端Eureka client
(3)问题描述:
SpringCloud Eureka客户端启动报错。
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

(4)问题分析:
年纪大了,漏写了两个注解~

就是加权限认证的时候

    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // 关闭csrf
            http.csrf().disable();
            // 支持httpBasic
            http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
        }
    }

少了两个注释,在WebSecurityConfig类上加上这两个注释以后,客户端就可以丝滑的启动啦~

 @Configuration
 @EnableWebSecurity

就是这个呀

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // 关闭csrf
            http.csrf().disable();
            // 支持httpBasic
            http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
        }
    }

备注:我百度过,有人说是因为这个配置写错的

eureka.client.service-url.defaultZone=http://yolanda:123@localhost:8762/eureka/

说是这个url后面要加个/,???,反正我加了还是报这个错的,经过我的检查发现是漏了注解。

二、详细的代码
啊,我看的是这本书《Spring Cloud微服务入门、实战与进阶》,所以代码也是照书上敲的,我jio得这本书很不错,上面的代码都跑得通,步骤也很详细~
为了记录我的错误,形成完整规范的笔记,我才把代码贴出来。

1、服务端A和B
新建一个maven项目,它的名字叫做eureka-server-cluster
(1)pom文件



    4.0.0

    com.yolanda
    eureka-server-cluster
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.6.RELEASE
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.SR2
                pom
                import
            
        
    

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

        
            org.springframework.boot
            spring-boot-starter-security
        

        
            junit
            junit
            test
        
    



(2)新建类App.java

package com.yolanda;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.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;


@EnableEurekaServer
@SpringBootApplication
public class App {

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

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // 关闭csrf
            http.csrf().disable();
            // 支持httpBasic
            http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
        }
    }

}

(3)resources里面新建一个application.properties

spring.application.name=eureka-server-cluster
spring.profiles.active=master

# 不向自己注册
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:
  ${spring.cloud.client.ip-address}:${server.port}

# 权限认证
spring.security.user.name=yolanda
spring.security.user.password=123

再新建一个application-master.properties

server.port=8761

eureka.client.service-url.defaultZone=http://yolanda:123@localhost:8762/eureka/

再新建一个application-slaveone.properties

server.port=8762

eureka.client.service-url.defaultZone=http://yolanda:123@localhost:8761/eureka/

为什么一个叫master一个叫slaveone嘞,就是说有两个机器上一个部署master的,那它的
application.properties里就这样写

spring.profiles.active=master

另一个机器部署slaveone,那它的application.properties就这样写

spring.profiles.active=slaveone

都在本地上启动就copy两个项目,就这一个配置不一样,启动就好啦~

2、客户端
新建一个maven项目,它的名字叫做eureka-client-user-service
(1)pom文件



    4.0.0

    com.yolanda
    eureka-client-user-service
    1.0-SNAPSHOT

    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.6.RELEASE
        
        
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Finchley.SR2
                pom
                import
            
        
    

(2)新建一个启动类,App.java

package com.yolanda;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


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

(3)新建一个controller,UserController.java

package com.yolanda.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserController {

    @GetMapping("/user/hello")
    public String hello() {
        return "hello";
    }
}

(4)resources里面新建一个application.properties

spring.application.name= eureka-client-user-service
server.port=8081

#eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
eureka.client.service-url.defaultZone=http://yolanda:123@localhost:8761/eureka/,http://yolanda:123@localhost:8762/eureka/
# 采用IP注册
eureka.instance.prefer-ip-address=true
# 定义实例ID格式
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

# 开启健康检查
eureka.client.healthcheck.enabled=true
# 续约更新时间间隔(默认30秒)
eureka.instance.lease-renewal-interval-in-seconds= 10
# 续约到期时间(默认90秒)
eureka.instance.lease-expiration-duration-in-seconds= 30

#自定义元数据
eureka.instance.metadata-map.myownproperty=yolandaisbeautiful

3、浏览器
没加注解前网页也会报错的
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

加类注解以后就好啦~

4、获取服务的注册信息
http://localhost:8761/eureka/apps/eureka-client-user-service

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955694.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号