二.搭建SpringCloud(Finchley.M8版本)服务集群(含源码)

本文档介绍了如何在SpringCloud Finchley.M8版本下搭建高可用的注册中心、服务提供者以及使用Ribbon进行负载均衡的客户端服务。详细步骤包括:配置两个相互注册的Eureka服务器,创建高可用的服务提供者,实现服务续约功能,并构建负载均衡的客户端服务。此外,还提供了各阶段的代码仓库链接以供参考。
摘要由CSDN通过智能技术生成
[size=xx-large][b]一、目标[/b][/size]
一个高可用的注册中心+ 一个高可用的服务提供者+一个使用负载均衡访问的客户端服务
[img]http://dl2.iteye.com/upload/attachment/0129/0701/7bfdc381-0dbe-3dc1-a1b8-dcbc8841a173.jpg[/img]
[size=xx-large][b]二、步骤[/b][/size]
以下在<spring-cloud.version>Finchley.M8</spring-cloud.version> 版本下建立
[size=small][b]1.建立一个高可用的注册中心[/b][/size]
POM主要依赖spring-cloud-starter-netflix-eureka-server:
	<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

使得项目工程具备EurekaServer注册中心功能,只需引用:
@EnableEurekaServer
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class SpringcloudApplication {

public static void main(String[] args) {
SpringApplication.run(SpringcloudApplication.class, args);
}
}

配置application文件,使得两个注册中心相互注册对方。
1).创建application-peer1.properties
 server.port = 11111
spring.application.name=eureka-service
eureka.instance.hostname = peer1
eureka.client.register-with-eureka = true
eureka.client.fetch-registry = true
eureka.client.serviceUrl.defaultZone = http://peer2:11112/eureka/

2).创建application-peer2.properties
 server.port = 11112
spring.application.name=eureka-service
eureka.instance.hostname = peer2
eureka.client.register-with-eureka = true
eureka.client.fetch-registry = true
eureka.client.serviceUrl.defaultZone = http://peer1:11111/eureka/

3).在C:\Windows\System32\drivers\etc\hosts下配置
127.0.0.1 peer1
127.0.0.1 peer2

4).分别启动两个注册服务中心
java -jar springcloud-0.0.1-SNAPSHOT.jar --spring.profiles.active = peer1
java -jar springcloud-0.0.1-SNAPSHOT.jar --spring.profiles.active = peer2
注册中心代码地址:https://github.com/dick1305/springcloud.git
[size=small][b]2.一个高可用的服务提供者[/b][/size]
1)POM文件
	<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>


2)使项目具备服务提供者功能,添加@EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class SpringServer1Application {

public static void main(String[] args) {
SpringApplication.run(SpringServer1Application.class, args);
}
}

3)发布helloworld服务
@RestController
public class HelloWorldContrller {
@Autowired
private DiscoveryClient client;
@RequestMapping(value="/hellWorld",method = RequestMethod.GET)
public String hellWorld(String content) {
List<ServiceInstance> instanceLst=client.getInstances("eureka-service");
System.out.println("=====================================");
for( ServiceInstance s:instanceLst) {
System.out.println(s.getPort()+":"+s.getHost());
}
return "helloWold " +content;
}
}

4)配置application.properties
自动关联注册两个服务中心,防止单点故障
server.port = 22223
spring.application.name=eureka-helloWorld
eureka.client.serviceUrl.defaultZone = http://peer1:11111/eureka,http://peer1:11112/eureka

5)启动服务
java -jar spring-server1-0.0.1-SNAPSHOT.jar --server.port = 22223
java -jar spring-server1-0.0.1-SNAPSHOT.jar --server.port = 22222
[url][size=medium][color=red]代码地址:https://github.com/dick1305/eurekaServerExamble.git[/color][/size][/url]
[b][size=small]3.构建一个使用ribbon负载均衡访问的客户端服务[/size][/b]
1)POM
	<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

2)使用@EnableDiscoveryClient 来注册发现服务,使用@LoadBalanced 来注解RestTemplate 提供访问服务提供者
@EnableDiscoveryClient
@SpringBootApplication
public class SpringRibbonConsumerApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}

public static void main(String[] args) {
SpringApplication.run(SpringRibbonConsumerApplication.class, args);
}
}

服务消费者访问服务提供者方法
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value="/consumerHellWorld",method = RequestMethod.GET)
public String consumerHellWorld(String content) {
return restTemplate.getForEntity("http://eureka-helloWorld/hellWorld?content="+content, String.class).getBody();
}
}

4)application.properties
spring.application.name = ribbon-consumer
server.port = 33333
eureka.client.serviceUrl.defaultZone = http://peer1:11111/eureka,http://peer1:11112/eureka

5)源代码:[url][size=x-small][color=red]https://github.com/dick1305/spring-ribbon-consumer-examble.git[/color][/size][/url]
[size=small][b]5.查看服务界面[/b][/size]
[img]http://dl2.iteye.com/upload/attachment/0129/0729/67b9ec97-700b-32e8-92d2-4f67d7a8ebd6.jpg[/img]
[b][size=small]6.访问http://localhost:33333/consumerHellWorld?content=3333 查看消费者请求后的结果。[/size][/b]
[b][size=small]
7.其他[/size][/b]
服务续约:在组册完服务后,服务提供者会维护一个心跳来告诉Eureka Server 用例还存活。可以通过续约配置:
eureka.instance.lease-renewal-interval-in-seconds = 30 //30秒为服务续约时间
eureka.instance.lease-expiration-duraiton-in-seconds = 90 //定义服务失效时间90秒
服务消费者:在服务消费者会维护一份只读的服务清单来返回给客户端,通过Ribbon(轮询)方式调用。获取服务的参数:eureka.client.fetch-registry = true (默认为true)
缓存清单更新时间:eureka.client.registry-fecth-interval-seconds =30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值