服务注册中心:
spring boot版本为2.0.0.RELEASE
pom文件:
<dependency>
<!-- spring cloud配置 -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!-- spring cloud注册中心配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!-- jaxb模块引用 - start -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- jaxb模块引用 - end -->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置文件:
# 在默认设置下,该服务注册中心也将自己作为客户端来尝试注册追究,所有我们需要禁用它的客户端注册行为,增加如下配置
eureka:
instance:
hostname: localhost
client:
# 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
register-with-eureka: false
# 表示是否从 Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而为false
fetch-registry: false
service-url:
default-zone: http://localhost:1111/eureka/
server:
# 端口号
port: 1111
spring:
# 注册中心服务ID
application:
name: eureka-server
启动类:
/**
* @EnableEurekaServer 启动一个服务注册中心提供给其他应用进行对话
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
服务提供者:
pom文件:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<!-- spring cloud配置 -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!-- spring cloud注册中心配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
配置文件:
spring:
application:
# 服务器名称
name: compute-service1
cloud:
config:
discovery:
# 启动服务发现的功能,开启了才能调用其它服务
enabled: true
# 发现的服务的名字--对应注测中心的服务名字
service-id: eureka-server
server:
# 端口号
port: 2222
servlet:
context-path: /api/hello
eureka:
client:
service-url:
defaultZone: http://localhost:1111/eureka/
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @EnableDiscoveryClient 基于spring-cloud-commons,激活Eureka中的DiscoveryClient实现(自动化配置,创建DiscoveryClient接口针对Eureka
* 客户端的EnableDiscoveryClient实例),才能实现Controller中对服务信息的输出
* @EnableEurekaClient 基于spring-cloud-netflix
* 如果选用的注册中心是eureka,那么就推荐@EnableEurekaClient,如果是其他的注册中心,那么推荐使用@EnableDiscoveryClient
*/
@EnableEurekaClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}