java version of spring cloud distributed micro-services (seven) Spring Cloud Sleuth-b2b2c small e-commerce program

First, build the project
case this paper there are three main project components: a server-zipkin, its main role using ZipkinServer functions, collect call data and display; a service-hi, the external exposure hi interfaces; a service-miya, Foreign miya exposed interfaces; both service can call each other; and only called, server-zipkin will collect the data, which is why the calls traced. Learn springcloud architecture can be added to beg: Er Siqi Liu Er San Sanwu Wujiu
Second, to build server-zipkin
build a spring-boot project named server-zipkin, introducing rely on its pom:


<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</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-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>io.zipkin.java</groupId>
			<artifactId>zipkin-server</artifactId>
		</dependency>

		<dependency>
			<groupId>io.zipkin.java</groupId>
			<artifactId>zipkin-autoconfigure-ui</artifactId>
		</dependency>

	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Camden.SR6</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

Program entry in its class, annotate @EnableZipkinServer, open ZipkinServer features:

@SpringBootApplication
@EnableZipkinServer
public class ServerZipkinApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServerZipkinApplication.class, args);
	}
}



In the configuration file application.yml designated port for the service:

server.port=9411

Third, create a service-hi
incorporated start dependent spring-cloud-starter-zipkin thereon pom, code is as follows:

<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--compile('org.springframework.cloud:spring-cloud-starter-zipkin')-->

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zipkin</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RC1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>



Specified in its configuration file application.yml zipkin server address, by arranging the head "spring.zipkin.base-url" specifies:

@SpringBootApplication
@EnableZipkinServer
public class ServerZipkinApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServerZipkinApplication.class, args);
	}
}


By introducing a spring-cloud-starter-zipkin provided spring.zipkin.base-url and dependent on it.

External exposure Interface:

@SpringBootApplication
@RestController
public class ServiceHiApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceHiApplication.class, args);
	}

	private static final Logger LOG = Logger.getLogger(ServiceHiApplication.class.getName());


	@Autowired
	private RestTemplate restTemplate;

	@Bean
	public RestTemplate getRestTemplate(){
		return new RestTemplate();
	}

	@RequestMapping("/hi")
	public String callHome(){
		LOG.log(Level.INFO, "calling trace service-hi  ");
		return restTemplate.getForObject("http://localhost:8989/miya", String.class);
	}
	@RequestMapping("/info")
	public String info(){
		LOG.log(Level.INFO, "calling trace service-hi ");

		return "i'm service-hi";

	}

	@Bean
	public AlwaysSampler defaultSampler(){
		return new AlwaysSampler();
	}
}

Fourth, the service-miya creating
process of creating pain service-hi, introduced into the same dependence configuration spring.zipkin.base-url.

External exposure Interface:


@SpringBootApplication
@RestController
public class ServiceMiyaApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceMiyaApplication.class, args);
	}

	private static final Logger LOG = Logger.getLogger(ServiceMiyaApplication.class.getName());


	@RequestMapping("/hi")
	public String home(){
		LOG.log(Level.INFO, "hi is being called");
		return "hi i'm miya!";
	}

	@RequestMapping("/miya")
	public String info(){
		LOG.log(Level.INFO, "info is being called");
		return restTemplate.getForObject("http://localhost:8988/info",String.class);
	}

	@Autowired
	private RestTemplate restTemplate;

	@Bean
	public RestTemplate getRestTemplate(){
		return new RestTemplate();
	}
}


Specified in its configuration file application.yml zipkin server address, by arranging the head "spring.zipkin.base-url" specifies:


server.port=8988
spring.zipkin.base-url=http://localhost:9411
spring.application.name=service-hi


By introducing a spring-cloud-starter-zipkin provided spring.zipkin.base-url and dependent on it.

External exposure Interface:

@SpringBootApplication
@RestController
public class ServiceHiApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceHiApplication.class, args);
	}

	private static final Logger LOG = Logger.getLogger(ServiceHiApplication.class.getName());


	@Autowired
	private RestTemplate restTemplate;

	@Bean
	public RestTemplate getRestTemplate(){
		return new RestTemplate();
	}

	@RequestMapping("/hi")
	public String callHome(){
		LOG.log(Level.INFO, "calling trace service-hi  ");
		return restTemplate.getForObject("http://localhost:8989/miya", String.class);
	}
	@RequestMapping("/info")
	public String info(){
		LOG.log(Level.INFO, "calling trace service-hi ");

		return "i'm service-hi";

	}

	@Bean
	public AlwaysSampler defaultSampler(){
		return new AlwaysSampler();
	}
}

Start project, demonstration track
in turn starts the above three projects, open the browser to access: http: // localhost: 9411 / , will appear the following interface:
Here Insert Picture Description
Access: http: // localhost: 8989 / miya, the browser appears:

i’m service-hi

Then open http: // localhost: 9411 / interface, click Dependencies, dependency services can be found:
Here Insert Picture Description
Click find traces, you can see specific data service call each other:

Here Insert Picture Description
Here Insert Picture Description

Released nine original articles · won praise 31 · views 387

Guess you like

Origin blog.csdn.net/m0_46413263/article/details/104900309