新建工程
新建SpringBoot工程eureka-client
,
其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 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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springcloud.bh</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client</name>
<description>eureka服务提供者</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<!--必须要此依赖否则启动不起来项目-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
配置application.yml
当Client向Server注册时,它会提供一些元数据,例如主机和端口,URL,主页等,Eureka Server从每个Client实例接收心跳消息, 如果心跳超时,则通常将该实例从Server中删除。
server:
port: 8762 #设置端口号,使用ribbon实现负载均衡
spring:
application:
name: eureka-client #指定微服务的名称,后续在调用的时候只需要使用该名称就可以进行服务的访问
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #属性对应服务注册中心的配置内容,指定服务注册中心的位置
Application
添加注解@EnableEurekaClient
,表明自己是一个Eureka Client。
package com.springcloud.bh;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
AdminController
新建AdminController类,作为服务提供者提供接口服务。
package com.springcloud.bh.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AdminController {
@Value("${server.port}")
private String port;
@GetMapping("hi")
public String sayHi(String message)
{
return String.format("你的消息是:%s 端口号:%s",message,port);
}
}
启动多个实例
在Run/Debug Congigurations中选中Eureka Client,勾选Allow parallel run
,表示允许运行多个实例。在application.yml
中修改server.port
来改变多个实例的运行端口。
打开浏览器访问http://localhost:8761 ,查看服务注册中心,可以看到Eureka Clien服务提供者注册了多个实例。
源码地址
Github: https://github.com/yeshang5/springcloud-example