1.构建服务zipkin
在spring Cloud为F版本的时候,已经不需要自己构建Zipkin Server了,只需要下载jar即可,下载地址:
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
下载并启动该服务。
登陆:http://localhost:9411 可以查看
2.eurka-server服务先启动,服务的注册中心
3.实现服务a
*********服务 eureka-service-a**********
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
com.cxb
springcloud
0.0.1-SNAPSHOT
eureka-service-a
Demo project for Spring Boot
1.8
Finchley.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-zipkin
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.boot
spring-boot-maven-plugin
application.yml 注意这里的zipkin的服务中心是http://localhost:9411 而不是http://localhost:9411/zipkin/ 被这个坑了好一会.
server:
port: 8770
spring:
application:
name: eureka-service-a
zipkin:
base-url: http://localhost:9411
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
IndexController
package com.cxb.springcloud.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class IndexController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getInfo")
public String get() {
return "service a";
}
@RequestMapping("/call")
public String callService(){
return restTemplate.getForObject("http://EUREKA-SERVICE-B/getInfo", String.class);
}
}
启动类EurekaServiceAApplication,注意这里的@EnableDiscoveryClient注解要加上,不然后面报错No instances available for EUREKA-SERVICE-B,被这个也坑了好一会0.0
package com.cxb.springcloud;
import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
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;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class EurekaServiceAApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceAApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@Bean
public Sampler defaultSampler() {
return Sampler.ALWAYS_SAMPLE;
}
}
启动服务a。
4.实现服务b,跟服务a基本一样
*********服务 eureka-service-b**********
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
com.cxb
springcloud
0.0.1-SNAPSHOT
eureka-service-b
Demo project for Spring Boot
1.8
Finchley.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-zipkin
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.boot
spring-boot-maven-plugin
application.yml
server:
port: 8771
spring:
application:
name: eureka-service-b
zipkin:
base-url: http://localhost:9411
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
IndexController
package com.cxb.springcloud.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.logging.Level;
@RestController
public class IndexController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getInfo")
public String get() {
return "service b";
}
@RequestMapping("/call")
public String callService(){
return restTemplate.getForObject("http://EUREKA-SERVICE-A/getInfo",String.class);
}
}
EurekaServiceBApplication启动类
package com.cxb.springcloud;
import brave.sampler.Sampler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
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;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class EurekaServiceBApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceBApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@Bean
public Sampler defaultSampler() {
return Sampler.ALWAYS_SAMPLE;
}
}
启动服务b。
最后测试:服务注册成功。
http://desktop-u2hs0mo:8771/call 调用服务a的接口成功。
http://desktop-u2hs0mo:8770/call 调用服务b的接口成功。
最后去http://localhost:9411/zipkin/中心查看服务之间的关系以及调用情况等。
学习相关代码下载