继之前Eureka服务端搭建后,新建Eureka客户端
Eureka服务端搭建参考:https://blog.csdn.net/weixin_42465125/article/details/88233722
客户端搭建使用于Eureka Server端同样的方式,在https://start.spring.io/ 生成一个client-demo的客户端工程
然后在Eureka Server端添加Spring Security模块,即在pom文件中添加Spring Security的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
修改properties配置文件:添加用户名和密码和修改service-url
#注册中心端口
server.port=8761
#主机名,会在控制页面中显示
eureka.instance.hostname=localhost
#通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://user:pwd@${eureka.instance.hostname}:${server.port}/eureka/
spring.security.user.name=user
spring.security.user.password=pwd
然后下面来配置客户端
启动类添加客户端服务发现的注解:@EnableDiscoveryClient【Spring提供更通用】或者EnableEurekaClient【语义上的区分,它实际也是一个@EnableDiscoveryClient,可以从它的源码看出,它是被@EnableDiscoveryClient元注解修饰的】
package com.example.clientdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ClientDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ClientDemoApplication.class, args);
}
}
然后是properties配置文件:
#服务端口
server.port=8081
#eureka主机名,会在控制页面中显示
eureka.instance.hostname=localhost
#eureka服务器页面中status的请求路径
eureka.instance.status-page-url=http://localhost:8761/index
#eureka注册中心服务器地址
eureka.client.service-url.defaultZone=http://user:pwd@localhost:8761/eureka/
#服务名
spring.application.name=client-demo
现在启动服务端,然后启动客户端
先确定服务端是OK的:
在添加Spring Security模块后,进入Eureka服务端的界面时就需要输入在配置文件中配置的用户名和密码,如下图:
然后输入用户名和密码登入:
现在说明Eureka Server是OK的
下面来启动客户端
现象1:
网上找了很多都说是修改properties的这两个属性【然后并不是】找了很多,终于找到问题的解决方法了
#通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
问题是因为:新版本的security默认开启csrf了,关掉就好了,在Eureka Server端配置下面一个类【注意是Server端不是Client端哈】,注意包名要能让Spring扫描到
package com.example.eurekademo.csrf;
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;
/**
*
* eureka服务端添加security验证之后,client注册失败 cannot execute any request on any know
* server
*
* 新版本的security默认开启csrf了,关掉就好了
*
*
*/
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable(); // 关闭csrf
http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); // 开启认证
}
}
参考博客:https://blog.csdn.net/weixin_39913200/article/details/80845867
然后再次启动,上面的问题就解决了,会有一个新的问题,但是这个问题如果是真正的开发,可能不会被发生,因为项目中一般来说会写Web层的Controller,那么自然而然就会去依赖spring-boot的web包,这里是实验所以各种问题就会有可能发送,请看下面:
在Eureka Server的界面上面还是没有注册client 的实例
解决方法是,在客户端需要添加springboot-web模块的依赖,在客户端的pom文件中添加下面的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
然后再次启动客户端
可以看到,Client注册成功了
在旧版的情况下没有这两个问题
旧版参考地址:https://blog.csdn.net/weixin_42465125/article/details/88365215
新版代码地址:https://github.com/CUITLLB/eureka-service-register-discovery-demo
********************************* 不积跬步无以至千里,不积小流无以成江海 *********************************