一、服務(wù)端的搭建
1、在pom文件中添加eureka服務(wù)依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2、編寫application.yml 配置
security:
basic:
enabled: true
user:
name: user
password: password123
server:
port: 8761
eureka:
client:
register-with-eureka: false #只把此服務(wù)當成eurekaservice,不要當成client
fetch-registry: false #只把此服務(wù)當成eurekaservice,不要當成client
service-url:
defaultZone: http://user:password123@localhost:8761/eureka
3、在啟動類上添加注解
@SpringBootApplication
@EnableEurekaServer
就可以啟動服務(wù)發(fā)現(xiàn)的服務(wù)端程序了。
二、客戶端的搭建
1、在pom文件中添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
2、編寫application.yml 配置
server:
port: 7901
session-timeout: 30
tomcat.max-threads: 0
tomcat.uri-encoding: UTF-8
spring:
application:
name: a-microservice-consumer-movie
logging:
level:
root: INFO
com.example.demo: debug
eureka:
client:
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
instance: #eureka管理頁面客戶端服務(wù)的地址顯示實際IP
prefer-ip-address: true #默認是false
3、在啟動類添加注解
@SpringBootApplication
@EnableEurekaClient //針對Eureka服務(wù)注冊使用
//@EnableDiscoveryClient //可以對其他服務(wù)注冊軟件使用
這樣客戶端配置完畢,先啟動服務(wù)端,再啟動客戶端,服務(wù)端就可以自動發(fā)現(xiàn)客戶端服務(wù)了。