一、问题描述
(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文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yolanda</groupId>
<artifactId>eureka-server-cluster</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath></relativePath>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
(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;
/**
* @Date: 2020/9/22 9:02 AM
* eureka服务端集群
*/
@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文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yolanda</groupId>
<artifactId>eureka-client-user-service</artifactId>
<version>1.0-SNAPSHOT</version>
<!--spring boot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<!--这句话不写会注册失败-->
<relativePath></relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--eureka-->
<dependency>
<!--这里是cloud不是boot-->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--开启健康检查-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<!--spring cloud-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
(2)新建一个启动类,App.java
package com.yolanda;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Date: 2020/9/21 4:37 PM
*/
@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;
/**
* @Date: 2020/9/21 4:54 PM
*/
@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