spring cloud Eureka & Ribbon --服务治理及负载均衡实现demo(单节点模式和高可用模式)

一.搭建服务注册中心
1.首先,创建一个基础的Spring Boot 项目,命名为eureka-server,pom.xml文件添加完对应的依赖后,如下:

 

     4.0.0
     com.example
     eureka-server
     0.0.1-SNAPSHOT
     jar
     eureka-server
     Demo project for Spring Boot
     
     
           org.springframework.boot
           spring-boot-starter-parent
           1.5.10.RELEASE
            
     
     
          UTF-8
          UTF-8
           1.8
     
     
           
                org.springframework.boot
                spring-boot-starter-web
           
           
                org.springframework.boot
                spring-boot-starter-test
                test
           
           
           
                org.springframework.cloud
                spring-cloud-starter-eureka-server
           
     
     
           
                
                     org.springframework.cloud
                     spring-cloud-dependencies
                     Edgware.SR1
                     pom
                     import
                
           
     
     
           
                
                     org.springframework.boot
                     spring-boot-maven-plugin
                
           
     

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,看到如下图,证明已经成功搭建一个服务注册中心.但里面目前没有服务的提供者,接下来就是往注册中心注册.

spring cloud Eureka & Ribbon --服务治理及负载均衡实现demo(单节点模式和高可用模式)_第1张图片
image.png

二.注册服务提供者
1.首先,创建一个基础的Spring Boot项目,pom.xml,文件新增依赖Eureka,如下,跟注册中心不一样的是,注册中心新增的依赖是:Eureka-server,而服务提供者是依赖:eureka

 

     4.0.0
     com.example
     spring_boot
     0.0.1-SNAPSHOT
     jar
     spring_boot
     Demo project for Spring Boot
     
           org.springframework.boot
           spring-boot-starter-parent
           1.5.10.RELEASE
            
     
     
          UTF-8
          UTF-8
           1.8
     
     
           
                org.springframework.boot
                spring-boot-starter-web
           
           
                org.springframework.boot
                spring-boot-starter-test
                test
           
           
                org.springframework.cloud
                spring-cloud-starter-eureka
           
     
     
           
                
                     org.springframework.cloud
                     spring-cloud-dependencies
                     Edgware.SR1
                     pom
                     import
                
           
     
     
           
                
                     org.springframework.boot
                     spring-boot-maven-plugin
                
           
     

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, 即可看到如下图,则表示注册成功,这个服务已经在服务注册中心了.

spring cloud Eureka & Ribbon --服务治理及负载均衡实现demo(单节点模式和高可用模式)_第2张图片
image.png

6.此时,直接访问: http://localhost:8080/hello,可以看到如下图,同时在控制台中能看到输出: /hello,host:localhost,service_id:hello-service
spring cloud Eureka & Ribbon --服务治理及负载均衡实现demo(单节点模式和高可用模式)_第3张图片
image.png

以上就已经实现单节点模式的服务注册中心,并注册服务提供者
三.高可用注册中心(服务注册中心集群)
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 进入到项目的位置如图

spring cloud Eureka & Ribbon --服务治理及负载均衡实现demo(单节点模式和高可用模式)_第4张图片
image.png

4.1.2使用shell,输入命令: mvn package,出现下图表示打jar包成功
spring cloud Eureka & Ribbon --服务治理及负载均衡实现demo(单节点模式和高可用模式)_第5张图片
image.png

4.1.3 进入target 目录,运行命令 java -jar .\eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1,此时,可能看起来是报错,但无需理会.
spring cloud Eureka & Ribbon --服务治理及负载均衡实现demo(单节点模式和高可用模式)_第6张图片
image.png

,新开一个shell,运行命令:java -jar .\eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
此时,并不会出现上面的报错信息.
访问: http://localhost:1111,出现如图即表示成功
spring cloud Eureka & Ribbon --服务治理及负载均衡实现demo(单节点模式和高可用模式)_第7张图片
image.png

访问: http://localhost:1112,出现如图即表示成功
spring cloud Eureka & Ribbon --服务治理及负载均衡实现demo(单节点模式和高可用模式)_第8张图片
image.png

以上表示设置好了多节点的服务注册中心,服务提供者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的情况.


spring cloud Eureka & Ribbon --服务治理及负载均衡实现demo(单节点模式和高可用模式)_第9张图片
image.png

以上表示,集群已经搭建好了,并向里面注册了服务提供者
四.服务发现与消费
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


image.png

2.创建一个Spring Boot的基础工程来实现服务消费者,取名为:ribbon-consumer,pom.xml文件如下.增加了ribbon的依赖
 

     4.0.0
     com.example
     ribbon-consumer
     0.0.1-SNAPSHOT
     jar
     ribbon-consumer
     Demo project for Spring Boot
     
     
           org.springframework.boot
           spring-boot-starter-parent
           1.5.10.RELEASE
            
     
     
          UTF-8
          UTF-8
           1.8
     
     
           
                org.springframework.boot
                spring-boot-starter-web
           
           
                org.springframework.boot
                spring-boot-starter-test
                test
           
           
           
                org.springframework.cloud
                spring-cloud-starter-eureka
           
           
                org.springframework.cloud
                spring-cloud-starter-ribbon
           
     

           
                
                     org.springframework.cloud
                     spring-cloud-dependencies
                     Edgware.SR1
                     pom
                     import
                
           
     
     
           
                
                     org.springframework.boot
                     spring-boot-maven-plugin
                
           
     

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 应用,会发现服务就会注册到服务注册中心了.

image.png

7.访问: http://localhost:9000/ribbon-consumer,观察控制台输出的信息,可以发现,随机的在调用8082跟8081.则表示已经实现了一个基本的负载均衡.

你可能感兴趣的:(spring cloud Eureka & Ribbon --服务治理及负载均衡实现demo(单节点模式和高可用模式))