• springcloud(三):服务提供与调用


     服务提供

     

    1、pom包配置

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

    2、配置文件

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

    3、启动类

    启动类中添加@EnableDiscoveryClient注解

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

    4、controller

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

    服务调用

    1、pom包配置

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

    2、配置文件

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

    3、启动类

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

     

    4、feign调用实现

    Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。

    Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。

    Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

    @FeignClient(name= "spring-cloud-producer")
    public interface HelloRemote {
    
        @RequestMapping(value = "/hello")
        public String hello(@RequestParam(value = "name") String name);
    
    
    }

    5、web层调用远程服务

    @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个,测试结果一样,请求会自动轮询到每个服务端来处理。

  • 相关阅读:
    javascript framework js常用框架
    快速排序Quick sort
    归并排序
    Linux中 设置apache,mysql 开机启动
    Linux下设置mysql和tomcat开机启动
    linux命令之ifconfig详细解释
    CentOS网络接口配置文件ifcfg-eth详解
    条件测试操作与流程控制语句
    从键盘或文件中获取标准输入:read命令
    linux yum命令详解
  • 原文地址:https://www.cnblogs.com/cnki/p/8975983.html
Copyright © 2020-2023  润新知