博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringCloud第二弹(高可用Eureka+Ribbon负载均衡)
阅读量:6518 次
发布时间:2019-06-24

本文共 8237 字,大约阅读时间需要 27 分钟。

先建立父工程

..

..一路next

搭建注册中心(需要建立三个工程,端口不一样)

..

..

..

修改入口类

package com.cloud.eurekaserver1111;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublic class EurekaServer1111Application {    public static void main(String[] args) {        SpringApplication.run(EurekaServer1111Application.class, args);    }}

 

修改属性文件(一共建立三个Eureka-Server服务,端口分别为1111,2222,3333)

server.port=1111eureka.instance.hostname=server.one.comeureka.client.fetch-registry=falseeureka.client.register-with-eureka=falseeureka.client.service-url.defaultZone=http://server.two.com:2222/eureka,http://server.three.com:3333/eureka

 ..

server.port=2222eureka.instance.hostname=server.two.comeureka.client.fetch-registry=falseeureka.client.register-with-eureka=falseeureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.three.com:3333/eureka

 

..

server.port=3333eureka.instance.hostname=server.three.comeureka.client.fetch-registry=falseeureka.client.register-with-eureka=falseeureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka

 

..

 

 

修改hosts文件(C:\Windows\System32\drivers\etc)

127.0.0.1 server.one.com127.0.0.1 server.two.com127.0.0.1 server.three.com

 

修改pom文件,把parent改成父工程

com.cloud
cloud-parent-two
0.0.1-SNAPSHOT

 建立服务提供者-8081

 ..

 ..

 ..

修改pom

com.cloud
cloud-parent-two
0.0.1-SNAPSHOT

 

修改入口类:

package com.cloud.bookservice8081;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClientpublic class BookService8081Application {    public static void main(String[] args) {        SpringApplication.run(BookService8081Application.class, args);    }}

 

修改属性文件

server.port=8081# 服务名spring.application.name=BookService# 注册地址eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka# 注册名eureka.instance.instance-id=book-service:8081eureka.instance.prefer-ip-address=true

 

 新建controller

package com.cloud.bookservice8081.controller;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@Slf4j@RestControllerpublic class BookController {    @Autowired    private DiscoveryClient discoveryClient;    @RequestMapping("/book")    public String index(){        List
services = discoveryClient.getServices(); services.forEach(e -> log.info("book-service:8081:" + e)); List
list = discoveryClient.getInstances("BOOKSERVICEPROVIDER"); list.forEach(e -> { log.info("book-service:8081:" + e.getServiceId() + "," + e.getHost() + "," + e.getPort() + "," + e.getUri()); }); return "{\n" + " \"bookName\": \"Apache Kafka实战\",\n" + " \"bookSize\": \"16开\",\n" + " \"pack\": \"平装\",\n" + " \"isbn\": \"9787121337765\",\n" + " \"publisher\": \"电子工业出版社\",\n" + " \"publishTime\": \"2018-05-01\",\n" + " \"service\": \"book-service:8081\"\n" + "}"; }}

 

建立服务提供者-8082

..

..

修改pom

com.cloud
cloud-parent-two
0.0.1-SNAPSHOT

 

修改入口类

package com.cloud.bookservice8082;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClientpublic class BookService8082Application {    public static void main(String[] args) {        SpringApplication.run(BookService8082Application.class, args);    }}

 

修改属性文件

server.port=8082# 服务名spring.application.name=BookService# 注册地址eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka# 注册名eureka.instance.instance-id=book-service:8082eureka.instance.prefer-ip-address=true

 

增加controller

package com.cloud.bookservice8082.controller;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@Slf4j@RestControllerpublic class BookController {    @Autowired    private DiscoveryClient discoveryClient;    @RequestMapping("/book")    public String index(){        List
services = discoveryClient.getServices(); services.forEach(e -> log.info("book-service:8082:" + e)); List
list = discoveryClient.getInstances("BOOKSERVICEPROVIDER"); list.forEach(e -> { log.info("book-service:8082:" + e.getServiceId() + "," + e.getHost() + "," + e.getPort() + "," + e.getUri()); }); return "{\n" + " \"bookName\": \"Apache Kafka实战\",\n" + " \"bookSize\": \"16开\",\n" + " \"pack\": \"平装\",\n" + " \"isbn\": \"9787121337765\",\n" + " \"publisher\": \"电子工业出版社\",\n" + " \"publishTime\": \"2018-05-01\",\n" + " \"service\": \"book-service:8082\"\n" + "}"; }}

 

最后建立消费者-8080

..

..

修改pom

com.cloud
cloud-parent-two
0.0.1-SNAPSHOT

 

修改入口类(配置了@LoadBalanced注解的Bean

package com.cloud.bookconsumer8080;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplication@EnableDiscoveryClient@EnableEurekaClientpublic class BookConsumer8080Application {    @Bean    @LoadBalanced    public RestTemplate restTemplate(){        return new RestTemplate();    }    public static void main(String[] args) {        SpringApplication.run(BookConsumer8080Application.class, args);    }}

 

属性文件

server.port=8080eureka.client.register-with-eureka=falseeureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka

 

controller

package com.cloud.bookconsumer8080.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class BookController {    private static final String PREFIX = "http://BOOKSERVICE";  // 微服务名字    @Autowired    private RestTemplate restTemplate;    @RequestMapping("consumeBook")    public String index(){        return restTemplate.getForEntity(PREFIX + "/book",String.class).getBody();    }}

 

与上一节不同,这次指向的是服务名

目录结构

 

 下面开始运行

访问  出现

可以看见当前Eureka连接另外两个Eureka,证明注册中心高可用集群搭建成功。

再看下面的服务,有两个,证明服务已经注册进来了

下面访问消费者  

刷新

证明负载均衡也成功了

 

 

 

转载于:https://www.cnblogs.com/LUA123/p/9365725.html

你可能感兴趣的文章
windows2003单域迁移到2008R2服务器
查看>>
cacti相关资料网站
查看>>
我的友情链接
查看>>
浅析:Android--Fragment的懒加载
查看>>
Linux操作系统目录和Linux常用的文件和目录管理命令
查看>>
DIY:自己动手做一个迷你 Linux 系统(二)
查看>>
猫猫学IOS(三十)UI之Quartz2D画图片画文字
查看>>
ethereumjs/merkle-patricia-tree-2-API
查看>>
go标准库的学习-runtime
查看>>
pytorch Debug —交互式调试工具Pdb (ipdb是增强版的pdb)-1-使用说明
查看>>
NodeJS学习之文件操作
查看>>
AJAX的get和post请求原生编写方法
查看>>
WebSocket 是什么原理?为什么可以实现持久连接
查看>>
Python自学笔记-logging模块详解
查看>>
Head First--设计模式
查看>>
iOS之CAGradientLayer属性简介和使用
查看>>
微信小程序UI组件、开发框架、实用库
查看>>
模块化Javascript代码的两种方式
查看>>
Money去哪了- 每日站立会议
查看>>
Python数据结构和算法学习笔记1
查看>>