Commit 5bc7bfff by GongYu

初始化

parent 5e6c6a71
/.classpath
/.project
/*.log
/.settings
/target
<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>me.rainsay</groupId>
<artifactId>first-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>testdemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.19.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package me.rainsay.testdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableCircuitBreaker
@EnableFeignClients
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
package me.rainsay.testdemo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private final static Logger log = LoggerFactory.getLogger(HelloController.class);
@GetMapping("/hello")
public String hello() {
log.info("GET /hello success");
return "hello world from spring boot";
}
}
package me.rainsay.testdemo.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import me.rainsay.testdemo.feign.fallback.BookClientFallback;
@FeignClient(name = "book-server", fallback = BookClientFallback.class)
public interface BookClient {
@RequestMapping(value = "/book", method = RequestMethod.GET)
String hello();
}
package me.rainsay.testdemo.feign.fallback;
import org.springframework.stereotype.Component;
import me.rainsay.testdemo.feign.BookClient;
@Component
public class BookClientFallback implements BookClient {
@Override
public String hello() {
return "No hello";
}
}
eureka:
client:
enabled: false
logging:
file: test.log
\ No newline at end of file
package me.rainsay.testdemo;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.cloud.netflix.ribbon.StaticServerList;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.google.common.net.MediaType;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import me.rainsay.testdemo.feign.BookClient;
@RunWith(SpringRunner.class)
@SpringBootTest(properties = {
"feign.hystrix.enabled=true",
"eureka.client.enabled=false"
})
@ContextConfiguration(classes = {
SomeTest.LocalRibbonClientConfiguration.class
})
public class SomeTest {
@ClassRule
public static WireMockRule wireMock = new WireMockRule(wireMockConfig().dynamicPort());
@Autowired
BookClient bookClient;
@Before
public void setup() {
stubFor(get(urlEqualTo("/book"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", MediaType.PLAIN_TEXT_UTF_8.toString())
.withBody("Hello World"))
);
}
@Test
public void testHello() {
String hello = bookClient.hello();
assertNotNull("shoud not be null",hello);
assertEquals("Hello World", hello);
}
@Test
public void testHelloFallback() {
stubFor(get(urlEqualTo("/book"))
.willReturn(aResponse().withFixedDelay(60000)));
String hello = bookClient.hello();
assertNotNull(hello);
assertEquals("No hello", hello);
}
@TestConfiguration
public static class LocalRibbonClientConfiguration {
@Bean
public ServerList<Server> ribbonServerList() {
return new StaticServerList<>(new Server("localhost", wireMock.port()));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment