Eureka服务端创建及服务注册
一、Eureka服务端创建
1. 引入依赖:pom.xml
<!-- eureka 服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
PS:此处不加版本是因为在子模块中引入的依赖,父模块已经管理了相关版本
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.SR10</spring-cloud.version> <mysql.version>5.1.47</mysql.version> <mybatis.version>2.1.1</mybatis.version> </properties> <dependencyManagement> <dependencies> <!-- springCloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencyManagement>
2. 添加注解:SpringBoot 启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
3. 添加配置:application.yml
server:
port: 10086 # 端口
spring:
application:
name: eurekaServer # 服务名称
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka # eureka 的地址信息(服务端可以将自己注册到注册中心)
4. 访问地址,端口与 eureka 服务端 application.yml 中配置一致
http://localhost:10086/
二、客户端创建
1. 引入依赖:pom.xml
<!-- eureka 客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2. 添加配置:application.yml
server:
port: 8080 # 端口
spring:
application:
name: orderServer # 服务名称
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka # eureka 的地址信息
3. 服务调用
参考:https://www.cnblogs.com/zhousjcn/p/15511161.html
三、总结