5分钟搭建SpringCloud Eureka服务注册中心的实现
创建父级项目 只需保留pom.xml文件
这里只需搭建一个微服务 其他操作并无
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tyy.springcloud</groupId> <artifactId>cloudstudy</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <!-- 这里是父级下面控制的子级 --> <modules> <!-- 服务客户端 --> <module>cloud-provider-8001</module> <!-- 注册中心 --> <module>cloud-eureka-server9001</module> </modules> <!-- 统一管理jar包版本 --> <!-- 具体这样 就是为了方便不在选择jar包版本号 防止jar包冲突报错 --> <properties> <!-- 编码格式 与JDK1.8 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <!--子模块继承之后,提供作用:锁定版本+子module不用groupId和version--> <dependencyManagement> <dependencies> <!--spring boot 2.2.2--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud Hoxton.SR1--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
搭建注册中心 cloud-eureka-server9001
首先搭建项目基本就是 写pom,写配置…
pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloudstudy</artifactId> <groupId>com.tyy.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-eureka-server9001</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
aplication.yml
server: port: 9001 eureka: instance: hostname: eureka9001.com #eureka服务端的实例名称 client: # false 表示不向注册中心注册自己 register-with-eureka: false # false 表示自己就是注册中心我的职责就是维护服务实例,并不需去检查服务 fetch-registry: false service-url: # 集群就是指向其他eureka 单机就是指向自己 #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址 defaultZone: http://eureka9001.com:9001/eureka/ server: #关闭自我保护机制,保证不可用服务被及时踢除 enable-self-preservation: false eviction-interval-timer-in-ms: 2000
3.启动类
@SpringBootApplication @EnableEurekaServer public class Eureka9001 { public static void main(String[] args) { SpringApplication.run(Eureka9001.class,args); } }
去电脑C:\Windows\System32\drivers\etc 里在hosts 文件
如果找不到,把隐藏文件显示出来就行了
搭建客户端 cloud-provider-8001 注册到9001
依旧先写入pom文件
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloudstudy</artifactId> <groupId>com.tyy.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-provider-8001</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
2.application.yml
server: port: 8001 spring: application: name: cloud-dept-service datasource: username: root password: root url: jdbc:mysql://localhost:3306/db2020?useUnicode=true&characterEncoding-utr-8&useSSL=false driver-class-name: com.mysql.jdbc.Driver eureka: client: # 表示是否将自己注册到EurekaServer 默认true register-with-eureka: true service-url: defaultZone: http://eureka9001.com:9001/eureka/ instance: instance-id: 8001 prefer-ip-address: true #访问路径显示ip地址 mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3.启动类
@SpringBootApplication @EnableEurekaClient public class DeptMain8001 { public static void main(String[] args) { SpringApplication.run(DeptMain8001.class,args); } }
测试
是不是很简单呢 启动时 要先启动注册中心 再启动客户端
这样就算搭建好啦~!
到此这篇关于5分钟搭建SpringCloud Eureka服务注册中心的实现的文章就介绍到这了,更多相关SpringCloud Eureka服务注册中心内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)