搭建服务注册中心
创建一个maven工程,做为注册中心,引入pom
依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--SpringCloud eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
配置application.ym
l文件
###服务启动端口号
server:
port: 8100
##定义服务名称
spring:
application:
name: com.songzixian
###服务注册名称
eureka:
instance:
hostname: 127.0.0.1
###客户端调用地址
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:8001/eureka/
###因为该应用为注册中心,需要将服务注册到注册中心(集群的时候需要为true)
register-with-eureka: true
###因为改应用为注册中心,不需要检索服务信息
fetch-registry: true
启动Eureka服务
@EnableEurekaServer
@SpringBootApplication
public class AppEureka1 {
public static void main(String[] args) {
SpringApplication.run(AppEureka1.class,args);
}
}
搭建服务消费者
创建一个maven工程,做为服务的消费者,引入pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot整合eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
配置application.yml文件
###服务启动端口号
server:
port: 8001
###服务名称(服务注册到eureka名称)
spring:
application:
name: com.songzixian
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
###因为该应用为注册中心,不会注册自己
register-with-eureka: true
###是否需要从eureka上获取注册信息
fetch-registry: true
创建一个测试类
@RestController
public class TestMabber {
@RequestMapping("testMabber")
public String testMabber(){
System.out.println("com.songzixian");
return "办法执行了";
}
}
启动服务
@EnableEurekaClient
@SpringBootApplication
public class AppManner {
public static void main(String[] args) {
SpringApplication.run(AppManner.class,args);
}
}
效果图