eureka注册微服务
建立eureka微服务
准备yml资源配置文件
server: port: 10086 spring: application: name: eureka eureka: client: service-url: defaultZone: http://127.0.0.1:10086/eureka register-with-eureka: false fetch-registry: false
准备依赖
<!-- web --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
在微服务中编写启动类
@SpringBootApplication @EnableEurekaServer public class EurekaApp { public static void main(String[] args) { SpringApplication.run(EurekaApp.class); } }
建立服务端server
准备yml资源配置文件
eureka: client: registry-fetch-interval-seconds: 5 # 获取服务列表的周期:5s service-url: defaultZone: http://127.0.0.1:10086/eureka instance: prefer-ip-address: true
准备依赖
<!-- web依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- eureka客户端依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.0.1.RELEASE</version> </dependency>
建立server启动类
@SpringBootApplication @EnableEurekaClient @MapperScan("xxxx") public class StuApp { public static void main(String[] args) { SpringApplication.run(StuApp.class); } }
这样注册就完成了
添加服务端验证
<!--security 验证依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
yml资源配置文件
#应用名称以及验证账号
spring:
application:
name: eureka
security:
user:
name: root #配置注册中心的登录用户
password: root #配置注册中心的密码
添加配置类(官网)
@EnableWebSecurity class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().ignoringAntMatchers("/eureka/**"); super.configure(http); } }
再次启动对应的服务端就需要密码了