用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

it绿萝    -  云代码空间

——

java B2B2C Springboot电子商务平台源码-Feign 基本使用

2019-02-15|306阅||

摘要:1、 【microcloud-consumer-feign】为了可以使用到 feign 支持,需要修改 pom.xml 配置文件,引入相关依赖包:需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码 817983997          

1、 【microcloud-consumer-feign】为了可以使用到 feign 支持,需要修改 pom.xml 配置文件,引入相关依赖包:需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码 yuncode.net
 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
 
feign 包含了 Ribbon 支持,所以导入了以上的依赖包之后就表示项目之中已经存在有了 ribbon 相关支持库。

2、 【microcloud-service】建立一个新的模块,这个模块专门负责客户端接口的定义;

3、 【microcloud-service】修改 pom.xml 配置文件,引用“microcloud-api”模块,这样就可以使用到 VO 类了;
 
        <dependency>
            <groupId>cn.study</groupId>
            <artifactId>microcloud-api</artifactId>
        </dependency>
 
       
4、 【microcloud-service】此时如果要通过 Feign 进行远程 Rest 调用,那么必须要考虑服务的认证问题。

此时可以删除原始的 RestConfig 进行的配置处理,然后添加feign的认证配置类

 
package cn.study.commons.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import feign.auth.BasicAuthRequestInterceptor;
@Configuration
public class FeignClientConfig {
    @Bean
    public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("studyjava", "hello");
    }
}
 
5、 【microcloud-service】建立一个 IDeptClientService 接口;

 
package cn.study.service;

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.study.commons.config.FeignClientConfig;
import cn.study.vo.Dept;
/**
 * 通过注解@FeignClient添加接口对应的远程微服务名称value="MICROCLOUD-PROVIDER-DEPT"和
 * 服务的认证configuration=FeignClientConfig.class
 *
 */
@FeignClient(value="MICROCLOUD-PROVIDER-DEPT",configuration=FeignClientConfig.class)
public interface IDeptClientService {
    @RequestMapping(method=RequestMethod.GET,value="/dept/get/{id}")
    public Dept get(@PathVariable("id") long id) ;
    @RequestMapping(method=RequestMethod.GET,value="/dept/list")
    public List<Dept> list() ;
    @RequestMapping(method=RequestMethod.POST,value="/dept/add")
    public boolean add(Dept dept) ;
}
 
6、 【microcloud-consumer-feign】修改 pom.xml 配置文件,引入 microcloud-service 开发包:
 
        <dependency>
            <groupId>cn.study</groupId>
            <artifactId>microcloud-service</artifactId>
        </dependency>
 
7、 【microcloud-consumer-feign】修改 ConsumerDeptController 控制器程序类;

 
package cn.study.microcloud.controller;

import javax.annotation.Resource;

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

import cn.study.service.IDeptClientService;
import cn.study.vo.Dept;

@RestController
public class ConsumerDeptController {
    @Resource
    private IDeptClientService deptService ;
    @RequestMapping(value = "/consumer/dept/get")
    public Object getDept(long id) {
        return this.deptService.get(id);
    }
    @RequestMapping(value = "/consumer/dept/list")
    public Object listDept() {
        return this.deptService.list();
    }
    @RequestMapping(value = "/consumer/dept/add")
    public Object addDept(Dept dept) throws Exception {
        return this.deptService.add(dept);
    }
}
 
8、 【microcloud-consumer-feign】修改程序启动主类,追加操作处理。

 
package cn.study.microcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages={"cn.study.service"})//进行接口IDeptClientService的扫描生成使得可以注入到ConsumerDeptController里面
public class Consumer_80_StartSpringCloudApplication {
    public static void main(String[] args) {
        SpringApplication.run(Consumer_80_StartSpringCloudApplication.class,
                args);
    }
}
 
9、 启动测试:http://client.com/consumer/dept/get?id=1 ,可以发现 Feign 在处理的时候自带有负载均衡的配置项。java B2B2C Springboot电子商务平台源码

顶 0踩 0收藏
文章评论
    发表评论

    个人资料

    • 昵称: it绿萝
    • 等级: 高级设计师
    • 积分: 5730
    • 代码: 0 个
    • 文章: 192 篇
    • 随想: 0 条
    • 访问: 15 次
    • 关注

    人气代码

      最新提问

        站长推荐