这里采用springboot 2.1.3.RELEASE,springcloud Greenwich.RELEASE
一、Eureka 服务注册中心
1.maven依赖
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
...
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
...
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>
2.新建配置文件application.yml
单节点配置
新建application-default.yml(这里方便一起说明故另新建application-default.yml,其实作为单节点直接在application.yml中添加即可)
#独立eureka注册服务中心
server:
port: 7001
spring:
application:
name: eureka-server
eureka:
instance:
hostname: peer1
instance-id: ${spring.application.name}:${spring.cloud.client.hostname}:${server.port}
client:
register-with-eureka: false #由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
fetch-registry: false #检索服务列表
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
双节点配置
新建application-peer1.yml
server:
port: 7001
spring:
application:
name: eureka-server
eureka:
instance:
hostname: peer1
instance-id: ${spring.application.name}:${spring.cloud.client.hostname}:${server.port}
client:
service-url:
defaultZone: http://peer2:7002/eureka/
新建application-peer2.yml
server:
port: 7002
spring:
application:
name: eureka-server
eureka:
instance:
hostname: peer2
instance-id: ${spring.application.name}:${spring.cloud.client.hostname}:${server.port}
client:
service-url:
defaultZone: http://peer1:7001/eureka/
application.yml默认启动default单节点
#并行eureka注册服务中心
spring:
profiles:
active:
- default
window在C:\Windows\System32\drivers\etc\hosts文件中添加如下
127.0.0.1 peer1
127.0.0.1 peer2
liunx|centos vi /etc/hosts 添加
3.启动类添加@EnableEurekaServer注解,开启Eureka服务发现的功能
@SpringBootApplication
@EnableEurekaServer
public class SpringcloudEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaApplication.class, args);
}
}
4.java -jar的方式启动高可用Eureka Server
java -jar eureka.jar --spring.profiles.active=peer1
java -jar eureka.jar --spring.profiles.active=peer2
二、Eureka 服务提供者
1.maven依赖
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
...
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.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-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</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>
...
2.application.yml配置
server:
port: 7010
spring:
application:
name: eureka-client
eureka:
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.hostname}:${server.port}
status-page-url: http://${spring.cloud.client.hostname}:${server.port}/demo
client:
register-with-eureka: true #由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
fetch-registry: true #检索服务列表
service-url:
defaultZone: http://peer1:7001/eureka/
3.提供服务接口
@RestController
public class ClientController {
@GetMapping("demo")
public Map<String,Object> demo(String params){
Map<String,Object> result = new HashMap<String,Object>();
result.put("code", "000000");
result.put("message", "调用成功");
JSONObject data = new JSONObject();
data.put("info", "提供服务方");
data.put("params", params==null?"":params);
result.put("data", data);
return result;
}
}
4.启动类
在使用服务发现的时候有两种注解,一种为@EnableDiscoveryClient,一种为@EnableEurekaClient,用法差不多
spring cloud中discovery service有许多种实现(eureka、consul、zookeeper等等)
@EnableDiscoveryClient基于spring-cloud-commons
@EnableEurekaClient基于spring-cloud-netflix
就是如果选用的注册中心是eureka,那么就推荐@EnableEurekaClient,
如果是其他的注册中心,那么推荐使用@EnableDiscoveryClient
@SpringBootApplication
@EnableEurekaClient
public class SpringcloudClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudClientApplication.class, args);
}
}
5.eureka注册中心情况
eureka-7001
eureka-7002
页面结果