一个高可用的注册中心+ 一个高可用的服务提供者+一个使用负载均衡访问的客户端服务
二、步骤
以下在<spring-cloud.version>Finchley.M8</spring-cloud.version> 版本下建立
1.建立一个高可用的注册中心
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
2.一个高可用的服务提供者
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
代码地址:https://github.com/dick1305/eurekaServerExamble.git
3.构建一个使用ribbon负载均衡访问的客户端服务
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)源代码: https://github.com/dick1305/spring-ribbon-consumer-examble.git
5.查看服务界面
6.访问http://localhost:33333/consumerHellWorld?content=3333 查看消费者请求后的结果。
7.其他
服务续约:在组册完服务后,服务提供者会维护一个心跳来告诉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