前期条件,已经创建好注册中心,如果没有,请看上一篇

http://blog.csdn.net/hcmony/article/details/77855158。

一,创建服务消费者

springcloud项目创建参考http://blog.csdn.net/hcmony/article/details/77855158

创建方式和创建eureka工程一样,相比于服务提供者,多了一个feign


1,pox.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.hcmony</groupId>
   <artifactId>springcloud-customer</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>springcloud-customer</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.6.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <spring-cloud.version>Dalston.SR3</spring-cloud.version>
   </properties>

   <dependencies>

      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-feign</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka-server</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>

   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>


</project>

2,application.properties

eureka.client.service-url.defaultZone: http://localhost:8761/eureka/
server.port=8764
spring.application.name=springcloud-customer


3,SpringcloudCustomerApplication

package com.hcmony;

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;

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class SpringcloudCustomerApplication {

   public static void main(String[] args) {
      SpringApplication.run(SpringcloudCustomerApplication.class, args);
   }
}


4,TestController

package com.hcmony;

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;

/**
 * Created by hcmony on 2017/9/5.
 */
@RestController
public class TestController {

   @Autowired
   private  TestService testService;

   @RequestMapping(value = "/test",method = RequestMethod.GET)
   public String test(){
      return  testService.test();
   }

}


5,TestService

package com.hcmony;

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

/**
 * Created by hcmony on 2017/9/5.
 */
@FeignClient(value = "springcloud-server")
public interface TestService {

   @RequestMapping("/test")
   public String test();

}

===========================================================================

下面是今天搭建环境时遇到的一个问题

关于版本问题今天遇到一个坑坑了四个小时:

创建生产者的时候boot-parent版本用的2.0.3,项目正常启动完成服务注册

于是创建消费者的时候也用的2.0.3,引入了feign,编译报错信息如下


网上查这是maven库冲突,在项目pom.xml所在目录下执行

mvn -U dependency:tree -Dverbose > tree.txt 结果没有发现“conflict”

网上找到那些成功的版本boot-parent用的都是1.x.x版本,直接在pom中还是不行

折腾了四个小时,在idea中重新建立eureka项目,版本选1.5.15


然后再手动引入feign包,没有报错,终于正常启动,所以如果选boot版本在开始创建的时候就要选好。

这篇文章刚开始的pom中写boot版本用的1.5.6,虽然我没有试过这个,但感觉是可以的。

我猜测最可能的原因是feign和boot-parent 2.x.x的版本相冲突

idea创建maven项目,本教程适合各类小白(一)

idea创建maven,spring,springmvc,mybatis,项目(二)

idea创建maven,spring,springmvc,mybatis,项目(三)
idea创建springboot项目图文教程(四)
idea创建springboot项目图文教程(配置文件)(五)
idea创建springcloud项目图文教程(EurekaServer注册中心)(六)

idea创建springcloud项目图文教程(创建服务提供者)(七)

idea创建springcloud项目图文教程(创建消费者)(八)

idea创建springcloud项目图文教程(Feign实现负载均衡)(九)

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐