diff --git a/feign-eureka/pom.xml b/feign-eureka/pom.xml new file mode 100644 index 0000000..7750958 --- /dev/null +++ b/feign-eureka/pom.xml @@ -0,0 +1,96 @@ + + + 4.0.0 + + org.springframework.cloud + spring-cloud-sample-feign-eureka + 1.0.0.BUILD-SNAPSHOT + jar + + spring-cloud-sample-feign-eureka + Demo project for Spring Cloud + + + org.springframework.cloud + spring-cloud-starter-parent + 1.0.0.BUILD-SNAPSHOT + + + + + UTF-8 + 1.7 + + + + + + + maven-deploy-plugin + + true + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-eureka + + + org.springframework.cloud + spring-cloud-starter-feign + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + diff --git a/feign-eureka/src/main/java/demo/HelloClientApplication.java b/feign-eureka/src/main/java/demo/HelloClientApplication.java new file mode 100644 index 0000000..7b99c87 --- /dev/null +++ b/feign-eureka/src/main/java/demo/HelloClientApplication.java @@ -0,0 +1,63 @@ +package demo; + +import com.netflix.client.config.IClientConfig; +import com.netflix.loadbalancer.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.netflix.feign.FeignClient; +import org.springframework.cloud.netflix.feign.FeignClientScan; +import org.springframework.cloud.netflix.ribbon.RibbonClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; + +import static org.springframework.web.bind.annotation.RequestMethod.*; + +/** + * @author Spencer Gibb + */ +@SpringBootApplication +@EnableDiscoveryClient +@RestController +@FeignClientScan +@RibbonClient(name = "hello", configuration = HelloRibbonClientConfiguration.class) +public class HelloClientApplication { + @Autowired + HelloClient client; + + @RequestMapping("/") + public String hello() { + return client.hello(); + } + + public static void main(String[] args) { + SpringApplication.run(HelloClientApplication.class, args); + } + + @FeignClient("hello") + interface HelloClient { + @RequestMapping(value = "/", method = GET) + String hello(); + } +} + +// Load balancer with fixed server list for "hello" pointing to example.com +@Configuration +class HelloRibbonClientConfiguration { + + @Bean + public ILoadBalancer ribbonLoadBalancer() { + //because of this, it doesn't use eureka to lookup the server, + // but the classpath is tested + BaseLoadBalancer balancer = new BaseLoadBalancer(); + balancer.setServersList(Arrays.asList(new Server("example.com", 80))); + return balancer; + } + +} diff --git a/feign-eureka/src/main/resources/application.yml b/feign-eureka/src/main/resources/application.yml new file mode 100644 index 0000000..c6736ec --- /dev/null +++ b/feign-eureka/src/main/resources/application.yml @@ -0,0 +1,25 @@ +spring: + application: + name: HelloClient + +server: + port: 7211 + +eureka: + password: password + client: + serviceUrl: + defaultZone: http://user:${eureka.password}@localhost:8761/eureka/ + instance: + leaseRenewalIntervalInSeconds: 10 + metadataMap: + instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}} + + +endpoints: + restart: + enabled: true + shutdown: + enabled: true + health: + sensitive: false \ No newline at end of file diff --git a/feign-eureka/src/test/java/demo/HelloClientApplicationTests.java b/feign-eureka/src/test/java/demo/HelloClientApplicationTests.java new file mode 100644 index 0000000..2781dd6 --- /dev/null +++ b/feign-eureka/src/test/java/demo/HelloClientApplicationTests.java @@ -0,0 +1,35 @@ +package demo; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = HelloClientApplication.class) +@WebAppConfiguration +@IntegrationTest("server.port=0") +public class HelloClientApplicationTests { + + @Value("${local.server.port}") + int port; + + @Test + public void clientConnects() { + ResponseEntity response = new TestRestTemplate().getForEntity("http://localhost:" + port, String.class); + assertNotNull("response was null", response); + assertEquals("Wrong status code", HttpStatus.OK, response.getStatusCode()); + assertTrue(response.getBody().contains(" + + 4.0.0 + + org.springframework.cloud + spring-cloud-sample-feign + 1.0.0.BUILD-SNAPSHOT + jar + + spring-cloud-sample-feign + Demo project for Spring Cloud + + + org.springframework.cloud + spring-cloud-starter-parent + 1.0.0.BUILD-SNAPSHOT + + + + + UTF-8 + 1.7 + + + + + org.springframework.cloud + spring-cloud-starter-feign + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + maven-deploy-plugin + + true + + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + diff --git a/feign/src/main/java/demo/FeignClientApplication.java b/feign/src/main/java/demo/FeignClientApplication.java new file mode 100644 index 0000000..c02e1e9 --- /dev/null +++ b/feign/src/main/java/demo/FeignClientApplication.java @@ -0,0 +1,27 @@ +package demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cloud.netflix.feign.FeignClient; +import org.springframework.cloud.netflix.feign.FeignClientScan; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +@Configuration +@ComponentScan +@EnableAutoConfiguration +@FeignClientScan +public class FeignClientApplication { + + public static void main(String[] args) { + SpringApplication.run(FeignClientApplication.class, args); + } +} + +@FeignClient(value = "example.com", loadbalance = false) +interface RestClient { + @RequestMapping(value="/", method=RequestMethod.GET) + String hello(); +} diff --git a/feign/src/main/resources/application.properties b/feign/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/feign/src/test/java/demo/FeignClientApplicationTests.java b/feign/src/test/java/demo/FeignClientApplicationTests.java new file mode 100644 index 0000000..d0b6044 --- /dev/null +++ b/feign/src/test/java/demo/FeignClientApplicationTests.java @@ -0,0 +1,23 @@ +package demo; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = FeignClientApplication.class) +public class FeignClientApplicationTests { + + @Autowired + private RestClient client; + + @Test + public void clientConnects() { + assertTrue(client.hello().contains("http://www.spring.io + feign + feign-eureka zuul-proxy zuul-proxy-eureka