Spring Cloud配置服务发现

在上一篇中,已经搭建好了注册中心eureka-server以及一个生产者服务service-producer-A,

现在我们需要搭建一个消费者来发现服务,可以使用Spring Cloud提供的feign来实现服务发现,

具体实现如下:

一、创建消费者服务:service-consumer-feign

在根目录spring_cloud中创建Maven Moudle模块:service-consumer-feign

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud</artifactId>
        <groupId>com.sam</groupId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>service-consumer-feign</artifactId>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
    </dependencies>
</project>


创建具体的服务发现类:ServiceAFeignClient

package com.sam.service.consumer.feign;
 
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
 
/**
 * @ClassName: ServiceAFeignClient 
 * @Description: 服务客户端实现 
 * @author sam 
 * @date 2018年8月10日 下午4:00:42
 */
@Component
@FeignClient(value = "service-producer") // 这里的name对应调用服务的spring.applicatoin.name
public interface ServiceAFeignClient {
 
    @RequestMapping(value = "/hi")
    String hi(@RequestParam("id") String id);
 
}

创建Controller来测试:TestController

package com.sam.service.consumer.feign;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @ClassName: TestController 
 * @Description: feign实现
 * @author mqx 
 * @date 2018年8月9日 下午4:39:45
 */
@RestController
public class TestController {
 
    @Autowired
    ServiceAFeignClient serviceAFeignClient;
 
    @RequestMapping("/hi")
    public String hi(@RequestParam String id){
        return serviceAFeignClient.hi(id);
    }
}

创建消费者服务启动类:ClientApplicationFeign

package com.sam.service.consumer.feign;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
 
/**
 * @ClassName: ClientApplicationFeign 
 * @Description: 消费者服务(使用Feign实现)
 * @author sam 
 * @date 2018年8月10日 下午4:00:02
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ClientApplicationFeign {
 
    public static void main(String[] args) {
        SpringApplication.run(ClientApplicationFeign.class, args);
    }
}


创建配置文件:application.yml

server:
  port: 8911
 
eureka:
  client:
    serviceUrl:
          defaultZone: http://localhost:8010/eureka/
 
spring:
  application:
      name: service-consumer-feign


 
以上代码配置完成后!在启动新增的这个消费者服务:service-consumer-feign,

可以看到消费者和生产者都可以在注册中心看到,如下图:

二、访问测试:

在创建及启动服务完成之后,可以访问消费者服务端口 测试下,

http://localhost:8911/hi?id=123

如下图:

可以看到访问正常,至此服务发现配置完成!

猜你喜欢

转载自blog.csdn.net/yuyecsdn/article/details/90028383