127.0.0.1 server1
127.0.0.1 server2
127.0.0.1 client
127.0.0.1 feign1
127.0.0.1 zipkin
<?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 https://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>2.3.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>eurekaserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurekaserver</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<!-- 服务追踪 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<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>
spring:
application:
name: eureka-server
zipkin:
discovery-client-enabled: true
baseUrl: http://zipkin:8061
sender:
type: web
sleuth:
sampler:
probability: 1 # 设置抽样采集率为100%,默认为0.1,即10%,这里为了测试
server:
port: 8089
eureka:
instance:
hostname: server1
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://server2:8088/eureka/
server:
enable-self-preservation: false #自我保护机制
eviction-interval-timer-in-ms: 30000 #及时踢出已关停的节点
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
两个服务applicattion的名称一样
spring:
application:
name: eureka-server
zipkin:
discovery-client-enabled: true
baseUrl: http://zipkin:8061
sender:
type: web
sleuth:
sampler:
probability: 1 # 设置抽样采集率为100%,默认为0.1,即10%,这里为了测试
server:
port: 8088
eureka:
instance:
hostname: server2
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://server1:8089/eureka/
server:
enable-self-preservation: false #自我保护机制
eviction-interval-timer-in-ms: 30000 #及时踢出已关停的节点
spring:
application:
name: eureka-server
zipkin:
discovery-client-enabled: true
baseUrl: http://zipkin:8061
sender:
type: web
sleuth:
sampler:
probability: 1 # 设置抽样采集率为100%,默认为0.1,即10%,这里为了测试
server:
port: 8088
eureka:
instance:
hostname: server2
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://server1:8089/eureka/
server:
enable-self-preservation: false #自我保护机制
eviction-interval-timer-in-ms: 30000 #及时踢出已关停的节点
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer2Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer2Application.class, args);
}
}
<?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 https://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>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cl</groupId>
<artifactId>eurekaclient1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurekaclient1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<!-- 服务追踪 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<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>
################ 项目基本配置 ################
server:
port: 8011
servlet:
# 项目访问路径前缀
context-path: /
spring:
application:
# 注册到Eureka的服务名
name: clienttest
zipkin:
discovery-client-enabled: true
baseUrl: http://zipkin:8061
sender:
type: web
sleuth:
sampler:
probability: 1 # 设置抽样采集率为100%,默认为0.1,即10%,这里为了测试
################ Eureka配置 ################
eureka:
client:
service-url:
defaultZone: http://server1:8089/eureka/,http://server2:8088/eureka/
@SpringBootApplication
@EnableEurekaClient
public class Eurekaclient1Application {
public static void main(String[] args) {
SpringApplication.run(Eurekaclient1Application.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@RestController
public class EurekaClientConctroller {
@RequestMapping("/test")
public String test(){
return "i am Client1";
}
}
其中pom和client1一样,只是设置了不同端口
################ 项目基本配置 ################
server:
port: 8012
servlet:
# 项目访问路径前缀
context-path: /
spring:
application:
# 注册到Eureka的服务名
name: clienttest
zipkin:
discovery-client-enabled: true
baseUrl: http://zipkin:8061
sender:
type: web
sleuth:
sampler:
probability: 1 # 设置抽样采集率为100%,默认为0.1,即10%,这里为了测试
################ Eureka配置 ################
eureka:
client:
service-url:
defaultZone: http://server1:8089/eureka/,http://server2:8088/eureka/
@RestController
public class EurekaClientConctroller {
@RequestMapping("/test")
public String test(){
return "i am Client2";
}
}
启动client2,访问注册中心,显示client2已经注册
<?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 https://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>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.fei</groupId>
<artifactId>feign1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feign1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<!-- 服务追踪 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<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>
server:
port: 8085
eureka:
client:
serviceUrl:
defaultZone: http://server1:8089/eureka/,http://server2:8088/eureka/
spring:
application:
name: feign1
zipkin:
discovery-client-enabled: true
baseUrl: http://zipkin:8061/
sender:
type: web
sleuth:
sampler:
probability: 1 # 设置抽样采集率为100%,默认为0.1,即10%,这里为了测试
feign:
hystrix:
enabled: true
package com.fei.feign1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
public class Feign1Application {
public static void main(String[] args) {
SpringApplication.run(Feign1Application.class, args);
}
@Bean
@LoadBalanced
RestTemplate RestTemplate() {
return new RestTemplate();
}
}
其中clienttest是client1和client2注册到Eureka的服务名
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "clienttest")
public interface Feign {
@RequestMapping(value = "/test")
String test();
}
package com.fei.feign1;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class FeigntTestController {
@Autowired
Feign feign;
@Autowired
RestTemplate restTemplate;
@GetMapping("/feignTest")
@HystrixCommand(fallbackMethod = "error")
public String getStr(){
System.out.println(1111111111);
return feign.test();
}
public String error(){
return "errrrrr";
}
@GetMapping("/restTest")
@HystrixCommand(fallbackMethod = "error")
public String getTest(){
System.out.println(123);
return restTemplate.getForObject("http://clienttest/test",String.class);
}
}
package com.fei.feign1;
import org.springframework.stereotype.Component;
@Component
public class HystrixTest implements Feign {
@Override
public String test() {
return "error!!";
}
}
启动后可以看到注册中心已有feign1
此时请求两次http://localhost:8085/feignTest会负载到两个client1和client2上
<?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 https://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>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gateway</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>
<dependencies>
<!-- 服务追踪 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<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>
spring:
application:
name: gateway
zipkin:
discovery-client-enabled: true
baseUrl: http://zipkin:8061
sender:
type: web
sleuth:
sampler:
probability: 1 # 设置抽样采集率为100%,默认为0.1,即10%,这里为了测试
server:
port: 8070
spring:
cloud:
gateway:
routes:
- id: 1
uri: lb://feign1:8085
predicates:
- Path=/
eureka:
instance:
hostname: cloud-gateway-service
client:
serviceUrl:
defaultZone: http://server1:8089/eureka/,http://server2:8088/eureka/
logging:
level:
# log 级别
org.springframework.cloud.gateway: debug
package com.gateway.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableEurekaClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("path_route", r -> r.path("/feignTest")
.uri("http://feign1:8085"))
.route("path_route", r -> r.path("/restTest")
.uri("http://feign1:8085"))
.build();
}
}
启动后再eureka上显示
http://localhost:8070/feignTest 或者http://localhost:8070/restTest
均可以返回
<?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 https://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>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project-name>zipkin-server</project-name>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
<zipkin.version>2.11.9</zipkin.version>
</properties>
<groupId>com.cloud</groupId>
<artifactId>${project-name}</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project-name}</name>
<description>${project-name}</description>
<dependencies>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<version>${zipkin.version}</version>
</dependency>
<!-- 使用消息的方式收集数据(使用rabbitmq)
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-collector-rabbitmq</artifactId>
<version>${zipkin.version}</version>
</dependency>-->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>2.11.9</version>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.3.4.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号-->
<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>
</project>
server:
port: 8061
spring:
application:
name: zipkinserver2
eureka:
instance:
hostname: zipkin
client:
serviceUrl:
defaultZone: http://server1:8089/eureka/,http://server2:8088/eureka/
management:
metrics:
web:
server:
auto-time-requests: false
package com.example.zipkinserver2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import zipkin2.server.internal.EnableZipkinServer;
@SpringBootApplication
@EnableEurekaClient
@EnableZipkinServer
public class Zipkinserver2Application {
public static void main(String[] args) {
SpringApplication.run(Zipkinserver2Application.class, args);
}
}
启动后查看注册中心zipkin已经注册
请求http://localhost:8070/restTest后在请求zipkin服务http://localhost:8061