栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring cloud--声明式调用feign

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring cloud--声明式调用feign

第一个feign程序

1.搭建Eureka server(之前的文章里有)

2.创建服务提供者(之前的文章里有)

在服务提供者中eureka-provider中创建controller包,controller包下定义controller类(eureka-provider->controller包->controller类)

package com.example.eurekaprovider.controller;

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

@RestController

public class HelloController {
	
	@RequestMapping(value = "/hello",method = RequestMethod.GET)
	public String sayHello() {
		return "hello Feign";
	}
}

3.创建feign

第一步:使用IDEA创建一个名称为eureka-feign-client的Spring Boot项目,添加eureka Client、web和feign依赖

第二步:添加配置信息
在全局配置文件application.yml中添加feign的配置信息

spring:
  application:
    name: eureka-feign-client
server:
  port: 8764
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/

第三步:添加注解
在项目启动类EurekaFeginClientApplication上添加@EnableEurekaClient注解,开启EurekaClient功能,添加EurekaFeignClient注解,开启FeignClient功能

package com.itheima.eurekafeignclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

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

第四步:创建service包和和controller包

1.在eureka-feign-client中创建service包,并创建接口FeignService(eureka-feign-client->service包->FeignService接口)

package com.itheima.eurekafeignclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

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

2.在eureka-feign-client中创建controller包,并创建FeignController类,并定义hello()方法调用FeignService的sayHello()方法(eureka-feign-client->controller包->FeignController类->hello())

package com.itheima.eurekafeignclient.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.itheima.eurekafeignclient.service.FeignService;

@RestController
public class FeignController {
	@Autowired
	FeignService feignService;
		@RequestMapping(value = "/hello",method = RequestMethod.GET)
		public String hello() {
			return feignService.sayHello();
		}
}

测试结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/981222.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号