一.搭建服务注册中心
1.首先,创建一个基础的Spring Boot 项目,命名为eureka-server,pom.xml文件添加完对应的依赖后,如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="[http://maven.apache.org/POM/4.0.0"](http://maven.apache.org/POM/4.0.0%22); xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance"](http://www.w3.org/2001/XMLSchema-instance%22);
xsi:schemaLocation="[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0)[http://maven.apache.org/xsd/maven-4.0.0.xsd"](http://maven.apache.org/xsd/maven-4.0.0.xsd%22);>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<!-- 注意,不同版本的springboot,对应不同的spring cloud版本,这里使用的是1.5.10 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入Eureka-server依赖,注册服务中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR1</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.在EurekaServerApplication,java(名字根据你的项目名字而不同)中加入注解@EnableEurekaServer,使其变为服务注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3.(先实现单节点模式的服务注册中心)因为服务注册中心,本身也可以作为服务可被注册到其他的中心,我们如果只是实现单节点的服务注册中心,则需要设置2个配置,设置为本身不能被注册.如下,在配置文件,application.properties
#端口不冲突即可
server.port=1111
eureka.instance.hostname=localhost
#代表不向注册中心注册自己,所以设置为false
eureka.client.register-with-eureka=false
# 由于注册中心的职责是维护服务案例,它并不需要去检索服务,所以设置为false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone = http://${eureka.instance.hostname}:${server.port}/eureka/
4.启动应用eureka-server ,访问:http://localhost:1111,看到如下图,证明已经成功搭建一个服务注册中心.但里面目前没有服务的提供者,接下来就是往注册中心注册.
二.注册服务提供者
1.首先,创建一个基础的Spring Boot项目,pom.xml,文件新增依赖Eureka,如下,跟注册中心不一样的是,注册中心新增的依赖是:Eureka-server,而服务提供者是依赖:eureka
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="[http://maven.apache.org/POM/4.0.0"](http://maven.apache.org/POM/4.0.0%22); xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance"](http://www.w3.org/2001/XMLSchema-instance%22);
xsi:schemaLocation="[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0)[http://maven.apache.org/xsd/maven-4.0.0.xsd"](http://maven.apache.org/xsd/maven-4.0.0.xsd%22);>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring_boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring_boot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR1</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.新建类HelloController,内容如下,注入DiscoveryClient,打印相关的日志内容
@RestController
public class HelloController {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
@RequestMapping(value="/hello",method=RequestMethod.GET)
public String hello() {
ServiceInstance localServiceInstance = client.getLocalServiceInstance();
logger.info("/hello,host:"+localServiceInstance.getHost()+",service_id:"+localServiceInstance.getServiceId());
return "Spring Boot";
}
}
3.在application.java,中新增注解,注册为服务客户端@EnableEurekaClient
@EnableEurekaClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4.修改application.properties 文件.设置服务名称,以及配置注册到哪个服务注册中心
spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone= http://localhost:1111/eureka/
5.启动应用,此时,刷新:http://localhost:1111, 即可看到如下图,则表示注册成功,这个服务已经在服务注册中心了.
6.此时,直接访问:http://localhost:8080/hello,可以看到如下图,同时在控制台中能看到输出: /hello,host:localhost,service_id:hello-service
以上就已经实现单节点模式的服务注册中心,并注册服务提供者
三.高可用注册中心(服务注册中心集群)
1.在eureka-server 中,与application.properties,平级新建文件:application-peer1.properties,application-peer2.properties,文件内容如下:
#application-peer1.properties文件内容
spring.application.name=eureka-server
server.port=1111
eureka.instance.hostname=peer1
spring.profiles=peer1
eureka.client.serviceUrl.defaultZone = http://peer2:1112/eureka/
=========================================
#application-peer2.properties文件内容
spring.application.name=eureka-server
server.port=1112
eureka.instance.hostname=peer2
spring.profiles=peer2
eureka.client.serviceUrl.defaultZone = http://peer1:1111/eureka/
2.在etc/hosts 文件中添加peer1,peer2的转换,让上面配置的host形式的serviceUrl能在本地访问,windows的路径:C:/Windows/System32/dirvers/etc/hosts
127.0.0.1 peer1
127.0.0.1 peer2
3.注意,此时eureka-server中的application.properties 文件中,的
#eureka.client.register-with-eureka=false
#eureka.client.fetch-registry=false
这2个配置需要注释掉,因为高可用注册中心,注册中心之间需要能相互注册.所以这2个配置,不能设置为FALSE,可以选择注释掉,或者设置为true
4.运行应用,这里介绍2种方式,第一种是运行jar包,第二种是使用Eclipse启动-配置参数即可,不详细描述.
4.1jar运行的方式:
4.1.1 进入到项目的位置如图
4.1.2使用shell,输入命令: mvn package,出现下图表示打jar包成功
4.1.3 进入target 目录,运行命令 java -jar .\eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1,此时,可能看起来是报错,但无需理会.
,新开一个shell,运行命令:java -jar .\eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
此时,并不会出现上面的报错信息.
访问:http://localhost:1111,出现如图即表示成功
访问:http://localhost:1112,出现如图即表示成功
以上表示设置好了多节点的服务注册中心,服务提供者hello-service还需要进行简单的配置才能将服务注册到eureka-server 集群中
,修改hello-service的配置文件如下,
spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone= http://peer2:1112/eureka/,http://peer1:1111/eureka/
此时,启动hello-service服务,这个服务就会被同时注册到1111,1112里面,如图显示的是1111的情况.
以上表示,集群已经搭建好了,并向里面注册了服务提供者
四.服务发现与消费
1.使用Ribbon实现简单的负载均衡,首先,先运行eureka-server集群,再通过命令,运行2个不同端口的hello-service
java -jar .\spring_boot-0.0.1-SNAPSHOT.jar --server.port=8081
java -jar .\spring_boot-0.0.1-SNAPSHOT.jar --server.port=8082
2.创建一个Spring Boot的基础工程来实现服务消费者,取名为:ribbon-consumer,pom.xml文件如下.增加了ribbon的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="[http://maven.apache.org/POM/4.0.0"](http://maven.apache.org/POM/4.0.0%22); xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance"](http://www.w3.org/2001/XMLSchema-instance%22);
xsi:schemaLocation="[http://maven.apache.org/POM/4.0.0](http://maven.apache.org/POM/4.0.0)[http://maven.apache.org/xsd/maven-4.0.0.xsd"](http://maven.apache.org/xsd/maven-4.0.0.xsd%22);>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>ribbon-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ribbon-consumer</name>
<description>Demo project for Spring Boot</description>
<!-- 注意,不同版本的springboot,对应不同的spring cloud版本,这里使用的是1.5.10 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR1</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>
3.在RibbonConsumerApplication.java中,注册为client端,创建RestTemplate的SpringBean实例
@EnableEurekaClient
@SpringBootApplication
public class RibbonConsumerApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}
4.创建ConsumerController.java,代码如下:
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value="/ribbon-consumer",method=RequestMethod.GET)
public String ribbon() {
//url-地址名字,是对应上文的hello-service,不能使用ip,这就没有负载均衡的意义
return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
}
}
5.在application.properties 文件中,配置相关信息
spring.application.name=ribbon-consumer
server.port=9000
eureka.client.serviceUrl.defaultZone= http://localhost:1111/eureka/
6.启动ribbon-consumer 应用,会发现服务就会注册到服务注册中心了.
7.访问:http://localhost:9000/ribbon-consumer,观察控制台输出的信息,可以发现,随机的在调用8082跟8081.则表示已经实现了一个基本的负载均衡.