玩转springcloud(三):服务的提供者与调用者(注册于发现)

时间:2019-11-23
本文章向大家介绍玩转springcloud(三):服务的提供者与调用者(注册于发现),主要包括玩转springcloud(三):服务的提供者与调用者(注册于发现)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、简介


 

上文我们实践了cloud的注册中心的单服务于多节点的搭建,房子造好了得有人来住不是,这篇我们实践下服务提供者于调用者的案例,也就是服务端和客户端的调用。

本文会设计三个module:注册中心(eureka),服务提供方(server),服务调用方(client)。其中注册中心,我们就用上文搞的,不能重复造轮子啊~~

简单说下项目执行及调用过程:首先启动注册中心,然后在启动服务端和客户端,服务端会注册到注册中心,

二、实践演练


 服务提供方

我们在服务端创建一个接口可接收字符串参数,然后拼接这个参数输出("hello " + 参数 + ",this is first messge,我是8079服务哦")

1、老生常谈,先上pom依赖  

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

 2、配置文件走起

spring:
  application:
    name: spring-cloud-eureka-producer
server:
  port: 8079
eureka:
  client:
    serviceUrl:
      defaultZone: http://xjy1:8097/eureka/

 参数之前都结实过啦,这里不再赘述

 3、启动类添加注解@EnableDiscoveryClient

package com.oldmonk.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @program: cloud
 * @description: 启动类
 * @author: xujingyang
 * @create: 2019-10-09 12:06
 **/
@SpringBootApplication
@EnableDiscoveryClient
public class ErkaProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ErkaProducerApplication.class);
    }
}

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

4、提供的接口服务

package com.oldmonk.cloud.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: cloud
 * @description: 测试接口
 * @author: xujingyang
 * @create: 2019-10-08 14:45
 **/
@RestController
public class Hello {


    @RequestMapping("/hello")
    public String index(String name) {
        return "hello " + name + ",this is first messge,我是8079服务哦";
    }

}

5、启动注册中心,然后启动服务提供方,得下图

至此服务提供方就配置完成了!

 服务调用方

1、老生常谈,先上pom依赖 ,与提供方是一样的

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

 2、配置文件走起 

spring:
  application:
    name: spring-cloud-eureka-consumer
server:
 port: 9009
eureka:
  client:
    serviceUrl:
      defaultZone: http://xjy1:8097/eureka/

3、启动类添加注解

package com.oldmonk.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @program: cloud
 * @description: 启动类
 * @author: xujingyang
 * @create: 2019-10-09 12:06
 **/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ErkaConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ErkaConsumerApplication.class);
    }
}
  • @EnableDiscoveryClient :启用服务注册与发现
  • @EnableFeignClients:启用feign进行远程调用

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

4、客户端接口

package com.oldmonk.cloud.controller;

import com.oldmonk.cloud.service.remote.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: cloud
 * @description: 测试接口
 * @author: xujingyang
 * @create: 2019-10-08 14:45
 **/
@RestController
public class HelloController {

    @Autowired
    HelloRemote remote;

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

}

5、feign调用

package com.oldmonk.cloud.service.remote;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @program: cloud-lx
 * @description: 远程服务调用
 * @author: xujingyang
 * @create: 2019-10-09 13:07
 **/
@FeignClient(name = "spring-cloud-eureka-producer")
public interface HelloRemote {

    @RequestMapping("/hello")
    String hello(@RequestParam("name") String name);
}

参数必须保持一致与服务提供方的接口

6、启动

至此,服务调用方配置完成

 测试

简单调用

1、先输入:http://localhost:8079/hello?name=xjy检查服务提供方服务是否正常

 说服务提供方正常启动,提供的服务也正常。

2、浏览器中输入:http://localhost:9009/hi/xjy

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

负载均衡

待续....

原文地址:https://www.cnblogs.com/xujingyang/p/11919129.html