Spring boot2.0.x+SpringCloud(Finchley版本)分布式架构--服务消费者(Rest+Ribbon负载均衡)(二)

前言声明:
如果您有更好的技术与作者分享,或者商业合作;请访问作者个人网站 http://www.esqabc.com/view/message.html 留言给作者。
如果该案例触犯您的专利,请在这里:http://www.esqabc.com/view/message.html 留言给作者说明原由,作者一经查实,马上删除。

二(1),搭建服务消费者使用(Rest+Ribbon负载均衡)

开始前准备工作:
1,创建两个服务生产者(esq-provider-a和esq-provider-b)
2,创建一个服务消费者(esq-consumer)
架构如下:
在这里插入图片描述

服务生产者一(esq-provider-a) :

1,pom配置文件:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <!-- 父项目 -->
  <parent>
    <groupId>com.esq.master</groupId>
    <artifactId>esq-master</artifactId>
    <version>1.0.0</version>
  </parent>
  <!-- 本项目信息 -->
  <groupId>com.esq.provider</groupId>
  <artifactId>esq-provider-a</artifactId>
  <packaging>pom</packaging>
  <name>esq-provider-a</name>
  <description>服务产生者1</description>
  
   <!-- 配置编码格式  begin -->
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>
  <!-- 配置编码格式  end -->
  
  
  <!-- JAR包配置  begin-->
  <dependencies>
		<!-- 引入关联实体与公共方法-->
		<dependency>
            <groupId>com.esq.common</groupId>
            <artifactId>esq-common</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!-- Cloud 服务需要的jar包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- Springboot web服务器的jar-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
  </dependencies>
  <!-- JAR包配置  end-->
  

  
  <!-- 插件配置  begin -->
   <build>
	<plugins>
	

	</plugins>	
   </build>
  <!-- 插件配置  end -->
</project>

2,application.properties配置文件:

#配置启动端口号
server.port=9011
#配置项目访问路径
#server.context-path=/eureka
#配置服务名称
spring.application.name=esq-service
#配置服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:9000/eureka/

3,创建一个controler(UserControler):

package org.esq.provider.controler;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 用户控制器
 * @author esq
 * @Create 2019-09-09 09:09:09
 * @Website www.esqabc.com
 * @WeChat 
 */
@RestController
public class UserControler {
	
	@Value("${server.port}")
    String port;
	
	@RequestMapping("/esquser")
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "欢迎" + name + "访问,来自服务端口:" + port+"的服务。";
    }
}

4,启动文件(ProviderAApplication):

package org.esq.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.CommandLineRunner;
import java.util.Arrays;
/**
 * 服务产生者1启动类
 * @author esq
 * @Create 2019-09-09 09:09:09
 * @Website www.esqabc.com
 * @WeChat 
 */
@SpringBootApplication
@EnableEurekaClient 
public class ProviderAApplication{

	public static void main(String[] args) {
		System.out.println("。。。。。。服务产生者程序开始启动。。。。。。");
		long startTime = System.currentTimeMillis();    //获取开始时间
		SpringApplication.run(ProviderAApplication.class, args);
		long endTime = System.currentTimeMillis();    //获取结束时间
		System.out.println("服务产生者程序启动用时:" + (endTime - startTime) + "毫秒");//输出程序运行时间
		System.out.println("服务产生者程序启动一共用时:" + (endTime - startTime)/60 + "秒。。。。。。");
		System.out.println("。。。。。。服务产生者程序启动成功。。。。。。");
	}
	@Bean
	public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
		
	   return args -> {
	       String[] beanNames = ctx.getBeanDefinitionNames();
	       Arrays.sort(beanNames);
	       Arrays.stream(beanNames).forEach(System.out::println);
	   };
	}	
	
}

服务生产者二(esq-provider-b) :

1,pom配置文件:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <!-- 父项目 -->
  <parent>
    <groupId>com.esq.master</groupId>
    <artifactId>esq-master</artifactId>
    <version>1.0.0</version>
  </parent>
  <!-- 本项目信息 -->
  <groupId>com.esq.provider</groupId>
  <artifactId>esq-provider-b</artifactId>
  <packaging>pom</packaging>
  <name>esq-provider-b</name>
  <description>服务产生者2</description>
  
   <!-- 配置编码格式  begin -->
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>
  <!-- 配置编码格式  end -->
  
  
  <!-- JAR包配置  begin-->
  <dependencies>
		<!-- 引入关联实体与公共方法-->
		<dependency>
            <groupId>com.esq.common</groupId>
            <artifactId>esq-common</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!-- Cloud 服务需要的jar包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- Springboot web服务器的jar-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
  </dependencies>
  <!-- JAR包配置  end-->
  

  
  <!-- 插件配置  begin -->
   <build>
	<plugins>
	

	</plugins>	
   </build>
  <!-- 插件配置  end -->
</project>

2,application.properties配置文件:

#配置启动端口号
server.port=9022
#配置项目访问路径
#server.context-path=/eureka
#配置服务名称
spring.application.name=esq-service
#配置服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:9000/eureka/

3,创建一个controler(UserControler):

package org.esq.provider.controler;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 用户控制器
 * @author esq
 * @Create 2019-09-09 09:09:09
 * @Website www.esqabc.com
 * @WeChat 
 */
@RestController
public class UserControler {
	
	@Value("${server.port}")
    String port;
	
	@RequestMapping("/esquser")
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "欢迎" + name + "访问,来自服务端口:" + port+"的服务。";
    }
}

4,启动文件(ProviderBApplication):

package org.esq.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.CommandLineRunner;
import java.util.Arrays;
/**
 * 服务产生者2启动类
 * @author esq
 * @Create 2019-09-09 09:09:09
 * @Website www.esqabc.com
 * @WeChat 
 */
@SpringBootApplication
@EnableEurekaClient 
public class ProviderBApplication{

	public static void main(String[] args) {
		System.out.println("。。。。。。服务产生者程序开始启动。。。。。。");
		long startTime = System.currentTimeMillis();    //获取开始时间
		SpringApplication.run(ProviderAApplication.class, args);
		long endTime = System.currentTimeMillis();    //获取结束时间
		System.out.println("服务产生者程序启动用时:" + (endTime - startTime) + "毫秒");//输出程序运行时间
		System.out.println("服务产生者程序启动一共用时:" + (endTime - startTime)/60 + "秒。。。。。。");
		System.out.println("。。。。。。服务产生者程序启动成功。。。。。。");
	}
	@Bean
	public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
		
	   return args -> {
	       String[] beanNames = ctx.getBeanDefinitionNames();
	       Arrays.sort(beanNames);
	       Arrays.stream(beanNames).forEach(System.out::println);
	   };
	}	
	
}

创建一个服务消费者(esq-consumer):
在这里插入图片描述
1,pom配置文件:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <!-- 父项目 -->
  <parent>
    <groupId>com.esq.master</groupId>
    <artifactId>esq-master</artifactId>
    <version>1.0.0</version>
  </parent>
  <!-- 本项目信息 -->
  <groupId>com.esq.consumer</groupId>
  <artifactId>esq-consumer</artifactId>
  <name>esq-consumer</name>
  <url>http://maven.apache.org</url>
  <description>服务消费者</description>
  
   <!-- 配置编码格式  begin -->
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>
  <!-- 配置编码格式  end -->
  
 <!-- JAR包配置  begin-->
  <dependencies>
		<!-- 引入关联实体与公共方法-->
		<dependency>
            <groupId>com.esq.common</groupId>
            <artifactId>esq-common</artifactId>
            <version>1.0.0</version>
        </dependency>
         <!-- Cloud 服务需要的jar包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- Springboot web服务器的jar-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 负载均衡客户端 的jar -->
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
  </dependencies>
  <!-- JAR包配置  end-->
  

  
  <!-- 插件配置  begin -->
   <build>
	<plugins>
	

	</plugins>	
   </build>
  <!-- 插件配置  end -->
</project>

2,application.properties配置文件:


# 配置启动端口号
server.port=9002
# 配置项目访问路径
#server.context-path=/eureka
#配置服务名称
spring.application.name=esq-consumer
#服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:9000/eureka/

3,创建一个controler(UserControler):

package com.esq.consumer.controler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.esq.consumer.service.UserService;

/**
 * 用户控制器
 * @author esq
 * @Create 2019-09-09 09:09:09
 * @Website www.esqabc.com
 * @WeChat 
 */
@RestController
public class UserControler {
	
	@Autowired
	UserService userService;
	
	 @GetMapping(value = "/esquser")
	 public String hi(@RequestParam String name) {
		 System.out.println(name);
		 System.out.println("进入消费者==》》用户控制器==》》调用esquser服务");
	     return userService.getServiceInfo(name);
	}
}
package com.esq.consumer.service;


/**
 * 用户接口
 * @author esq
 * @Create 2019-09-09 09:09:09
 * @Website www.esqabc.com
 * @WeChat 
 */
public interface UserService {
	
	 public String getServiceInfo(String name);
}

package com.esq.consumer.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.esq.consumer.service.UserService;

/**
 * 用户接口实现
 * @author esq
 * @Create 2019-09-09 09:09:09
 * @Website www.esqabc.com
 * @WeChat 
 */
@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
    RestTemplate restTemplate;
	
	public String getServiceInfo(String name) {
		return restTemplate.getForObject("http://ESQ-SERVICE/esquser?name="+name,String.class);
	}
}

4,启动文件(ConsumerApplication):

package com.esq.consumer;

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.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.boot.CommandLineRunner;
import java.util.Arrays;
/**
 * 服务消费者启动类
 * @author esq
 * @Create 2019-09-09 09:09:09
 * @Website www.esqabc.com
 * @WeChat 
 */
@SpringBootApplication
@EnableEurekaClient 
@EnableDiscoveryClient
/**
 * 通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean:restTemplate;
 * 并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
 */
public class ConsumerApplication{

	public static void main(String[] args) {
		System.out.println("。。。。。。服务消费者程序开始启动。。。。。。");
		long startTime = System.currentTimeMillis();    //获取开始时间
		SpringApplication.run(ConsumerApplication.class, args);
		long endTime = System.currentTimeMillis();    //获取结束时间
		System.out.println("服务消费者程序启动用时:" + (endTime - startTime) + "毫秒");//输出程序运行时间
		System.out.println("服务消费者程序启动一共用时:" + (endTime - startTime)/60 + "秒。。。。。。");
		System.out.println("。。。。。。服务消费者程序启动成功。。。。。。");
	}
	@Bean
	public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
	   return args -> {
	       String[] beanNames = ctx.getBeanDefinitionNames();
	       Arrays.sort(beanNames);
	       Arrays.stream(beanNames).forEach(System.out::println);
	   };
	}
	
	@Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

注意:项目启动顺序为:先启动注册中心(esq-registry) ,然后启动(服务生产者1,2)(esq-provider-a,esq-provider-b) 最后启动服务消费者(esq-consumer):
在这里插入图片描述

在浏览器输入:http://localhost:9000/ 可以看到同一个服务有两个端口 9011 9022:

在这里插入图片描述

在浏览器输入:http://localhost:9002/esquser?name=12312 会交替显示:

欢迎12312访问,来自服务端口:9011的服务。
欢迎12312访问,来自服务端口:9022的服务。

总结:
一个服务注册中心(esq-registry)端口为9000
跑了两个实例,端口分别为9011,9022,分别向服务注册中心,注册同一个服务名称端口不同的服务
消费者(esq-consumer)端口为9002,向服务注册中心注册
当esq-consumer通过restTemplate调用实例的esquser接口时,因为用Ribbon进行了负载均衡,会轮流的调用实例:9011和9022 两个端口的esquser接口;

===============================

更多请访问:https://blog.csdn.net/esqabc/article/details/87804603

=================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值