# springcloud
**Repository Path**: wwwlwww/springcloud
## Basic Information
- **Project Name**: springcloud
- **Description**: 基于springcloud、fegin、zipkin实现的一个服务注册、发现、服务链路追踪的例子
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 2
- **Forks**: 0
- **Created**: 2017-10-24
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# springcloud
基于springcloud、fegin、zipkin实现的一个服务注册、发现、服务链路追踪的例子
bizService:服务提供者
ConsumerService:服务消费者
springCloudEureka:服务注册中心
zipkinServer:服务链路追踪


详细说明:
* 一、服务注册中心
1.pom.xml
```
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-test
test
```
2.Application
```
@SpringBootApplication
@EnableEurekaServer
//@EnableDiscoveryClient
/**
* 服务注册发现中心
*/
public class SpringCloudEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaApplication.class, args);
}
}
```
3.application.properties
```
server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
spring.application.name=eureka-serve
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
```
* 二、服务提供
1.pom.xml
```
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-test
test
```
2.Application
```
/**
* 服务提供
*/
@SpringBootApplication
//启用服务注册与发现
@EnableDiscoveryClient
//启用feign进行远程调用
@EnableFeignClients
public class BizServiceApplication {
public static void main(String[] args) {
SpringApplication.run(BizServiceApplication.class, args);
}
}
```
3.application.properties
```
spring.application.name=spring-cloud-producer
server.port=8001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
```
4.定义服务提供Controller
```
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return "hello " + name + ",this is first messge";
}
}
```
* 三、服务调用
1.pom.xml
```
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-feign
```
2.Application
```
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerServiceApplication.class, args);
}
}
```
3.application.properties
```
spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
```
4.定义远程调用服务接口
```
@FeignClient(name= "spring-cloud-producer")
@Component
public interface HelloRemote {
@RequestMapping(value = "/hello")
String hello(@RequestParam(value = "name") String name);
}
```
5.Controller
```
@RestController
public class ConsumerController {
@Autowired
private HelloRemote helloRemote;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return helloRemote.hello(name);
}
}
```
* 四、服务链路追踪
服务提供者和消费者配置
1、pom.xml
```
org.springframework.cloud
spring-cloud-starter-zipkin
```
2、在application.properties追加下面配置
```
#配置zipKin Server的地址
spring.zipkin.base-url=http://127.0.0.1:9411
```