Spring Cloud Eureka 服务治理学习2 注册服务提供者


一、操作

1. pom

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

2. 入口类

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
* @author Cade on 2021-5-31
*/
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@EnableAdminServer
@SpringBootApplication
@EnableDiscoveryClient
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication .class, args);
}
}

3. 配置文件

eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: http://eureka服务端ip:eureka端口号/eureka/
instance:
prefer-ip-address: true
ip-address: 者ip
non-secure-port:
instance-id:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health

二、启动效果

Spring Cloud Eureka 服务治理学习2 注册服务提供者_spring

三、安全验证

给服务端的pom.xml加上安全设置:

spring:
security:
user:
name: admin
password: 123456

这时登陆注册中心就需要输入用户名密码,客户端也需要加入相应配置:

eureka:
instance:
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://admin:123456@eureka server ip:8761/eureka/

没配置用户名密码的话,启动时会报401错误。