您的位置:首页 > 编程语言 > Java开发

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

2017-09-05 20:35 585 查看
前期条件,已经创建好注册中心,如果没有,请看上一篇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</artifact
4000
Id>
<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();

}


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实现负载均衡)(九)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: