一、Spring Cloud 微服务注册与发现 实例

126 阅读1分钟

1、 工具及软件版本

JDK: 1.8

Spring Boot: 2.3.0.RELEASE

Spring Cloud: Hoxton.SR6

Maven: 3.5.3

2、服务提供者

2.1、创建Maven项目,artifactId是eureka-server,pom.xml内容如下:

<project xmlns="maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSch…"

xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4…

4.0.0

com.baiyilanjian.cloud

eureka-server

1.0.0

jar

org.springframework.boot

spring-boot-starter-parent

2.3.0.RELEASE

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

org.springframework.cloud

spring-cloud-dependencies

Hoxton.SR6

pom

import

org.springframework.boot

spring-boot-maven-plugin

2.2、spring boot的启动类,添加注解,表示这是一个注册中心,代码如下:

package com.baiyilanjian.cloud;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**

  • Eureka 服务提供者

  • @author

*/

@SpringBootApplication

@EnableEurekaServer

public class EurekaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServerApplication.class, args);

}

}

2.3,修改application.yml配置文件,代码如下:

spring:

application:

name: eureka-server#服务名称

server:

指定该Eureka实例的端口

port:8081

过滤此条日志的输出

logging:

level:

com.netflix: warn

eureka:

client:

registerWithEureka: false

fetchRegistry: false

serviceUrl: defaultZone: http://localhost:8081/eureka/

2.4、访问 http://localhost:8081/ ,进入以下界面

3、服务消费者

3.1、创建Maven项目,artifactId是eureka-client,pom.xml内容如下:

<project xmlns="maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSch…"

xsi:schemaLocation="maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4…

4.0.0

com.baiyilanjian.cloud

eureka-client

1.0.0

jar

org.springframework.boot

spring-boot-starter-parent

2.3.0.RELEASE

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-dependencies

Hoxton.SR6

pom

import

org.springframework.boot

spring-boot-maven-plugin

3.2、spring boot的启动类,添加注解,表示这是一个注册中心,代码如下:

package com.baiyilanjian.cloud;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**

  • Eureka 服务消费者

  • @author

*/

@SpringBootApplication

@EnableDiscoveryClient

public class EurekaClientApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaClientApplication.class, args);

}

}

3.3,修改application.yml配置文件,代码如下:

spring:

application:

name: eureka-client#服务名称

server:

指定该Eureka实例的端口

port:8082

eureka:

client:

serviceUrl:

defaultZone: http://localhost:8081/eureka/

3.4、访问 http://localhost:8081/ ,显示以下界面,服务注册成功