中國廣東省深圳市望海路半島城邦三期 518067 +86 13113668890 <netkiller@msn.com>
java
$Id: book.xml 606 2013-05-29 09:52:58Z netkiller $git
版權 © 2015-2018 Neo Changithub
版權聲明web
轉載請與做者聯繫,轉載時請務必標明文章原始出處和做者信息及本聲明。spring
http://www.netkiller.cnapache |
---|
http://netkiller.github.ioapp |
http://netkiller.sourceforge.netmaven |
核心參數ide
配置項 默認值 spring.cloud.consul.enabled true spring.cloud.consul.host localhost spring.cloud.consul.port 8500
<?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>cn.netkiller</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <url>http://www.netkiller.cn</url> <organization> <name>Netkiller Spring Cloud 手札</name> <url>http://www.netkiller.cn</url> </organization> <developers> <developer> <name>Neo</name> <email>netkiller@msn.com</email> <organization>Netkiller Spring Cloud 手札</organization> <organizationUrl>http://www.netkiller.cn</organizationUrl> <roles> <role>Author</role> </roles> </developer> </developers> <!--使用aliyun鏡像 --> <repositories> <repository> <id>alimaven</id> <name>Maven Aliyun Mirror</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath /> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix</artifactId> <version>1.3.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config</artifactId> <version>1.3.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <modules> <module>consol-producer</module> <module>consol-consumer</module> <module>consol-config</module> <module>openfeign</module> </modules> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
<?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>cn.netkiller</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <!-- <groupId>cn.netkiller</groupId> --> <artifactId>consul-producer</artifactId> <!-- <version>0.0.1-SNAPSHOT</version> --> <name>consul-producer</name> <url>http://www.netkiller.cn</url> <description>Spring Cloud Consul Sample</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>11</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-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <!-- Only needed at compile time --> <optional>true</optional> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <!-- <version>3.8.1</version> --> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- <configuration> <mainClass>cn.netkiller.config.Application</mainClass> </configuration> --> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> </project>
server.port=8080 spring.application.name=spring-cloud-consul-producer spring.cloud.consul.host=192.168.4.217 spring.cloud.consul.port=8500
package cn.netkiller.consul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { System.out.println("Hello World!"); SpringApplication.run(Application.class, args); } }
package cn.netkiller.consul.controller; import org.springframework.web.bind.annotation.GetMapping; 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 { public TestController() { // TODO Auto-generated constructor stub } @GetMapping("/hello") public String provider() { return "Helloworld!!!"; } @RequestMapping("/hi") public String hi(@RequestParam(name = "name") String name) { return "hi " + name + "!"; } }
<?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>cn.netkiller</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.netkiller</groupId> <artifactId>consul-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>consul-consumer</name> <url>http://www.netkiller.cn</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- <configuration> <mainClass>cn.netkiller.config.Application</mainClass> </configuration> --> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> </project>
server.port=8082 spring.application.name=spring-cloud-consul-consumer spring.cloud.consul.host=192.168.4.217 spring.cloud.consul.port=8500 #設置不須要註冊到 consul 中 spring.cloud.consul.discovery.register=false
package cn.netkiller.consul.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { System.out.println("Consol Consumer!"); SpringApplication.run(Application.class, args); } }
package cn.netkiller.consul.consumer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class TestController { public ConsumerController() { // TODO Auto-generated constructor stub } @Autowired private LoadBalancerClient loadBalancerClient; @Autowired private DiscoveryClient discoveryClient; /** * 獲取全部服務 */ @RequestMapping("/services") public Object services() { return discoveryClient.getInstances("spring-cloud-consul-producer"); } /** * 從全部服務中選擇一個服務(輪詢) */ @RequestMapping("/discover") public Object discover() { return loadBalancerClient.choose("spring-cloud-consul-producer").getUri().toString(); } @RequestMapping("/call") public String call() { ServiceInstance serviceInstance = loadBalancerClient.choose("spring-cloud-consul-producer"); System.out.println("服務地址:" + serviceInstance.getUri()); System.out.println("服務名稱:" + serviceInstance.getServiceId()); String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class); System.out.println(callServiceResult); return callServiceResult; } }
<?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>cn.netkiller</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>cn.netkiller</groupId> <artifactId>openfeign</artifactId> <version>0.0.1-SNAPSHOT</version> <name>openfeign</name> <url>http://www.netkiller.cn</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</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>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- <configuration> <mainClass>cn.netkiller.config.Application</mainClass> </configuration> --> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> </project>
server.port=8083 spring.application.name=spring-cloud-consul-openfeign spring.cloud.consul.host=192.168.4.217 spring.cloud.consul.port=8500 spring.cloud.consul.discovery.register=false
package cn.netkiller.openfeign; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class Application { public static void main(String[] args) { System.out.println("openfeign!"); SpringApplication.run(Application.class, args); } }
package cn.netkiller.openfeign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "spring-cloud-consul-producer", fallback = FeignFallback.class) public interface TestFeign { @RequestMapping(value = "/hi", method = RequestMethod.GET) String hi(@RequestParam(value = "name") String name); }
package cn.netkiller.openfeign; public class FeignFallback implements TestFeign { @Override public String hi(String name) { return "sorry,熔斷介入"; } }
package cn.netkiller.openfeign; 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 TestFeign testFeign; @RequestMapping("/feign") public String testFeign(@RequestParam(name = "name") String name) { return testFeign.hi(name); } }