码迷,mamicode.com
首页 > 其他好文 > 详细

feign整合nacos

时间:2021-06-02 11:44:54      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ppi   server   style   启动   nbsp   报错   用户名   配置   color   

创建两个项目,服务提供者service-offer和服务消费者service-consumer

一、service-offer的pom文件

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>

启动类上添加两个注解 @EnableDiscoveryClient @EnableFeignClients

yml配置

server:
  port: 8183
spring:
  application:
    name: service-offer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

 

提供一个API

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello world";
    }
}

服务提供者完成

二、创建服务消费者

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.5.RELEASE</version>
            <!--    排除nacos中的ribbon依赖,否则会报错        -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

启动类:添加注解

@EnableDiscoveryClient
@EnableFeignClients

yml配置

spring:
  application:
    name: service-consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
server:
  port: 8184

创建feign接口

@FeignClient("service-offer")//填写配置的server name
public interface ProvideClient {
    @RequestMapping("/hello")
    String hello();
}

并调用该接口

@RestController
public class HelloController {
    @Autowired
    private ProvideClient provideClient;
    @RequestMapping("/helloWorld")
    public String hello(){
        return provideClient.hello();
    }
}

调用成功,访问http://localhost:8848/nacos/index.html,用户名密码都是nacos,登录可查看服务状态

访问http://localhost:8184/helloWorld,访问成功

 

feign整合nacos

标签:ppi   server   style   启动   nbsp   报错   用户名   配置   color   

原文地址:https://www.cnblogs.com/gstszbc/p/14814291.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!