diff --git b/client-feign-hystrix/pom.xml a/client-feign-hystrix/pom.xml new file mode 100644 index 0000000..44c8aa0 --- /dev/null +++ a/client-feign-hystrix/pom.xml @@ -0,0 +1,26 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>client-feign-hystrix</artifactId> + <name>client-feign-hystrix</name> + <description>服务消费者-feign-断路器模式</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-feign</artifactId> + </dependency> + </dependencies> +</project> diff --git b/client-feign-hystrix/src/main/java/cn/xbz/clientfeignhystrix/ClientFeignCallBack.java a/client-feign-hystrix/src/main/java/cn/xbz/clientfeignhystrix/ClientFeignCallBack.java new file mode 100644 index 0000000..a4f3228 --- /dev/null +++ a/client-feign-hystrix/src/main/java/cn/xbz/clientfeignhystrix/ClientFeignCallBack.java @@ -0,0 +1,12 @@ +package cn.xbz.clientfeignhystrix; + +import org.springframework.stereotype.Service; + +@Service +public class ClientFeignCallBack implements IClientFeign{ + + @Override + public String hello(String user) { + return "sorry , " + user + " the feign is error ~"; + } +} \ No newline at end of file diff --git b/client-feign-hystrix/src/main/java/cn/xbz/clientfeignhystrix/ClientFeignHystrixApplication.java a/client-feign-hystrix/src/main/java/cn/xbz/clientfeignhystrix/ClientFeignHystrixApplication.java new file mode 100644 index 0000000..7977c02 --- /dev/null +++ a/client-feign-hystrix/src/main/java/cn/xbz/clientfeignhystrix/ClientFeignHystrixApplication.java @@ -0,0 +1,16 @@ +package cn.xbz.clientfeignhystrix; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.netflix.feign.EnableFeignClients; + +@EnableEurekaClient +@EnableFeignClients +@SpringBootApplication +public class ClientFeignHystrixApplication { + + public static void main(String[] args) { + SpringApplication.run(ClientFeignHystrixApplication.class, args); + } +} \ No newline at end of file diff --git b/client-feign-hystrix/src/main/java/cn/xbz/clientfeignhystrix/IClientFeign.java a/client-feign-hystrix/src/main/java/cn/xbz/clientfeignhystrix/IClientFeign.java new file mode 100644 index 0000000..0988ef5 --- /dev/null +++ a/client-feign-hystrix/src/main/java/cn/xbz/clientfeignhystrix/IClientFeign.java @@ -0,0 +1,14 @@ +package cn.xbz.clientfeignhystrix; + +import org.springframework.cloud.netflix.feign.FeignClient; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Component +@FeignClient(value="service" , fallback = ClientFeignCallBack.class) +public interface IClientFeign { + + @RequestMapping("/hello") + String hello(@RequestParam("user") String user); +} \ No newline at end of file diff --git b/client-feign-hystrix/src/main/java/cn/xbz/clientfeignhystrix/TestController.java a/client-feign-hystrix/src/main/java/cn/xbz/clientfeignhystrix/TestController.java new file mode 100644 index 0000000..010f543 --- /dev/null +++ a/client-feign-hystrix/src/main/java/cn/xbz/clientfeignhystrix/TestController.java @@ -0,0 +1,17 @@ +package cn.xbz.clientfeignhystrix; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class TestController { + + @Autowired + private IClientFeign clientFeign; + + @RequestMapping("/test") + public String test(String user) { + return clientFeign.hello(user); + } +} \ No newline at end of file diff --git b/client-feign-hystrix/src/main/resources/application.properties a/client-feign-hystrix/src/main/resources/application.properties new file mode 100644 index 0000000..28a2a90 --- /dev/null +++ a/client-feign-hystrix/src/main/resources/application.properties @@ -0,0 +1,8 @@ +spring.application.name=client-feign-hystrix + +server.port=8832 + +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ + +#开启feign的断路器模式 +feign.hystrix.enabled=true \ No newline at end of file diff --git b/client-feign/pom.xml a/client-feign/pom.xml new file mode 100644 index 0000000..d9a0bc0 --- /dev/null +++ a/client-feign/pom.xml @@ -0,0 +1,26 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>client-feign</artifactId> + <name>client-feign</name> + <description>服务消费者-feign</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-feign</artifactId> + </dependency> + </dependencies> +</project> diff --git b/client-feign/src/main/java/cn/xbz/clientfeign/ClientFeignApplication.java a/client-feign/src/main/java/cn/xbz/clientfeign/ClientFeignApplication.java new file mode 100644 index 0000000..f339ef5 --- /dev/null +++ a/client-feign/src/main/java/cn/xbz/clientfeign/ClientFeignApplication.java @@ -0,0 +1,16 @@ +package cn.xbz.clientfeign; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.netflix.feign.EnableFeignClients; + +@EnableEurekaClient +@EnableFeignClients +@SpringBootApplication +public class ClientFeignApplication { + + public static void main(String[] args) { + SpringApplication.run(ClientFeignApplication.class, args); + } +} \ No newline at end of file diff --git b/client-feign/src/main/java/cn/xbz/clientfeign/IClientFeign.java a/client-feign/src/main/java/cn/xbz/clientfeign/IClientFeign.java new file mode 100644 index 0000000..11948ee --- /dev/null +++ a/client-feign/src/main/java/cn/xbz/clientfeign/IClientFeign.java @@ -0,0 +1,14 @@ +package cn.xbz.clientfeign; + +import org.springframework.cloud.netflix.feign.FeignClient; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Component +@FeignClient("service")//调用的服务名称 +public interface IClientFeign { + + @RequestMapping("/hello") + String hello(@RequestParam("user") String user); +} \ No newline at end of file diff --git b/client-feign/src/main/java/cn/xbz/clientfeign/TestController.java a/client-feign/src/main/java/cn/xbz/clientfeign/TestController.java new file mode 100644 index 0000000..f3dc5bf --- /dev/null +++ a/client-feign/src/main/java/cn/xbz/clientfeign/TestController.java @@ -0,0 +1,17 @@ +package cn.xbz.clientfeign; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class TestController { + + @Autowired + private IClientFeign clientFeign; + + @RequestMapping("/test") + public String test(String user) { + return clientFeign.hello(user); + } +} \ No newline at end of file diff --git b/client-feign/src/main/resources/application.properties a/client-feign/src/main/resources/application.properties new file mode 100644 index 0000000..da2cfbe --- /dev/null +++ a/client-feign/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.application.name=client-feign + +server.port=8822 + +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ \ No newline at end of file diff --git b/client-ribbon-hystrix/pom.xml a/client-ribbon-hystrix/pom.xml new file mode 100644 index 0000000..d68cf25 --- /dev/null +++ a/client-ribbon-hystrix/pom.xml @@ -0,0 +1,37 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>client-ribbon-hystrix</artifactId> + <name>client-ribbon-hystrix</name> + <description>服务消费者-ribbon-断路器模式</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-ribbon</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-hystrix</artifactId> + </dependency> + + <!-- 如果需要dashboard监控 , 则被监控的服务需要引入actuator --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-actuator</artifactId> + </dependency> + </dependencies> +</project> diff --git b/client-ribbon-hystrix/src/main/java/cn/xbz/clientribbonhystrix/ClientRibbonHystrixApplication.java a/client-ribbon-hystrix/src/main/java/cn/xbz/clientribbonhystrix/ClientRibbonHystrixApplication.java new file mode 100644 index 0000000..13f7cc6 --- /dev/null +++ a/client-ribbon-hystrix/src/main/java/cn/xbz/clientribbonhystrix/ClientRibbonHystrixApplication.java @@ -0,0 +1,25 @@ +package cn.xbz.clientribbonhystrix; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.netflix.hystrix.EnableHystrix; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +@EnableEurekaClient +@EnableHystrix +@SpringBootApplication +public class ClientRibbonHystrixApplication { + + public static void main(String[] args) { + SpringApplication.run(ClientRibbonHystrixApplication.class, args); + } + + @Bean + @LoadBalanced + public RestTemplate restTemplate(){ + return new RestTemplate(); + } +} \ No newline at end of file diff --git b/client-ribbon-hystrix/src/main/java/cn/xbz/clientribbonhystrix/TestController.java a/client-ribbon-hystrix/src/main/java/cn/xbz/clientribbonhystrix/TestController.java new file mode 100644 index 0000000..8bc0e09 --- /dev/null +++ a/client-ribbon-hystrix/src/main/java/cn/xbz/clientribbonhystrix/TestController.java @@ -0,0 +1,25 @@ +package cn.xbz.clientribbonhystrix; + +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +public class TestController { + + @Autowired + private RestTemplate restTemplate; + + @RequestMapping("/test") + @HystrixCommand(fallbackMethod = "testError") + public String test(String user) { + return restTemplate.getForObject("http://service/hello?user=" + user, String.class); + } + + + public String testError(String user){ + return "sorry , " + user + " the ribbon is error ~"; + } +} \ No newline at end of file diff --git b/client-ribbon-hystrix/src/main/resources/application.properties a/client-ribbon-hystrix/src/main/resources/application.properties new file mode 100644 index 0000000..f03e80d --- /dev/null +++ a/client-ribbon-hystrix/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.application.name=client-ribbon-hystrix + +server.port=8831 + +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ \ No newline at end of file diff --git b/client-ribbon/pom.xml a/client-ribbon/pom.xml new file mode 100644 index 0000000..677ee05 --- /dev/null +++ a/client-ribbon/pom.xml @@ -0,0 +1,26 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>client-ribbon</artifactId> + <name>client-ribbon</name> + <description>服务消费者-ribbon</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-ribbon</artifactId> + </dependency> + </dependencies> +</project> diff --git b/client-ribbon/src/main/java/cn/xbz/clientribbon/ClientRibbonApplication.java a/client-ribbon/src/main/java/cn/xbz/clientribbon/ClientRibbonApplication.java new file mode 100644 index 0000000..165aae2 --- /dev/null +++ a/client-ribbon/src/main/java/cn/xbz/clientribbon/ClientRibbonApplication.java @@ -0,0 +1,24 @@ +package cn.xbz.clientribbon; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + + +@EnableEurekaClient +@SpringBootApplication +public class ClientRibbonApplication { + + public static void main(String[] args) { + SpringApplication.run(ClientRibbonApplication.class, args); + } + + @Bean + @LoadBalanced + public RestTemplate restTemplate(){ + return new RestTemplate(); + } +} \ No newline at end of file diff --git b/client-ribbon/src/main/java/cn/xbz/clientribbon/TestController.java a/client-ribbon/src/main/java/cn/xbz/clientribbon/TestController.java new file mode 100644 index 0000000..de71325 --- /dev/null +++ a/client-ribbon/src/main/java/cn/xbz/clientribbon/TestController.java @@ -0,0 +1,18 @@ +package cn.xbz.clientribbon; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +public class TestController { + + @Autowired + private RestTemplate restTemplate; + + @RequestMapping("/test") + public String test(String user) { + return restTemplate.getForObject("http://service/hello?user=" + user, String.class); + } +} \ No newline at end of file diff --git b/client-ribbon/src/main/resources/application.properties a/client-ribbon/src/main/resources/application.properties new file mode 100644 index 0000000..08a8a4b --- /dev/null +++ a/client-ribbon/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.application.name=client-ribbon + +server.port=8821 + +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ \ No newline at end of file diff --git b/client-security/pom.xml a/client-security/pom.xml new file mode 100644 index 0000000..39f17f4 --- /dev/null +++ a/client-security/pom.xml @@ -0,0 +1,26 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>client-security</artifactId> + <name>client-security</name> + <description>安全认证</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-security</artifactId> + </dependency> + </dependencies> +</project> diff --git b/client-security/src/main/java/cn/xbz/clientsecurity/ClientSecurityApplication.java a/client-security/src/main/java/cn/xbz/clientsecurity/ClientSecurityApplication.java new file mode 100644 index 0000000..5536524 --- /dev/null +++ a/client-security/src/main/java/cn/xbz/clientsecurity/ClientSecurityApplication.java @@ -0,0 +1,30 @@ +package cn.xbz.clientsecurity; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + + +@EnableEurekaClient +@SpringBootApplication +@RestController +public class ClientSecurityApplication { + + public static void main(String[] args) { + SpringApplication.run(ClientSecurityApplication.class, args); + } + + @Value("${spring.application.name}") + private String name; + + @Value("${server.port}") + private Integer port; + + @RequestMapping("/hello") + public String hello(String user) { + return "hello , " + user + " . I am " + name + " , from port " + port; + } +} \ No newline at end of file diff --git b/client-security/src/main/resources/application.properties a/client-security/src/main/resources/application.properties new file mode 100644 index 0000000..85f82ff --- /dev/null +++ a/client-security/src/main/resources/application.properties @@ -0,0 +1,8 @@ +spring.application.name=client-security + +server.port=8861 + +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ + +#安全认证的配置 +security.basic.enabled=true \ No newline at end of file diff --git b/config-client/pom.xml a/config-client/pom.xml new file mode 100644 index 0000000..6dd80bf --- /dev/null +++ a/config-client/pom.xml @@ -0,0 +1,35 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + <artifactId>config-client</artifactId> + <name>config-client</name> + <description>配置中心客户端(即使用配置信息的其他服务)</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-config-client</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-actuator</artifactId> + </dependency> + + </dependencies> +</project> diff --git b/config-client/src/main/java/cn/xbz/configclient/ConfigClientApplication.java a/config-client/src/main/java/cn/xbz/configclient/ConfigClientApplication.java new file mode 100644 index 0000000..565109f --- /dev/null +++ a/config-client/src/main/java/cn/xbz/configclient/ConfigClientApplication.java @@ -0,0 +1,30 @@ +package cn.xbz.configclient; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@EnableEurekaClient +@RestController +@SpringBootApplication +public class ConfigClientApplication { + + public static void main(String[] args) { + SpringApplication.run(ConfigClientApplication.class, args); + } + + + @Value("${environment}") + private String environment; + + @Value("${remark}") + private String remark; + + @RequestMapping("/configInfo") + public String configInfo() { + return "configInfo ~ environment : " + "" + " ; remark : " + remark; + } +} \ No newline at end of file diff --git b/config-client/src/main/resources/bootstrap.properties a/config-client/src/main/resources/bootstrap.properties new file mode 100644 index 0000000..e888481 --- /dev/null +++ a/config-client/src/main/resources/bootstrap.properties @@ -0,0 +1,15 @@ +#bootstrap和application都是spring boot项目的默认配置文件 +#只是bootstrap的优先级较高 . 由于配置信息都是先于项目加载的 , 所以需要写到bootstrap中 + +#此处命名 , 必须与git中文件的文件前缀(client-dev)保持一致 , 否则无法匹配 +spring.application.name=client +server.port=8841 +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ + +spring.cloud.config.uri=http://localhost:8840/ +spring.cloud.config.profile=dev +#远程仓库的分支 +spring.cloud.config.label=master + +#是否需要权限拉去,默认是true,如果不false就不允许你去拉取配置中心Server更新的内容 +#management.security.enabled=false \ No newline at end of file diff --git b/config/pom.xml a/config/pom.xml new file mode 100644 index 0000000..461e53b --- /dev/null +++ a/config/pom.xml @@ -0,0 +1,25 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>config</artifactId> + <name>config</name> + <description>配置中心</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-config-server</artifactId> + </dependency> + </dependencies> +</project> diff --git b/config/src/main/java/cn/xbz/config/ConfigApplication.java a/config/src/main/java/cn/xbz/config/ConfigApplication.java new file mode 100644 index 0000000..b1eb028 --- /dev/null +++ a/config/src/main/java/cn/xbz/config/ConfigApplication.java @@ -0,0 +1,16 @@ +package cn.xbz.config; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.config.server.EnableConfigServer; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; + +@EnableEurekaClient +@EnableConfigServer +@SpringBootApplication +public class ConfigApplication { + + public static void main(String[] args) { + SpringApplication.run(ConfigApplication.class, args); + } +} \ No newline at end of file diff --git b/config/src/main/resources/application.properties a/config/src/main/resources/application.properties new file mode 100644 index 0000000..6547d81 --- /dev/null +++ a/config/src/main/resources/application.properties @@ -0,0 +1,16 @@ +spring.application.name=config +server.port=8840 + +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ + +#服务的git仓库地址 +spring.cloud.config.server.git.uri=https://gitee.com/xingbz/cloud_config_test_server + +##配置文件所在的目录 +#spring.cloud.config.server.git.search-paths=/** +##配置文件所在的分支 +#spring.cloud.config.label=master +##git仓库的用户名 +#spring.cloud.config.username=xbz1210@163.com +##git仓库的密码 +#spring.cloud.config.password=MayunZs0725@Q \ No newline at end of file diff --git b/dashboard/pom.xml a/dashboard/pom.xml new file mode 100644 index 0000000..54102d2 --- /dev/null +++ a/dashboard/pom.xml @@ -0,0 +1,24 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + <artifactId>dashboard</artifactId> + <name>dashboard</name> + <description>hystrix监控板(可独立运行 , 单实例监控)</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-hystrix</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> + </dependency> + </dependencies> +</project> diff --git b/dashboard/src/main/java/cn/xbz/dashboard/DashboardApplication.java a/dashboard/src/main/java/cn/xbz/dashboard/DashboardApplication.java new file mode 100644 index 0000000..43dbc0b --- /dev/null +++ a/dashboard/src/main/java/cn/xbz/dashboard/DashboardApplication.java @@ -0,0 +1,14 @@ +package cn.xbz.dashboard; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; + +@EnableHystrixDashboard +@SpringBootApplication +public class DashboardApplication { + + public static void main(String[] args) { + SpringApplication.run(DashboardApplication.class, args); + } +} \ No newline at end of file diff --git b/dashboard/src/main/resources/application.properties a/dashboard/src/main/resources/application.properties new file mode 100644 index 0000000..b1ce28d --- /dev/null +++ a/dashboard/src/main/resources/application.properties @@ -0,0 +1,3 @@ +spring.application.name=dashboard + +server.port=8839 \ No newline at end of file diff --git b/eureka/pom.xml a/eureka/pom.xml new file mode 100644 index 0000000..73400cb --- /dev/null +++ a/eureka/pom.xml @@ -0,0 +1,21 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>eureka</artifactId> + <name>eureka</name> + <description>服务注册中心</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka-server</artifactId> + </dependency> + </dependencies> +</project> diff --git b/eureka/src/main/java/cn/xbz/eureka/EurekaApplication.java a/eureka/src/main/java/cn/xbz/eureka/EurekaApplication.java new file mode 100644 index 0000000..33f720b --- /dev/null +++ a/eureka/src/main/java/cn/xbz/eureka/EurekaApplication.java @@ -0,0 +1,15 @@ +package cn.xbz.eureka; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; + +/** 添加 @EnableEurekaServeer , 将当前模块声明为 服务注册中心 */ +@EnableEurekaServer +@SpringBootApplication +public class EurekaApplication { + + public static void main(String[] args) { + SpringApplication.run(EurekaApplication.class, args); + } +} \ No newline at end of file diff --git b/eureka/src/main/resources/application.properties a/eureka/src/main/resources/application.properties new file mode 100644 index 0000000..6020c05 --- /dev/null +++ a/eureka/src/main/resources/application.properties @@ -0,0 +1,14 @@ +server.port=8888 + +eureka.instance.hostname=localhost + +#缺省设置是集群注册中心 , 系统也会将自己作为一个服务来尝试注册到中心 , 在单机模式下我们需要禁用它的注册行为 + +#是否将当前模块自身作为服务注册到中心 , 单节点选false +eureka.client.registerWithEureka=false + +#是否从EurekaServer获取注册信息,默认为true。单节点不需要同步其他的EurekaServer节点的数据 +eureka.client.fetchRegistry=false + +#注册中心的访问地址 +eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ \ No newline at end of file diff --git b/pom.xml a/pom.xml new file mode 100644 index 0000000..ecf0fa9 --- /dev/null +++ a/pom.xml @@ -0,0 +1,66 @@ +<?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> + <parent> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-parent</artifactId> + <version>1.5.12.RELEASE</version> + <relativePath/> <!-- lookup parent from repository --> + </parent> + + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + + <modules> + <module>eureka</module><!-- 服务注册中心 --> + <module>service-a</module><!-- 服务提供者A --> + <module>service-b</module><!-- 服务提供者B --> + <module>client-ribbon</module><!-- 服务消费者-ribbon --> + <module>client-feign</module><!-- 服务消费者-feign --> + <module>client-ribbon-hystrix</module><!-- 服务消费者-ribbon-断路器模式 --> + <module>client-feign-hystrix</module><!-- 服务消费者-feign-断路器模式 --> + + <module>config</module><!-- 配置中心 --> + <module>config-client</module><!-- 配置中心实例 --> + <module>zuul</module><!-- 路由 --> + <module>zuul-filter</module><!-- 路由-过滤功能 --> + <module>client-security</module><!-- 安全认证 --> + + <module>dashboard</module><!-- hystrix监控板 --> + <module>turbine</module><!-- 聚合监控 --> + <module>sleuth</module><!-- 日志跟踪 --> + <module>zipkin</module><!-- 服务追踪 --> + <module>zipkin-service</module><!-- 集成zipkin的服务提供者 --> + <module>zipkin-client-sleuth</module><!-- 集成zipkin和sleuth的服务消费者 --> + </modules> + + <properties> + <java.version>1.8</java.version> + <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> + </properties> + + <!-- 统一指定cloud相关组件的依赖版本 --> + <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> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + </plugin> + </plugins> + </build> +</project> \ No newline at end of file diff --git b/service-a/pom.xml a/service-a/pom.xml new file mode 100644 index 0000000..772b36f --- /dev/null +++ a/service-a/pom.xml @@ -0,0 +1,27 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>service-a</artifactId> + <name>service-a</name> + <description>服务提供者</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + + <!-- 使用sleuth时放开 --> + <!--<dependency>--> + <!--<groupId>org.springframework.cloud</groupId>--> + <!--<artifactId>spring-cloud-starter-sleuth</artifactId>--> + <!--</dependency>--> + </dependencies> +</project> diff --git b/service-a/src/main/java/cn/xbz/servicea/ServiceAApplication.java a/service-a/src/main/java/cn/xbz/servicea/ServiceAApplication.java new file mode 100644 index 0000000..27e1bf1 --- /dev/null +++ a/service-a/src/main/java/cn/xbz/servicea/ServiceAApplication.java @@ -0,0 +1,30 @@ +package cn.xbz.servicea; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** 添加@EnableEurekaClient , 将当前模块声明为eureka客户端 , 即注册到服务中心 */ +@EnableEurekaClient +@SpringBootApplication +@RestController +public class ServiceAApplication { + + public static void main(String[] args) { + SpringApplication.run(ServiceAApplication.class, args); + } + + @Value("${spring.application.name}") + private String name; + + @Value("${server.port}") + private Integer port; + + @RequestMapping("/hello") + public String hello(String user) { + return "hello , " + user + " . I am " + name + " , from port " + port; + } +} \ No newline at end of file diff --git b/service-a/src/main/resources/application.properties a/service-a/src/main/resources/application.properties new file mode 100644 index 0000000..f8b0fab --- /dev/null +++ a/service-a/src/main/resources/application.properties @@ -0,0 +1,10 @@ +spring.application.name=service + +server.port=8811 + +#注册中心的访问地址 +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/,1,2 + +#使用sleuth时放开 +#logging.level.roo=INFO +#logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG \ No newline at end of file diff --git b/service-b/pom.xml a/service-b/pom.xml new file mode 100644 index 0000000..338c794 --- /dev/null +++ a/service-b/pom.xml @@ -0,0 +1,21 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>service-b</artifactId> + <name>service-b</name> + <description>服务提供者</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + </dependencies> +</project> diff --git b/service-b/src/main/java/cn/xbz/serviceb/ServiceBApplication.java a/service-b/src/main/java/cn/xbz/serviceb/ServiceBApplication.java new file mode 100644 index 0000000..59398ac --- /dev/null +++ a/service-b/src/main/java/cn/xbz/serviceb/ServiceBApplication.java @@ -0,0 +1,30 @@ +package cn.xbz.serviceb; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** 添加@EnableEurekaClient , 将当前模块声明为eureka客户端 , 即注册到服务中心 */ +@EnableEurekaClient +@SpringBootApplication +@RestController +public class ServiceBApplication { + + public static void main(String[] args) { + SpringApplication.run(ServiceBApplication.class, args); + } + + @Value("${spring.application.name}") + private String name; + + @Value("${server.port}") + private Integer port; + + @RequestMapping("/hello") + public String hello(String user) { + return "hello , " + user + " . I am " + name + " , from port " + port; + } +} \ No newline at end of file diff --git b/service-b/src/main/resources/application.properties a/service-b/src/main/resources/application.properties new file mode 100644 index 0000000..cabebac --- /dev/null +++ a/service-b/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=service + +server.port=8812 + +#注册中心的访问地址 +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ \ No newline at end of file diff --git b/sleuth/pom.xml a/sleuth/pom.xml new file mode 100644 index 0000000..4cdc682 --- /dev/null +++ a/sleuth/pom.xml @@ -0,0 +1,31 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>sleuth</artifactId> + <name>sleuth</name> + <description>日志跟踪</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-ribbon</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-sleuth</artifactId> + </dependency> + </dependencies> +</project> diff --git b/sleuth/src/main/java/cn/xbz/sleuth/SleuthApplication.java a/sleuth/src/main/java/cn/xbz/sleuth/SleuthApplication.java new file mode 100644 index 0000000..92836d6 --- /dev/null +++ a/sleuth/src/main/java/cn/xbz/sleuth/SleuthApplication.java @@ -0,0 +1,23 @@ +package cn.xbz.sleuth; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +@EnableEurekaClient +@SpringBootApplication +public class SleuthApplication { + + public static void main(String[] args) { + SpringApplication.run(SleuthApplication.class, args); + } + + @Bean + @LoadBalanced + public RestTemplate restTemplate(){ + return new RestTemplate(); + } +} \ No newline at end of file diff --git b/sleuth/src/main/java/cn/xbz/sleuth/TestController.java a/sleuth/src/main/java/cn/xbz/sleuth/TestController.java new file mode 100644 index 0000000..0373af6 --- /dev/null +++ a/sleuth/src/main/java/cn/xbz/sleuth/TestController.java @@ -0,0 +1,18 @@ +package cn.xbz.sleuth; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +public class TestController { + + @Autowired + private RestTemplate restTemplate; + + @RequestMapping("/test") + public String test(String user) { + return restTemplate.getForObject("http://service/hello?user=" + user, String.class); + } +} \ No newline at end of file diff --git b/sleuth/src/main/resources/application.properties a/sleuth/src/main/resources/application.properties new file mode 100644 index 0000000..d3f0d63 --- /dev/null +++ a/sleuth/src/main/resources/application.properties @@ -0,0 +1,9 @@ +spring.application.name=sleuth + +server.port=8837 + +#注册中心的访问地址 +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ + +logging.level.roo=INFO +logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG \ No newline at end of file diff --git b/turbine/pom.xml a/turbine/pom.xml new file mode 100644 index 0000000..e13b6a0 --- /dev/null +++ a/turbine/pom.xml @@ -0,0 +1,21 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>turbine</artifactId> + <name>turbine</name> + <description>聚合监控(可独立运行 , 多实例聚合监控)</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-turbine</artifactId> + </dependency> + </dependencies> +</project> diff --git b/turbine/src/main/java/cn/xbz/turbine/TurbineApplication.java a/turbine/src/main/java/cn/xbz/turbine/TurbineApplication.java new file mode 100644 index 0000000..8254d7a --- /dev/null +++ a/turbine/src/main/java/cn/xbz/turbine/TurbineApplication.java @@ -0,0 +1,14 @@ +package cn.xbz.turbine; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.turbine.EnableTurbine; + +@EnableTurbine +@SpringBootApplication +public class TurbineApplication { + + public static void main(String[] args) { + SpringApplication.run(TurbineApplication.class, args); + } +} \ No newline at end of file diff --git b/turbine/src/main/resources/application.properties a/turbine/src/main/resources/application.properties new file mode 100644 index 0000000..9c04240 --- /dev/null +++ a/turbine/src/main/resources/application.properties @@ -0,0 +1,10 @@ +spring.application.name=turbine + +server.port=8838 + +#监控的应用名 +turbine.appConfig=service +#写在""之内 , 或用 new String("") +turbine.clusterNameExpression="default" + +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka \ No newline at end of file diff --git b/zipkin-client-sleuth/pom.xml a/zipkin-client-sleuth/pom.xml new file mode 100644 index 0000000..351b28b --- /dev/null +++ a/zipkin-client-sleuth/pom.xml @@ -0,0 +1,33 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>zipkin-client-sleuth</artifactId> + <name>zipkin-client-sleuth</name> + <description>集成zipkin和sleuth的服务消费者</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-ribbon</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-sleuth</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-zipkin</artifactId> + </dependency> + </dependencies> +</project> diff --git b/zipkin-client-sleuth/src/main/java/cn/xbz/zipkinclientsleuth/TestController.java a/zipkin-client-sleuth/src/main/java/cn/xbz/zipkinclientsleuth/TestController.java new file mode 100644 index 0000000..ca60f09 --- /dev/null +++ a/zipkin-client-sleuth/src/main/java/cn/xbz/zipkinclientsleuth/TestController.java @@ -0,0 +1,18 @@ +package cn.xbz.zipkinclientsleuth; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +@RestController +public class TestController { + + @Autowired + private RestTemplate restTemplate; + + @RequestMapping("/test") + public String test(String user) { + return restTemplate.getForObject("http://zipkin-service/hello?user=" + user, String.class); + } +} \ No newline at end of file diff --git b/zipkin-client-sleuth/src/main/java/cn/xbz/zipkinclientsleuth/ZipkinClientSleuthApplication.java a/zipkin-client-sleuth/src/main/java/cn/xbz/zipkinclientsleuth/ZipkinClientSleuthApplication.java new file mode 100644 index 0000000..7898afe --- /dev/null +++ a/zipkin-client-sleuth/src/main/java/cn/xbz/zipkinclientsleuth/ZipkinClientSleuthApplication.java @@ -0,0 +1,23 @@ +package cn.xbz.zipkinclientsleuth; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +@EnableEurekaClient +@SpringBootApplication +public class ZipkinClientSleuthApplication { + + public static void main(String[] args) { + SpringApplication.run(ZipkinClientSleuthApplication.class, args); + } + + @Bean + @LoadBalanced + public RestTemplate restTemplate(){ + return new RestTemplate(); + } +} \ No newline at end of file diff --git b/zipkin-client-sleuth/src/main/resources/application.properties a/zipkin-client-sleuth/src/main/resources/application.properties new file mode 100644 index 0000000..718a884 --- /dev/null +++ a/zipkin-client-sleuth/src/main/resources/application.properties @@ -0,0 +1,11 @@ +logging.level.roo=INFO +logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG + +spring.application.name=zipkin-client-sleuth + +server.port=8852 + +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ + +#zipkin服务器地址 +spring.zipkin.baseUrl=http://localhost:8850 \ No newline at end of file diff --git b/zipkin-service/pom.xml a/zipkin-service/pom.xml new file mode 100644 index 0000000..6b33429 --- /dev/null +++ a/zipkin-service/pom.xml @@ -0,0 +1,25 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>zipkin-service</artifactId> + <name>zipkin-service</name> + <description>集成zipkin的服务提供者</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-zipkin</artifactId> + </dependency> + </dependencies> +</project> diff --git b/zipkin-service/src/main/java/cn/xbz/zipkinservice/ZipkinServiceApplication.java a/zipkin-service/src/main/java/cn/xbz/zipkinservice/ZipkinServiceApplication.java new file mode 100644 index 0000000..00d1a83 --- /dev/null +++ a/zipkin-service/src/main/java/cn/xbz/zipkinservice/ZipkinServiceApplication.java @@ -0,0 +1,29 @@ +package cn.xbz.zipkinservice; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@EnableEurekaClient +@SpringBootApplication +public class ZipkinServiceApplication { + + public static void main(String[] args) { + SpringApplication.run(ZipkinServiceApplication.class, args); + } + + @Value("${spring.application.name}") + private String name; + + @Value("${server.port}") + private Integer port; + + @RequestMapping("/hello") + public String hello(String user) { + return "hello , " + user + " . I am " + name + " , from port " + port; + } +} \ No newline at end of file diff --git b/zipkin-service/src/main/resources/application.properties a/zipkin-service/src/main/resources/application.properties new file mode 100644 index 0000000..5fae6c2 --- /dev/null +++ a/zipkin-service/src/main/resources/application.properties @@ -0,0 +1,8 @@ +spring.application.name=zipkin-service + +server.port=8851 + +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ + +#zipkin服务器地址 +spring.zipkin.baseUrl=http://localhost:8850 \ No newline at end of file diff --git b/zipkin/pom.xml a/zipkin/pom.xml new file mode 100644 index 0000000..2e2000a --- /dev/null +++ a/zipkin/pom.xml @@ -0,0 +1,28 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>zipkin</artifactId> + <name>zipkin</name> + <description>服务追踪-zipkin</description> + + <dependencies> + <dependency> + <groupId>io.zipkin.java</groupId> + <artifactId>zipkin-server</artifactId> + </dependency> + + <!-- zipkin 可视化 UI --> + <dependency> + <groupId>io.zipkin.java</groupId> + <artifactId>zipkin-autoconfigure-ui</artifactId> + <scope>runtime</scope> + </dependency> + </dependencies> +</project> diff --git b/zipkin/src/main/java/cn/xbz/zipkin/ZipkinApplication.java a/zipkin/src/main/java/cn/xbz/zipkin/ZipkinApplication.java new file mode 100644 index 0000000..5e9730f --- /dev/null +++ a/zipkin/src/main/java/cn/xbz/zipkin/ZipkinApplication.java @@ -0,0 +1,14 @@ +package cn.xbz.zipkin; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import zipkin.server.EnableZipkinServer; + +@EnableZipkinServer +@SpringBootApplication +public class ZipkinApplication { + + public static void main(String[] args) { + SpringApplication.run(ZipkinApplication.class, args); + } +} \ No newline at end of file diff --git b/zipkin/src/main/resources/application.properties a/zipkin/src/main/resources/application.properties new file mode 100644 index 0000000..cd6990e --- /dev/null +++ a/zipkin/src/main/resources/application.properties @@ -0,0 +1,3 @@ +spring.application.name=zipkin + +server.port=8850 \ No newline at end of file diff --git b/zuul-filter/pom.xml a/zuul-filter/pom.xml new file mode 100644 index 0000000..c2067fd --- /dev/null +++ a/zuul-filter/pom.xml @@ -0,0 +1,26 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>zuul-filter</artifactId> + <name>zuul-filter</name> + <description>路由-过滤功能</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-zuul</artifactId> + </dependency> + </dependencies> +</project> diff --git b/zuul-filter/src/main/java/cn/xbz/zuulfilter/MyZuulFilter.java a/zuul-filter/src/main/java/cn/xbz/zuulfilter/MyZuulFilter.java new file mode 100644 index 0000000..902e06e --- /dev/null +++ a/zuul-filter/src/main/java/cn/xbz/zuulfilter/MyZuulFilter.java @@ -0,0 +1,61 @@ +package cn.xbz.zuulfilter; + +import com.netflix.zuul.ZuulFilter; +import com.netflix.zuul.context.RequestContext; +import org.springframework.stereotype.Component; + +import javax.servlet.http.HttpServletRequest; + +@Component +public class MyZuulFilter extends ZuulFilter { + + /** + * 过滤器类型 + * pre : 请求路由之前调用 + * routing : 路由请求时调用 + * error : 处理请求出错时调用 + * post : routing和error过滤器之后调用 + */ + @Override + public String filterType() { + return "pre"; + } + + /** 执行顺序 , 数字越小优先级越高 , 即越先执行 */ + @Override + public int filterOrder() { + return 0; + } + + /** 是否执行当前过滤器 , 返回false则不执行 */ + @Override + public boolean shouldFilter() { + return true; + } + + /** + * 过滤器的具体业务 + * ctx.setSendZuulResponse(false) 不再请求路由 + * ctx.setResponseStatusCode(401) 返回的错误码 + * ctx.setResponseBody(body) 返回的body内容 + */ + @Override + public Object run() { + RequestContext ctx = RequestContext.getCurrentContext(); + HttpServletRequest request = ctx.getRequest(); + System.out.println(String.format("%s ~~~ %s", request.getMethod(), String.valueOf(request.getRequestURL()))); + Object user = request.getParameter("user"); + if (user == null) { + System.err.println("error ! no user !"); + ctx.setSendZuulResponse(false); + ctx.setResponseStatusCode(401); + try { + ctx.getResponse().getWriter().write("error ! no user !"); + } catch (Exception e) { + e.printStackTrace(); + } + } + System.out.println("~ over ~"); + return null; + } +} \ No newline at end of file diff --git b/zuul-filter/src/main/java/cn/xbz/zuulfilter/ZuulFilterApplication.java a/zuul-filter/src/main/java/cn/xbz/zuulfilter/ZuulFilterApplication.java new file mode 100644 index 0000000..e997964 --- /dev/null +++ a/zuul-filter/src/main/java/cn/xbz/zuulfilter/ZuulFilterApplication.java @@ -0,0 +1,22 @@ +package cn.xbz.zuulfilter; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; +import org.springframework.context.annotation.Bean; + +@EnableEurekaClient +@EnableZuulProxy +@SpringBootApplication +public class ZuulFilterApplication { + + public static void main(String[] args) { + SpringApplication.run(ZuulFilterApplication.class, args); + } + + @Bean + public MyZuulFilter initZuulFilter(){ + return new MyZuulFilter(); + } +} \ No newline at end of file diff --git b/zuul-filter/src/main/resources/application.properties a/zuul-filter/src/main/resources/application.properties new file mode 100644 index 0000000..e4d557b --- /dev/null +++ a/zuul-filter/src/main/resources/application.properties @@ -0,0 +1,13 @@ +spring.application.name=zuul-filter + +server.port=9999 + +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ + +#将/zuul-a/路径下的请求分发到client-ribbon服务上 +zuul.routes.zuul-a.path=/zuul-a/** +zuul.routes.zuul-a.serviceId=client-ribbon + +#将/zuul-b/路径下的请求分发到client-feign服务上 +zuul.routes.zuul-b.path=/zuul-b/** +zuul.routes.zuul-b.serviceId=client-feign \ No newline at end of file diff --git b/zuul/pom.xml a/zuul/pom.xml new file mode 100644 index 0000000..2bda845 --- /dev/null +++ a/zuul/pom.xml @@ -0,0 +1,25 @@ +<?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> + <parent> + <groupId>cn.xbz</groupId> + <artifactId>cloud-demo</artifactId> + <version>0.0.1-SNAPSHOT</version> + </parent> + + <artifactId>zuul</artifactId> + <name>zuul</name> + <description>路由</description> + + <dependencies> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-eureka</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.cloud</groupId> + <artifactId>spring-cloud-starter-zuul</artifactId> + </dependency> + </dependencies> +</project> diff --git b/zuul/src/main/java/cn/xbz/zuul/ZuulApplication.java a/zuul/src/main/java/cn/xbz/zuul/ZuulApplication.java new file mode 100644 index 0000000..a15e2af --- /dev/null +++ a/zuul/src/main/java/cn/xbz/zuul/ZuulApplication.java @@ -0,0 +1,16 @@ +package cn.xbz.zuul; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; + +@EnableEurekaClient +@EnableZuulProxy +@SpringBootApplication +public class ZuulApplication { + + public static void main(String[] args) { + SpringApplication.run(ZuulApplication.class, args); + } +} \ No newline at end of file diff --git b/zuul/src/main/resources/application.properties a/zuul/src/main/resources/application.properties new file mode 100644 index 0000000..a805f22 --- /dev/null +++ a/zuul/src/main/resources/application.properties @@ -0,0 +1,13 @@ +spring.application.name=zuul + +server.port=7777 + +eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/ + +#将/zuul-a/路径下的请求分发到client-ribbon服务上 +zuul.routes.zuul-a.path=/zuul-a/** +zuul.routes.zuul-a.serviceId=client-ribbon + +#将/zuul-b/路径下的请求分发到client-feign服务上 +zuul.routes.zuul-b.path=/zuul-b/** +zuul.routes.zuul-b.serviceId=client-feign \ No newline at end of file