fegin动态调用

第一部分: apollo搭建 

1>apollo下载

链接:百度网盘 请输入提取码

提取码:hvbo

2>文件解压

3>创建apollo数据库,sql文件在解压文件中的【sql】文件夹内;在任意一款mysql可视化工具中导入sql脚本,即可成功创建apollo需要使用的数据库

4>配置库

修改【demo】配置文件中的数据库连接,用户名/密码

5 >启动

cmd黑窗口下,进入【demo】所在的文件路径,执行 demo.sh start ;停止: demo.sh stop

现象: 可以看到启动窗口,窗口在启动之后会自动关闭

6>验证

浏览器访问: http://localhost:8070/   用户名/密码为  apollo/admin

7>创建一个项目

第二部分:

1>创建springboot项目

2>pom文件引入jar

<dependency>

    <groupId>com.ctrip.framework.apollo</groupId>

    <artifactId>apollo-client</artifactId>

    <version>1.3.0</version>

</dependency>

3>启动类添加注解 @EnableApolloConfig

4>创建【server.properties】文件:

文件路径:如果没有,需要自行新增

内容为:

env=DEV

5>项目resources下创建【META-INF】文件夹,在【META-INF】下面在创建 app.properties

内容:

app.id= apollo上项目对应的appId

6>项目resources下创建【apollo-env.properties】配置文件

内容为

local.meta=http://192.168.1.108:8080  -- 对应自己电脑ip

dev.meta=http://192.168.1.108:8080    -- 对应自己电脑ip

7>创建监听apollo配置文件,具体内容为

package com.jia.customterfegin.config;

import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class RefreshApolloConfig implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Autowired
    private RefreshScope refreshScope;

    @ApolloConfigChangeListener(value = {"application.properties"})
    public void onChange(ConfigChangeEvent changeEvent) {
        log.info("----------------------------修改了apollo配置-----------------------------");
        refreshTaskScheduleProperties(changeEvent);
    }

    private void refreshTaskScheduleProperties(ConfigChangeEvent changeEvent) {
        // 更新相应的bean的属性值,主要是存在@ConfigurationProperties注解的bean
        this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
        refreshScope.refreshAll();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

8>使用@value注解获取配置,为配置指定默认值 

 获取配置值需要是完整的url,包含但不限于:  http:ip:端口/请求地址/请求参数

package com.jia.customterfegin.Server;

import com.jia.customterfegin.Fegin.ServerFegin.ServerFeginClient;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Service;

import java.net.URI;

@Service

@Slf4j

public class UserServer {

    @Autowired

    private ServerFeginClient serverFeginClient;

    @Value("${zjc.port:http://192.168.1.108:8087/ServerFegin}")

    private String port;

    @Value("${zjc.url:/user/1}")

    private String url;

    public String nameByServerFeginClient(Integer id) throws Exception {

        return serverFeginClient.resultNameById(new URI(port+url), id);

    }

}

9>fegin的接口中使用RequestMapping注解,其中注解属性value置为空

package com.jia.customterfegin.Fegin.ServerFegin;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.PathVariable;

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

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

import org.springframework.web.bind.annotation.RequestParam;

import java.net.URI;

@FeignClient(name = "ServerFegin", url = "${zjc.port}", fallback = ServerFeginClientFallBack.class)

public interface ServerFeginClient {

    @RequestMapping(value = "", method = RequestMethod.GET)

    public String resultNameById(URI uri, @RequestParam(value = "id") Integer id) throws Exception;

}

10>测试通过在apollo上面修改配置,观察调用地址是否变更

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值