1.创建Springboot项目
![](https://img-blog.csdnimg.cn/img_convert/f33b6da88c10b79a91afdc0807b7ce66.png)
![](https://img-blog.csdnimg.cn/img_convert/908e50227a276726c1c2ad3185ade99b.png)
![](https://img-blog.csdnimg.cn/img_convert/37284376f6b1c69714c385c19666c6f7.png)
![](https://img-blog.csdnimg.cn/img_convert/43436796b68a06be79fdbfe41cd7b0f5.png)
2.项目pom依赖
<?xmlversion="1.0" encoding="UTF-8"?>
<projectxmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<groupId>com.cssl</groupId>
<artifactId>kgc47eureka6060</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>kgc47eureka6060</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR10</spring-cloud.version>
</properties>
<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>
</dependencies>
</project>
3.application.yml配置
server:
port: 6060 #Eureka Server和Tomcat端口(不配默认8761,Tomcat8080)
spring:
application:
name: eureka #应用名称,也就是微服务名称,不写默认空
security: #注册到eureka需要用户名和密码
user:
name: admin
password: 123
eureka:
server:
enable-self-preservation: false #禁用保护,默认true
eviction-interval-timer-in-ms: 10000 #清理间隔(默认是60*1000)
client:
fetch-registry: false #是否从eureka server获取服务信息,默认true
register-with-eureka: false #是否向eureka server注册,默认true
service-url:
defaultZone: http://localhost:6060/eureka/ #注册地址(不配默认8761)
4.启动类:
@SpringBootApplication
@EnableEurekaServer
publicclassKgc47eureka6060Application {
publicstaticvoidmain(String[] args) {
SpringApplication.run(Kgc47eureka6060Application.class, args);
}
}
5.设置csr检查(坑)
在2.0以后,需要加入这个检查设置,不然eureka-client无法注册到eureka-server上去
@EnableWebSecurity
publicclassWebSecurityConfigextendsWebSecurityConfigurerAdapter {
@Override
protectedvoidconfigure(HttpSecurityhttp) throwsException {
http.csrf().disable(); //禁止使用csr验证
super.configure(http);
}
}
6.运行访问
![](https://img-blog.csdnimg.cn/img_convert/c89ecfeab0182713059ede4b49fbc1a9.png)
![](https://img-blog.csdnimg.cn/img_convert/51bfd3b8df9eeb73619d61ba3f41f7d0.png)
7.创建eureka-client客户端项目(除了eureka server注册中心以外其他都是eureka-客户端(都要导web包,其他包情况而定))
仍然是创建springboot项目,只是在选择组件的注意
![](https://img-blog.csdnimg.cn/img_convert/40b9e9443649eb05ad541fe3625985f8.png)
8.项目pom依赖
<?xmlversion="1.0" encoding="UTF-8"?>
<projectxmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<groupId>com.cssl</groupId>
<artifactId>kgc47consumer8090</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>kgc47consumer8090</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR10</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
9.application.yml配置
提供者:
server:
port: 7001
spring:
application:
name: provider
eureka:
client:
register-with-eureka: true #向eurek server 注册
fetch-registry: true #获取信息列表
service-url:
defaultZone: http://admin:123@localhost:6001/eureka/ #注意用户名和密码 @
10.启动类
@SpringBootApplication
@EnableEurekaClient与@EnableDiscoveryClient(这个注解可以向任何注册中心注入)一样
publicclassKgc47consumer8090Application {
publicstaticvoidmain(String[] args) {
SpringApplication.run(Kgc47consumer8090Application.class, args);
}
}
11.查看eureka主页
访问 http://127.0.0.1:6060 ,在applications中显示客户端信息
![](https://img-blog.csdnimg.cn/img_convert/fe5c323680c8c5bcdb7ade9ea26fa350.png)