先来效果图
eureka集群节点一
eureka集群节点二
节点一显示的注册服务
节点二显示的注册服务
eureka服务端配置如下所示
现在将eureka服务端放到了测试环境 192.168.31.50 192.168.31.60
服务信息
最后给出全部的eureka代码
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.eureka</groupId><artifactId>pingan-cloud</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.14</version><relativePath/></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2021.0.1</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>org.springframework.security</groupId>--> <!-- <artifactId>spring-security-web</artifactId>--> <!-- <version>5.4.11</version>--> <!-- </dependency>--><dependency><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>1.4.20</version></dependency><dependency><groupId>org.codehaus.jettison</groupId><artifactId>jettison</artifactId><version>1.5.4</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>3.1.0</version><configuration><encoding>UTF-8</encoding></configuration></plugin></plugins><finalName>eureka</finalName></build></project>
application.properties
#端口号 server.port=8764 eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@192.168.31.60:8763/eureka eureka.client.register-with-eureka=false eureka.client.fetch-registry=false #关闭自动保护机制 eureka.server.enable-self-preservation=false #eureka清理无效节点的时间间隔,单位毫秒 eureka.server.eviction-interval-timer-in-ms=60000spring.application.name=eureka_8764#日志打印级别 #logging.level.com.eureka=info #logging.level.web=info #spring.devtools.add-properties=falselogging.level.com.eureka=debug logging.level.org.springframework.web.client.RestTemplate=info logging.level.org.springframework.web.HttpLogging=info logging.level.web=debug spring.devtools.add-properties=falsespring.security.user.name=qiuxie spring.security.user.password=E13301455191qiuxiespring.security.basic.enable=true
代码部分
package com.eureka.config;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; import org.springframework.security.config.http.SessionCreationPolicy;@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);http.csrf().disable();//注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic,// 如果是form方式,不能使用url格式登录http.authorizeRequests().anyRequest().authenticated().and().httpBasic();} }
package com.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /*** @author yourheart* @Description* @create 2022-04-20 21:17*/ @SpringBootApplication @EnableEurekaServer public class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class,args);} }
项目结构展示
客户端配置为
eureka.client.serviceUrl.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@192.168.31.60:8763/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@192.168.31.50:8764/eureka/spring.security.user.name=aa spring.security.user.password=123456