Skip to content

Commit

Permalink
服务消费者(Feign)
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-io committed Apr 19, 2018
1 parent f0ac2d4 commit d2b4fa3
Show file tree
Hide file tree
Showing 19 changed files with 646 additions and 0 deletions.
69 changes: 69 additions & 0 deletions chapter-03/eurekaserver/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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>io.ken.springcloud.eurekaserver</groupId>
<artifactId>eurekaserver</artifactId>
<version>1.0-SNAPSHOT</version>

<name>eurekaserver</name>
<url>http://ken.io</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

</dependencies>

<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<build>
<finalName>eurekaserver</finalName>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.ken.springcloud.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;


@EnableEurekaServer
@SpringBootApplication
public class App {

public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
13 changes: 13 additions & 0 deletions chapter-03/eurekaserver/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
server:
port: 8800

spring:
application:
name: EurekaServer

eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:${server.port}/eureka/
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.ken.springcloud.eurekaserver;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}
79 changes: 79 additions & 0 deletions chapter-03/feignclient/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?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>io.ken.springcloud.feignclient</groupId>
<artifactId>feignclient</artifactId>
<version>1.0-SNAPSHOT</version>

<name>feignclient</name>
<url>http://ken.io</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.BUILD-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-web</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

</dependencies>

<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<build>
<finalName>feignclient</finalName>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.ken.springcloud.feignclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class App {

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.ken.springcloud.feignclient.controller;

import io.ken.springcloud.feignclient.model.Plus;
import io.ken.springcloud.feignclient.service.TestService;
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;

@RestController
public class TestController {

@Autowired
private TestService testService;

@RequestMapping("/")
public Object index() {
return "feign client";
}

@RequestMapping("/ti")
public Object ti() {
return testService.indexService();
}

@RequestMapping("/plus")
public Object plus(@RequestParam("numa") int numA, @RequestParam("numb") int numB) {
return testService.plusService(numA, numB);
}

@RequestMapping("/plusab")
public Object plusA(@RequestParam("numa") int numA, @RequestParam("numb") int numB) {
Plus plus = new Plus();
plus.setNumA(numA);
plus.setNumB(numB);
return testService.plusabService(plus);
}

@RequestMapping("/plus2")
public Object plus2(Plus plus) {
return testService.plus2Service(plus);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.ken.springcloud.feignclient.model;

public class Plus {

private int numA;

private int numB;

public int getNumA() {
return numA;
}

public void setNumA(int numA) {
this.numA = numA;
}

public int getNumB() {
return numB;
}

public void setNumB(int numB) {
this.numB = numB;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.ken.springcloud.feignclient.model;

public class Result {

private int code;

private String message;

private Object content;

private String serviceName;

private String host;

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Object getContent() {
return content;
}

public void setContent(Object content) {
this.content = content;
}

public String getServiceName() {
return serviceName;
}

public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.ken.springcloud.feignclient.service;

import io.ken.springcloud.feignclient.model.Plus;
import io.ken.springcloud.feignclient.model.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "testservice")
public interface TestService {

@RequestMapping(value = "/", method = RequestMethod.GET)
String indexService();

@RequestMapping(value = "/plus", method = RequestMethod.GET)
Result plusService(@RequestParam(name = "numA") int numA, @RequestParam(name = "numB") int numB);

@RequestMapping(value = "/plus", method = RequestMethod.POST, consumes = "application/json")
Result plusabService(Plus plus);

@RequestMapping(value = "/plus2", method = RequestMethod.POST)
Result plus2Service(@RequestBody Plus plus);


}
11 changes: 11 additions & 0 deletions chapter-03/feignclient/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
server:
port: 8605

spring:
application:
name: feignclient

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8800/eureka/
Loading

0 comments on commit d2b4fa3

Please sign in to comment.