Commit 2507ae2e by Xingbz

项目初始化

parents
<?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>
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
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
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
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
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
<?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>
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
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
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
spring.application.name=client-feign
server.port=8822
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
\ No newline at end of file
<?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>
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
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
spring.application.name=client-ribbon-hystrix
server.port=8831
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
\ No newline at end of file
<?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>
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
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
spring.application.name=client-ribbon
server.port=8821
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
\ No newline at end of file
<?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>
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
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
<?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>
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
#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
<?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>
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
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
<?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>
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
spring.application.name=dashboard
server.port=8839
\ No newline at end of file
<?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>
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
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
<?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
<?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>
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
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
<?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>
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
spring.application.name=service
server.port=8812
#注册中心的访问地址
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
\ No newline at end of file
<?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>
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
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
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
<?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>
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
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
<?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>
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
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
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
<?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>
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
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
<?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>
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
spring.application.name=zipkin
server.port=8850
\ No newline at end of file
<?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>
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
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
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
<?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>
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
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment