forked from easonjim/5_java_example
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
389 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<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.jsoft.testzookeeper</groupId> | ||
<artifactId>zookeeperdemo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>pom</packaging> | ||
<name>ZooKeeperDemo</name> | ||
<description>This is ZookKeeperDemo</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.4.5.RELEASE</version> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- 健康监控 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
<!-- 断路器 --> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-hystrix</artifactId> | ||
</dependency> | ||
<!-- 负载均衡 --> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-ribbon</artifactId> | ||
</dependency> | ||
<!-- ZK依赖 --> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> | ||
</dependency> | ||
<!-- Web支持 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<!-- 测试 --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-dependencies</artifactId> | ||
<version>Camden.SR7</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<modules> | ||
<module>zookeeperservice</module> | ||
<module>zookeeperclient</module> | ||
</modules> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0"?> | ||
<project | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.jsoft.testzookeeper</groupId> | ||
<artifactId>zookeeperdemo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<groupId>com.jsoft.testzookeeper</groupId> | ||
<artifactId>zookeeperclient</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>zookeeperclient</name> | ||
<url>http://maven.apache.org</url> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
<dependencies> | ||
<!-- Feign客户端 --> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-feign</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
19 changes: 19 additions & 0 deletions
19
apollotest/test2/zookeeperclient/src/main/java/com/jsoft/testzookeeper/client/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.jsoft.testzookeeper.client; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; | ||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; | ||
import org.springframework.cloud.netflix.feign.EnableFeignClients; | ||
|
||
@SpringBootApplication | ||
@EnableDiscoveryClient | ||
@EnableCircuitBreaker | ||
@EnableFeignClients | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
SpringApplication.run(App.class,args); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...est/test2/zookeeperclient/src/main/java/com/jsoft/testzookeeper/client/config/Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.jsoft.testzookeeper.client.config; | ||
|
||
|
||
import org.springframework.cloud.client.loadbalancer.LoadBalanced; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class Config { | ||
|
||
@Bean | ||
@LoadBalanced | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...keeperclient/src/main/java/com/jsoft/testzookeeper/client/controller/HelloController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.jsoft.testzookeeper.client.controller; | ||
|
||
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; | ||
|
||
import com.jsoft.testzookeeper.client.service.HelloService; | ||
import com.jsoft.testzookeeper.client.service.ServiceFeign; | ||
|
||
@RestController | ||
public class HelloController { | ||
|
||
@Autowired | ||
private HelloService helloService; | ||
|
||
@Autowired | ||
private ServiceFeign serviceFeign; | ||
|
||
@RequestMapping(value = "hello") | ||
public String hello(@RequestParam String name) { | ||
return helloService.sayHello(name); | ||
} | ||
|
||
@RequestMapping(value = "hello2") | ||
public String hello2(@RequestParam String name) { | ||
return serviceFeign.sayHello(name); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...t2/zookeeperclient/src/main/java/com/jsoft/testzookeeper/client/service/HelloService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.jsoft.testzookeeper.client.service; | ||
|
||
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Service | ||
public class HelloService { | ||
|
||
@Autowired | ||
private RestTemplate restTemplate; | ||
|
||
@HystrixCommand(fallbackMethod = "sayHelloFallback") | ||
public String sayHello(String name) { | ||
return restTemplate.getForEntity("http://service-zookeeper/hello?name=" + name, String.class).getBody(); | ||
} | ||
|
||
private String sayHelloFallback(String name) { | ||
return "service error"; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...t2/zookeeperclient/src/main/java/com/jsoft/testzookeeper/client/service/ServiceFeign.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.jsoft.testzookeeper.client.service; | ||
|
||
import org.springframework.cloud.netflix.feign.FeignClient; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import com.jsoft.testzookeeper.client.service.fallback.FeignFallback; | ||
|
||
@FeignClient(value = "service-zookeeper", fallback = FeignFallback.class) | ||
public interface ServiceFeign { | ||
|
||
@RequestMapping(value = "/hello") | ||
String sayHello(@RequestParam(name = "name") String name); | ||
} |
15 changes: 15 additions & 0 deletions
15
...erclient/src/main/java/com/jsoft/testzookeeper/client/service/fallback/FeignFallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.jsoft.testzookeeper.client.service.fallback; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import com.jsoft.testzookeeper.client.service.ServiceFeign; | ||
|
||
@Component | ||
public class FeignFallback implements ServiceFeign { | ||
|
||
@Override | ||
public String sayHello(@RequestParam(name = "name") String name) { | ||
return null; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
apollotest/test2/zookeeperclient/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
server.port=8810 | ||
spring.application.name=/client-zookeeper | ||
spring.cloud.zookeeper.discovery.register=false | ||
spring.cloud.zookeeper.discovery.root=/spring-cloud-service | ||
spring.cloud.zookeeper.connect-string=localhost:2181 | ||
spring.cloud.zookeeper.dependencies.service-zookeeper.required=true | ||
spring.cloud.zookeeper.dependencies.service-zookeeper.path=/service-zookeeper | ||
spring.cloud.zookeeper.dependencies.service-zookeeper.loadBalancerType=ROUND_ROBIN |
38 changes: 38 additions & 0 deletions
38
apollotest/test2/zookeeperclient/src/test/java/com/jsoft/testzookeeper/client/AppTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.jsoft.testzookeeper.client; | ||
|
||
import junit.framework.Test; | ||
import junit.framework.TestCase; | ||
import junit.framework.TestSuite; | ||
|
||
/** | ||
* Unit test for simple App. | ||
*/ | ||
public class AppTest | ||
extends TestCase | ||
{ | ||
/** | ||
* Create the test case | ||
* | ||
* @param testName name of the test case | ||
*/ | ||
public AppTest( String testName ) | ||
{ | ||
super( testName ); | ||
} | ||
|
||
/** | ||
* @return the suite of tests being tested | ||
*/ | ||
public static Test suite() | ||
{ | ||
return new TestSuite( AppTest.class ); | ||
} | ||
|
||
/** | ||
* Rigourous Test :-) | ||
*/ | ||
public void testApp() | ||
{ | ||
assertTrue( true ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0"?> | ||
<project | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" | ||
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.jsoft.testzookeeper</groupId> | ||
<artifactId>zookeeperdemo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
<groupId>com.jsoft.testzookeeper</groupId> | ||
<artifactId>zookeeperservice</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>zookeeperservice</name> | ||
<url>http://maven.apache.org</url> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.ctrip.framework.apollo</groupId> | ||
<artifactId>apollo-client</artifactId> | ||
<version>0.8.0</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
18 changes: 18 additions & 0 deletions
18
apollotest/test2/zookeeperservice/src/main/java/com/jsoft/testzookeeper/service/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jsoft.testzookeeper.service; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; | ||
|
||
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig; | ||
|
||
@SpringBootApplication | ||
@EnableDiscoveryClient | ||
@EnableApolloConfig | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
SpringApplication.run(App.class, args); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...eperservice/src/main/java/com/jsoft/testzookeeper/service/controller/HelloController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.jsoft.testzookeeper.service.controller; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class HelloController { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(HelloController.class); | ||
|
||
@RequestMapping(value = "/hello", method = RequestMethod.GET) | ||
public String sayHello(@RequestParam(name = "name") String name) { | ||
log.info("param:name->{}", name); | ||
return "hello: " + name; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
apollotest/test2/zookeeperservice/src/main/resources/META-INF/app.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
app.id=zookeeperservice |
Oops, something went wrong.