创建spring-cloud工程
构建一个Maven项目:spring-cloud-demo
在pom.xml文件中加入:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.20.RELEASE</version>
</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.version>Dalston.SR4</spring-cloud.version>
<spring-boot.version>1.5.20.RELEASE</spring-boot.version>
</properties>
<!--对子模块依赖包的版本统一控制,子模块需要显示引用,子模块也可以重新声明版本-->
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
搭建服务注册中心Eureka服务
在spring-cloud-demo工程中添加module:eureka-server
在pom.xml文件中加入依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
在resource目录下创建文件:application.properties,内容如下:
################### Eureka服务端配置 ####################
server.port=8761
eureka.instance.hostname=localhost
#不向注册中心注册自己
eureka.client.register-with-eureka=false
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务, 所以也设置为false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
创建一个启动类:EurekaServerApplication.java,添加如下内容:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
运行EurekaServerApplication类中的main方法,由此Eureka服务端搭建完成
在浏览器中访问Eureka服务端:http://localhost:8761/
服务注册与发现:搭建Eureka服务注册者
在spring-cloud-demo工程中添加module:demo-service
在pom.xml文件中加入依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
在resource目录下创建文件:application.properties,内容如下:
########## Eureka客户端配置 ############
server.port=9806
spring.application.name=demo-service
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
创建一个启动类:DemoServiceApplication.java,添加如下内容:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class DemoServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DemoServiceApplication.class, args);
}
}
运行DemoServiceApplication类中的main方法,由此Eureka注册者搭建完成
在浏览器中访问Eureka服务端:http://localhost:8761/,发现demo-service已经注册进来了
添加一个Controller:PortController.java,添加如下内容:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PortController {
@Value("${server.port}")
private String port;
@RequestMapping("/port")
public String getPort() {
return "I am demo-service, I'm from port : " + port;
}
}
重新运行eureka-client项目,即EurekaClientApplication类中的main方法
在浏览器中访问:http://localhost:9806/port,即输出:I am demo-service, I'm from port : 9806
高可用注册中心
在eureka-server工程的resource目录下创建两个配置文件
application-peer1.properties
################### Eureka服务端配置 ####################
spring.application.name=eureka-server
server.port=8761
eureka.instance.hostname=peer1
#不向注册中心注册自己
eureka.client.register-with-eureka=false
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务, 所以也设置为false
eureka.client.fetch-registry=false
#高可用配置:注册到其他配置中心
eureka.client.serviceUrl.defaultZone=http://peer2:8762/eureka/
application-peer1.properties
################### Eureka服务端配置 ####################
spring.application.name=eureka-server
server.port=8761
eureka.instance.hostname=peer2
#不向注册中心注册自己
eureka.client.register-with-eureka=false
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务, 所以也设置为false
eureka.client.fetch-registry=false
#高可用配置:注册到其他配置中心
eureka.client.serviceUrl.defaultZone=http://peer1:8761/eureka/
编辑系统中C:\Windows\System32\drivers\etc目录下的hosts文件,添加如下内容:
127.0.0.1 peer1
127.0.0.1 peer2
将eureka-server工程打成jar包,然后用java -jar 命名运行eureka-server.jar两次并指定不同的配置文件:
java -jar eureka-server-0.0.1.jar --spring.profiles.active=peer1
java -jar eureka-server-0.0.1.jar --spring.profiles.active=peer2
后台运行
nohup java -jar eureka-server-0.0.1.jar --spring.profiles.active=peer1 >./log/eureka-1.log &
然后访问:http://localhost:8761/,即发现DS Replicas项下复制项:peer2
然后访问:http://localhost:8762/,即发现DS Replicas项下复制项:peer1
即将注册中心注册到其他注册中心上去,达到注册中心高可用的目的
服务注册者注册到Eureka集群中配置:
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/,http://localhost:8762/eureka/