上篇讲了如何简单搭建Eureka服务端,现在来谈下如何搭建一个简单的服务提供方。
服务提供方:将自身服务注册到 Eureka 注册中心,从而使服务消费方能够找到。
简单搭建步骤如下:
1.在pom.xml中需要引入的依赖如下,其中spring-cloud-starter-netflix-eureka-client,spring-boot-starter-web为必要依赖,没有spring-boot-starter-web依赖的话会导致启动报错:
<?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>
<groupId>com.example</groupId>
<artifactId>spring-cloud-eureka-provider-a</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-eureka-provider-a</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud-services.version>2.0.1.RELEASE</spring-cloud-services.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>
</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>
<dependency>
<groupId>io.pivotal.spring.cloud</groupId>
<artifactId>spring-cloud-services-dependencies</artifactId>
<version>${spring-cloud-services.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>
2.在启动类加上@EnableEurekaClient注解启动Eureka客户端,也可以用@EnableDiscoveryClient,区别在于@EnableEurekaClient只适用于Eureka,而@EnableDiscoveryClient可以用于其他且包括Eureka的服务治理框架:
package com.example.eureka.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
@RestController
public class SpringCloudEurekaProviderAApplication {
@RequestMapping("/a")
public String serverA() {
System.out.println("spring-cloud-eureka-provider-a");
return "spring-cloud-eureka-provider-a";
}
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaProviderAApplication.class, args);
System.out.println("spring-cloud-eureka-provider-a service has started!");
}
}
3.配置文件application.yml,需要配置才能找到Eureka服务器:
server:
#自身应用端口号
port: 10001
eureka:
client:
service-url:
#该属性来指定服务注册中心的地址
defaultZone: http://localhost:8761/eureka/
spring:
application:
#服务命名,可在注册中心页面看到,需要说明下,服务名可以相同,但是端口必须不同
name: spring-cloud-eureka-provider-a
4.启动工程启动类SpringCloudEurekaProviderAApplication:
在注册中心界面可以看到,我们新建的服务应用已经成功注册。
下篇谈如何搭建一个简单的服务消费者。