搭建eureka注册中心
1、new project 选择pring initializr
2、删掉里面的文件 只保留pom 使之成为一个空项目
3、new Module 还是spring initializr
然后选择 Spring Cloud DIscovery 》》Eureka Server
启动一个服务注册中心,只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动类上加:
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
4、修改resources下的application.properties 为yml格式 编写配置
# 设置端口
server:
port: 9090
#指定应用名称
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
#指定当前eureka客户端的注册地址 也就是eureka服务的提供方
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
register-with-eureka: false #表示自身不在向eureka注册中心注册
fetch-registry: false #启动时禁用client注册
5、启动服务 浏览器输入 http://127.0.0.1:9090
一个简单的注册中心就配置好了
然后搭建一个服务提供方
新建一个module 创建过程和上面一样
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cl</groupId>
<artifactId>customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>customer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
服务启动类上加注解@EnableEurekaClient 表明自己是一个eurekaclient.
@EnableEurekaClient
@SpringBootApplication
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
}
添加application.yml配置
server:
port: 9091
#指定服务名称
spring:
application:
name: eureka-customer
#指定eureka客户端的注册地址
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:9090/eureka/
最后启动服务打开eureka注册中心 就会看到叫eureka-cusomer服务已经注册上去了