Springcloud-feign调用

2020-12-07 14:17:31 浏览数 (1)

Eureka

配置文件

代码语言:javascript复制
###服务启动端口号
server:
  port: 8100
###服务名称(服务注册到eureka名称)
spring:
  application:
    name: app-itmayiedu-server
###服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka


    ###因为该应用为注册中心,不会注册自己
    register-with-eureka: true
    ###是否需要从eureka上获取注册信息
    fetch-registry: true

启动类

代码语言:javascript复制
@EnableEurekaServer
@SpringBootApplication
public class springcloudEureka {
    public static void main(String[] args) {
        SpringApplication.run(springcloudEureka.class,args);
    }
}

订单

配置文件

代码语言:javascript复制
###服务启动端口号
server:
  port: 8001
###服务名称(服务注册到eureka名称)
spring:
  application:
    name: app-itmayiedu-order
###服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka


    ###因为该应用为注册中心,不会注册自己
    register-with-eureka: true
    ###是否需要从eureka上获取注册信息
    fetch-registry: true

启动类

代码语言:javascript复制
@SpringBootApplication//启动注解
@EnableEurekaClient//将服务注册到注册中心
@EnableFeignClients//Feign注解
public class start{
    public static void main(String[] args) {
        SpringApplication.run(controller.class,args);
    }

feign接口

代码语言:javascript复制
@FeignClient(value = "app-itmayiedu-member")
public interface MemberController {

    @RequestMapping(value="/getMember",method= RequestMethod.POST)
    String getMember(@RequestParam("name")  String name);

会员

配置文件

代码语言:javascript复制
###服务启动端口号
server:
  port: 8080
###服务名称(服务注册到eureka名称)
spring:
  application:
    name: app-itmayiedu-member
###服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka


    ###因为该应用为注册中心,不会注册自己
    register-with-eureka: true
    ###是否需要从eureka上获取注册信息
    fetch-registry: true

启动类

代码语言:javascript复制
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class AppMember {
    public static void main(String[] args) {
        SpringApplication.run(AppMember.class, args);
    }
}

调用方法

代码语言:javascript复制
@RestController
public class MemberController {

    @Value("${server.port}")
    private String serverPort;
    @RequestMapping(value="/getMember",method= RequestMethod.POST)
    public String getMember(@RequestParam("name") String name) {
        return "this is member,我是会员服务!端口号:"   serverPort "名字" name;
    }
}

0 人点赞