springCloud系列第三篇-Feign

同第二篇,新建module service-hello,在主pom文件里面加入module配置

  <modules>
        <module>eureka-server</module>
        <module>service-hi</module>
        <module>service-hello</module>
    </modules>

代码结构:


image.png

1、service-hello pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>

        <groupId>com.cloud</groupId>
        <artifactId>cloud-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cloud</groupId>
    <artifactId>service-hi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-hi</name>

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

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


</project>

2、service-hello 配置文件

spring:
  application:
    name: service-hello
server:
  port: 8763

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

和service-hi相比只是改了下端口,换成yml方式了

3、启动类

package com.cloud.service.hello;

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;

@EnableFeignClients
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceHelloApplication {

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

}

4、controller

package com.cloud.service.hello;

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;

/**
 * @author: zhouwei
 * @version: v1.0
 * @description: com.cloud.service.hello
 * @date:2021-01-25
 */
@RestController
public class HelloController {
    @Autowired
    private UserService userService;

    @RequestMapping("/user/{id}")
    public String getAge(@PathVariable long id) {
        return userService.getUser(id);
    }
}

5、userService代码

package com.cloud.service.hello;

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

/**
 * @author: zhouwei
 * @version: v1.0
 * @description: com.cloud.service.hello
 * @date:2021-01-25
 */
@FeignClient("service-hi") //要调用的服务的名称
public interface UserService {
    @GetMapping("/user/{id}") //mapping地址
     String getUser(@RequestParam long id);
}

访问

image.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容