Eureka的创建
Eureka服务端
创建Eureka服务端
在启动类中添加注解开启Eureka服务
@SpringBootApplication
@EnableEurekaServer//开启Eureka服务
public class Day1212EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(Day1212EurekaApplication.class, args);
}
}
application.yml
server:
port: 8080 # 端口号
spring:
application:
name: eureka-user # 服务名
eureka:
client:
register-with-eureka: false # 是否注册Eureka本身到注册表中(默认为true)
fetch-registry: false # 是否拉取其它服务的信息(默认是true)
service-url:
defaultZone: http://localhost:${server.port}/eureka # Eureka本身的地址
在pom.xml文件中添加依赖(eureka服务端)
<!-- eureka服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Eureka服务端创建完成
效果图
application.yml中的eureka:client:register-with-eureka: true(改为true)
效果图
Eureka客户端
在客户端pom.xml文件中添加依赖:
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
<version>2.2.0.RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- SpringCloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
启动类中添加Eureka客户端功能
@SpringBootApplication
@EnableEurekaClient // 开启EurekaClient客户端
public class Day1212ProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run(Day1212ProductServiceApplication.class, args);
}
}
客户端application.yml
server:
port: 8082 # 端口号
spring:
datasource:
url: jdbc:mysql://localhost:3306/sql?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
application:
name: product-service # 服务名
eureka:
client:
service-url: # EurekaServer地址
defaultZone: http://127.0.0.1:8080/eureka # Eureka服务端地址
效果图
第一次写博客,如果有不正确的地方,欢迎大家纠正,谢谢