服务注册与发现有多个组件可以使用,Eureka、zookeeper、nacos等,这里以Eureka为例。
先new Directory 为hello-spring-cloud-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>
<parent>
<groupId>com.funtl</groupId>
<artifactId>hello-spring-cloud-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
</parent>
<artifactId>hello-spring-cloud-eureka</artifactId>
<packaging>jar</packaging>
<name>hello-spring-cloud-eureka</name>
<url>http://www.funtl.com</url>
<inceptionYear>2018-Now</inceptionYear>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Cloud End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.einblatt.hello.spring.cloud.eureka.EurekaApplication</mainClass>
<!--mainClass的路径为SpringBoot启动类-->
</configuration>
</plugin>
</plugins>
</build>
</project>
点击右侧maven projects 点击+
号选中刚刚添加的pom然后点击ok
创建目录结构
new Directory --src/main/java src/main/resources
创建文件目录
new Directory --com.funtl.hello.spring.cloud.eureka
创建启动类
package com.funtl.hello.spring.cloud.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
在resources下创建application.yml文件
spring:
application:
name: hello-spring-cloud-eureka
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false # registerWithEureka 和 fetchRegistry 为false 代表Eureka此时
fetchRegistry: false #是个服务
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
然后就可以启动启动类访问注册中心管理页面。
注意:后边的几个其他访问也仿照这个访问创建基础文件。