spring-clound 学习打卡3:简单入门-服务消费,基于【Feign】 【Greenwich.SR1版本】

spring-clound 学习打卡3:简单入门-服务消费,基于【Feign】 【Greenwich.SR1版本】

一、简介

工程传送门
https://github.com/unnunique/SpringCloundDakaLearning/tree/master/Chapter-3

利用feign 可以访问注册在服务中心的服务,自带负载均衡功能。
Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

简而言之:
Feign 采用的是基于接口的注解
Feign 整合了ribbon,具有负载均衡的能力
整合了Hystrix,具有熔断的能力

二、准备工作

继续用第一篇的工程, 启动server,端口为8761; 启动client 两次,端口分别为8762 、8773.

三、上代码。

3.1 pom 文件


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>parentartifactId>
        <groupId>com.sydney.dream.springcloundgroupId>
        <version>1.0.0version>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>chapter-3artifactId>


    <properties>
        <java.version>1.8java.version>
        <spring-cloud.version>Greenwich.SR1spring-cloud.version>
    properties>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>


project>

3.2 properties

server.port=8765

spring.application.name=service-consumer-client-demo2

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.default-zone=http://${eureka.instance.hostname}:${server.port}/eureka/

remote.eureka.servername=srvice-clientdemo

3.3 application

注意添加@EnableFeignClients

package com.sydney.dream;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class FeignClientDemoApp {
    public static void main(String[] args) {
        SpringApplication.run(FeignClientDemoApp.class, args);
    }
}

3.4 编写消费服务

package com.sydney.dream.chapter3;

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

@FeignClient(value = "${remote.eureka.servername}")
@Component
public interface HelloWorldService {
    @PostMapping(value = "/hi")
    String getHello(@RequestParam("name") String name);
}

3.5 controller

package com.sydney.dream.chapter3;

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

@RestController
public class HelloWorldController {

    @Autowired
    private HelloWorldService helloWorldService;

    @RequestMapping("/hi")
    public String getHelloWorld(String name) {
        return helloWorldService.getHello(name);
    }
}

四、启动服务

启动程序,多次访问http://localhost:8765/hi?name=forezp,浏览器交替显示:

hi didi,i am from port:8762
hi didi,i am from port:8763

你可能感兴趣的:(spring-clound)