add some feign test with and without eureka
This commit is contained in:
63
feign-eureka/src/main/java/demo/HelloClientApplication.java
Normal file
63
feign-eureka/src/main/java/demo/HelloClientApplication.java
Normal file
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
25
feign-eureka/src/main/resources/application.yml
Normal file
25
feign-eureka/src/main/resources/application.yml
Normal file
@@ -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
|
||||
@@ -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<String> 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("<html"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user