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

本文是IDEA创建SpringCloud项目的第八篇,主要介绍如何创建服务消费者。首先确保已建立注册中心,然后通过步骤创建服务提供者,包括pom.xml配置、application.properties设置、启动类编写、Controller和Service的实现。提供了完整的源代码链接以供参考。
摘要由CSDN通过智能技术生成

前期条件,已经创建好注册中心,如果没有,请看上一篇http://blog.csdn.net/hcmony/article/details/77855158。

一,创建服务提供者

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

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();

}

springcloud 源代码 https://github.com/hcmony/springcloud.git

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实现负载均衡)(九)

idea创建springcloud项目图文教程(config 实现配置中心)(十一)

idea创建springcloud项目图文教程(bus 消息总线)(十二)

 

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值