springcloud Spring Boot mybatis分布式微服务云架构(三):服务提供与调用

上一篇文章我们介绍了eureka服务注册中心的搭建,这篇文章介绍一下如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用的案例。

案例中有三个角色:服务注册中心、服务提供者、服务消费者,其中服务注册中心就是我们上一篇的eureka单机版启动既可,流程是首先启动注册中心,服务提供者生产服务并注册到服务中心中,消费者从服务中心中获取服务并执行。

服务提供

我们假设服务提供者有一个hello方法,可以根据传入的参数,提供输出“hello xxx,this is first messge”的服务

1、pom包配置

创建一个springboot项目,pom.xml中添加如下配置:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

2、配置文件

application.properties配置如下:

spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

参数在上一篇都已经解释过,这里不多说。

3、启动类

启动类中添加@EnableDiscoveryClient注解

@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {

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

4、controller

提供hello服务

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+",this is first messge";
    }
}

添加@EnableDiscoveryClient注解后,项目就具有了服务注册的功能。启动工程后,就可以在注册中心的页面看到SPRING-CLOUD-PRODUCER服务。

到此服务提供者配置就完成了。

服务调用

1、pom包配置

和服务提供者一致

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

2、配置文件

application.properties配置如下:

spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3、启动类

启动类添加@EnableDiscoveryClient@EnableFeignClients注解。

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {

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

}
  • @EnableDiscoveryClient :启用服务注册与发现
  • @EnableFeignClients:启用feign进行远程调用

Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

4、feign调用实现

@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}
  • name:远程服务名,及spring.application.name配置的名称

此类中的方法和远程服务中contoller中的方法名和参数需保持一致。

5、web层调用远程服务

将HelloRemote注入到controller层,像普通方法一样去调用即可。

@RestController
public class ConsumerController {

    @Autowired
    HelloRemote HelloRemote;

    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return HelloRemote.hello(name);
    }

}

到此,最简单的一个服务注册与调用的例子就完成了。

测试

简单调用

依次启动spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三个项目

先输入:http://localhost:9000/hello?name=neo 检查spring-cloud-producer服务是否正常

返回:hello neo,this is first messge

说明spring-cloud-producer正常启动,提供的服务也正常。

浏览器中输入:http://localhost:9001/hello/neo

返回:hello neo,this is first messge

说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。

负载均衡

以上面spring-cloud-producer为例子修改,将其中的controller改动如下:

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+",this is producer 2  send first messge";
    }
}

在配置文件中改动端口:

spring.application.name=spring-cloud-producer
server.port=9003
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

打包启动后,在eureka就会发现两个服务提供者,如下图:

然后在浏览器再次输入:http://localhost:9001/hello/neo 进行测试:

第一次返回结果:hello neo,this is first messge

第二次返回结果:hello neo,this is producer 2 send first messge

不断的进行测试下去会发现两种结果交替出现,说明两个服务中心自动提供了服务均衡负载的功能。如果我们将服务提供者的数量在提高为N个,测试结果一样,请求会自动轮询到每个服务端来处理。完整项目的源码来源 技术支持2147775633

原文地址:https://www.cnblogs.com/leafitit/p/9583515.html

时间: 2024-08-03 08:00:54

springcloud Spring Boot mybatis分布式微服务云架构(三):服务提供与调用的相关文章

springCloud Spring Boot mybatis分布式微服务云架构-docker-hystrix-dashboard-turbine(九)

简介 Hystrix的主要优点之一是它收集关于每个HystrixCommand的一套指标.Hystrix仪表板以有效的方式显示每个断路器的运行状况,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的断路器是否打开,请求响应时间, 请求失败率,请求超时个数等等数据.但是只使用Hystrix Dashboard的话, 你只能看到单个应用内的服务信息, 这明显不够. 我们需要一个工具能让我们汇总系统内多个服务的数据并显示到Hystrix Dashboard上,

springCloud Spring Boot mybatis分布式微服务云架构-docker-feign-hystrix-ribbon(七)

简介 在上一节中,我们讨论了feign+hystrix在项目开发中,除了考虑正常的调用之外,负载均衡和故障转移也是关注的重点,这也是feign + ribbon+hystrix的优势所在,本节我们就讨论一下在feign中使用ribbon,有两种方式 一.通过在配置文件application.yml配置,开启ribbon,并指定调用生产者相对上一节可以不做任何更改,可以看项目(microservice-consumer-movie-feign-with-hystrix-hystrix-factor

Spring Cloud Spring Boot mybatis分布式微服务云架构(二)

通过命令行设置属性值 相信使用过一段时间Spring Boot的用户,一定知道这条命令:java -jar xxx.jar --server.port=8888,通过使用–server.port属性来设置xxx.jar应用的端口为8888. 在命令行运行时,连续的两个减号--就是对application.properties中的属性值进行赋值的标识.所以,java -jar xxx.jar --server.port=8888命令,等价于我们在application.properties中添加属

Spring Cloud Spring Boot mybatis分布式微服务云架构(三)属性配置文件详解(1)

相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了原来自己整合Spring应用时繁多的XML配置内容,替代它的是在pom.xml中引入模块化的Starter POMs,其中各个模块都有自己的默认配置,所以如果不是特殊应用场景,就只需要在application.properties中完成一些属性配置就能开启各模块的应用. 在之前的各篇文章中都有提及关于application.pro

Spring Cloud Spring Boot mybatis分布式微服务云架构(一)快速入门

快速入门 本章主要目标完成Spring Boot基础项目的构建,并且实现一个简单的Http请求处理,通过这个例子对Spring Boot有一个初步的了解,并体验其结构简单.开发快速的特性. 系统要求: Java 7及以上Spring Framework 4.1.5及以上本文采用Java 1.8.0_73.Spring Boot 1.3.2调试通过. 使用Maven构建项目 通过SPRING INITIALIZR工具产生基础项目访问:http://start.spring.io/选择构建工具Mav

Spring Cloud Spring Boot mybatis分布式微服务云架构 开发Web应用

静态资源访问在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /static/public/resources/META-INF/resources举例:我们可以在src/main/resources/目录下创建static,在该位置放置一个图片文件.启动程序后,尝试访问http://localhost:8080/D.jpg.如能显示图片,配置成功. 渲染Web页面在

Spring Cloud Spring Boot mybatis分布式微服务云架构(十三)

准备工作 首先,构建一个简单的Web工程,以用于后续添加安全控制,也可以用之前Chapter3-1-2做为基础工程.若对如何使用Spring Boot构建Web应用,可以先阅读<Spring Boot开发Web应用>一文. Web层实现请求映射 [java] view plain copy@Controller public class HelloController { @RequestMapping("/") public String index() { return

Spring Cloud Spring Boot mybatis分布式微服务云架构(十三)使用Spring Security安全控制

准备工作 首先,构建一个简单的Web工程,以用于后续添加安全控制,若对如何使用Spring Boot构建Web应用,可以先阅读<Spring Boot开发Web应用>一文. Web层实现请求映射 @Controller public class HelloController { @RequestMapping("/") public String index() { return "index"; } @RequestMapping("/he

Spring Cloud Spring Boot mybatis分布式微服务云架构(二)使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程

在之前的所有Spring Boot和Spring Cloud相关博文中,都会涉及Spring Boot工程的创建.而创建的方式多种多样,我们可以通过Maven来手工构建或是通过脚手架等方式快速搭建,也可以通过<Springboot快速入门>一文中提到的SPRING INITIALIZR页面工具来创建,相信每位读者都有自己最喜欢和最为熟练的创建方式. 本文我们将介绍嵌入的Intellij中的Spring Initializr工具,它同Web提供的创建功能一样,可以帮助我们快速的构建出一个基础的S