spring cloud alibaba [feigen/dubbo] + zipkin实战

一.架构

spring cloud alibaba [feigen/dubbo] + zipkin实战_第1张图片

二.软件安装

1.mysql安装(存放zipkin数据,数据量大可以采用es)

---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: '1'
    k8s.eip.work/ingress: 'false'
    k8s.eip.work/service: NodePort
    k8s.eip.work/workload: mysql
  labels:
    k8s.eip.work/layer: db
    k8s.eip.work/name: mysql
  name: mysql
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s.eip.work/layer: db
      k8s.eip.work/name: mysql
  template:
    metadata:
      labels:
        k8s.eip.work/layer: db
        k8s.eip.work/name: mysql
    spec:
      containers:
        - env:
            - name: MYSQL_ROOT_PASSWORD
              value: '123456'
          image: 'mysql:5.7'
          name: mysql
          ports:
            - containerPort: 3306

---
apiVersion: v1
kind: Service
metadata:
  annotations:
    k8s.eip.work/workload: mysql
  labels:
    k8s.eip.work/layer: db
    k8s.eip.work/name: mysql
  name: mysql
  namespace: default
spec:
  ports:
    - name: dazczs
      nodePort: 31306
      port: 3306
      protocol: TCP
      targetPort: 3306
  selector:
    k8s.eip.work/layer: db
    k8s.eip.work/name: mysql
  type: NodePort


2.zookeeper安装和kafka安装请参考博主另一篇文章

https://blog.csdn.net/gy13477001651/article/details/106502548

3.zipkin安装

---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: '9'
    k8s.eip.work/ingress: 'false'
    k8s.eip.work/service: NodePort
    k8s.eip.work/workload: zipkin
  labels:
    k8s.eip.work/layer: svc
    k8s.eip.work/name: zipkin
  name: zipkin
  namespace: default
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s.eip.work/layer: svc
      k8s.eip.work/name: zipkin
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        k8s.eip.work/layer: svc
        k8s.eip.work/name: zipkin
    spec:
      containers:
        - env:
            - name: MYSQL_HOST
              value: 10.96.234.66
            - name: MYSQL_TCP_PORT
              value: '3306'
            - name: MYSQL_USER
              value: root
            - name: MYSQL_PASS
              value: '123456'
            - name: STORAGE_TYPE
              value: mysql
            - name: KAFKA_BOOTSTRAP_SERVERS
              value: '192.168.100.16:31367'
            - name: KAFKA_TOPIC
              value: zipkintest
          image: 'openzipkin/zipkin:2.19'
          imagePullPolicy: IfNotPresent
          name: zipkin
---
apiVersion: v1
kind: Service
metadata:
  annotations:
    k8s.eip.work/workload: zipkin
  labels:
    k8s.eip.work/layer: svc
    k8s.eip.work/name: zipkin
  name: zipkin
  namespace: default
spec:
  ports:
    - name: w2zrsi
      nodePort: 31977
      port: 9411
      protocol: TCP
      targetPort: 9411
  selector:
    k8s.eip.work/layer: svc
    k8s.eip.work/name: zipkin
  sessionAffinity: None
  type: NodePort



4.nacos安装

---
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: '2'
    k8s.eip.work/ingress: 'false'
    k8s.eip.work/service: NodePort
    k8s.eip.work/workload: nacos
  labels:
    k8s.eip.work/layer: svc
    k8s.eip.work/name: nacos
  name: nacos
  namespace: default
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s.eip.work/layer: svc
      k8s.eip.work/name: nacos
  template:
    metadata:
      creationTimestamp: null
      labels:
        k8s.eip.work/layer: svc
        k8s.eip.work/name: nacos
    spec:
      containers:
        - env:
            - name: MODE
              value: standalone
          image: nacos/nacos-server
          name: nacos
          ports:
            - containerPort: 8848
              protocol: TCP

---
apiVersion: v1
kind: Service
metadata:
  annotations:
    k8s.eip.work/workload: nacos
  labels:
    k8s.eip.work/layer: svc
    k8s.eip.work/name: nacos
  name: nacos
  namespace: default
spec:
  ports:
    - name: m4jegz
      nodePort: 31641
      port: 8848
      protocol: TCP
      targetPort: 8848
  selector:
    k8s.eip.work/layer: svc
    k8s.eip.work/name: nacos
  type: NodePort


三.微服务搭建(feign调用)

1.service-provider

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
://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">
    >4.0.0>
    >
        >org.springframework.boot>
        >spring-boot-starter-parent>
        >2.2.3.RELEASE>
        > <!-- lookup parent from repository -->
    
    com.yufu.cloud
    service-provider
    0.0.1-SNAPSHOT
    service-provider
    Demo project for Spring Boot

    >
        >1.8>
        -cloud-alibaba.version>2.2.1.RELEASE-cloud-alibaba.version>
    >

    >
        >
            >org.springframework.boot>
            >spring-boot-starter-web>
        >
        >
            >com.alibaba.cloud>
            >spring-cloud-starter-alibaba-nacos-discovery>
        >

        >
            >org.projectlombok>
            >lombok>
            >true>
        >
        >
            >org.springframework.boot>
            >spring-boot-starter-test>
            >test>
            >
                >
                    >org.junit.vintage>
                    >junit-vintage-engine>
                >
            >
        >
        >
            >org.springframework.cloud>
            >spring-cloud-starter-zipkin>
            >2.2.0.RELEASE>
        >

    >

    >
        >
            >
                >com.alibaba.cloud>
                >spring-cloud-alibaba-dependencies>
                >${
     spring-cloud-alibaba.version}>
                >pom>
                >import>
            >
        >
    >

    >
        >
            >
                >org.springframework.boot>
                >spring-boot-maven-plugin>
            >
        >
    >

    

>

ServiceProviderApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProviderApplication {
     

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

}

TestController.java

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class TestController {
     

    @GetMapping("test2")
    public String test2(String mer_id, HttpServletRequest request)
    {
     
        return mer_id;
    }
}

application.properties

spring.application.name=service-provider
spring.cloud.nacos.server-addr=192.168.100.16:31641
server.port=8082
spring.sleuth.sampler.rate=100
spring.zipkin.sender.type=kafka
spring.zipkin.kafka.topic=zipkintest
spring.kafka.bootstrap-servers=192.168.100.16:31367

2.service-consumer

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
://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">
    >4.0.0>
    >
        >org.springframework.boot>
        >spring-boot-starter-parent>
        >2.2.3.RELEASE>
        > <!-- lookup parent from repository -->
    
    com.yufu.cloud.serviceconsumer
    serviceconsumer
    0.0.1-SNAPSHOT
    serviceconsumer
    Demo project for Spring Boot

    >
        >1.8>
        -cloud-alibaba.version>2.2.1.RELEASE-cloud-alibaba.version>
    >

    >
        >
            >org.springframework.boot>
            >spring-boot-starter-web>
        >
        >
            >com.alibaba.cloud>
            >spring-cloud-starter-alibaba-nacos-discovery>
        >
        <!---->
            
            
        <!---->

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
            2.2.1.RELEASE
        


        >
            >org.projectlombok>
            >lombok>
            >true>
        >
        >
            >org.springframework.boot>
            >spring-boot-starter-test>
            >test>
            >
                >
                    >org.junit.vintage>
                    >junit-vintage-engine>
                >
            >
        >
        >
            >org.springframework.cloud>
            >spring-cloud-starter-zipkin>
            >2.2.0.RELEASE>
        >

        >
            >org.springframework.kafka>
            >spring-kafka>
        >

    >

    >
        >
            >
                >com.alibaba.cloud>
                >spring-cloud-alibaba-dependencies>
                >${
     spring-cloud-alibaba.version}>
                >pom>
                >import>
            >
        >
    >

    >
        >
            >
                >org.springframework.boot>
                >spring-boot-maven-plugin>
            >
        >
    >

    >
        >
            >spring-milestones>
            >Spring Milestones>
            >https://repo.spring.io/milestone>
        >
        >
            >spring-snapshots>
            >Spring Snapshots>
            >https://repo.spring.io/snapshot>
            >
                >true>
            >
        >
    >
    >
        >
            >spring-milestones>
            >Spring Milestones>
            >https://repo.spring.io/milestone>
        >
        >
            >spring-snapshots>
            >Spring Snapshots>
            >https://repo.spring.io/snapshot>
            >
                >true>
            >
        >
    >

>


ServiceconsumerApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceconsumerApplication {
     

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

}


TestController.java

import com.yufu.cloud.serviceconsumer.serviceconsumer.service.feign.MerService;
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 MerService merService;


    @RequestMapping("test")
    public String test() {
     
        return merService.test2("SAS123456798");
    }
}

MerService.java

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "service-provider")
public interface MerService {
     

    @GetMapping("test2")
    public String test2(@RequestParam("mer_id") String mer_id);

}

application.properties

spring.application.name=service-consumersss
spring.cloud.nacos.server-addr=192.168.100.16:31641
server.port=8081
spring.sleuth.sampler.rate=100
spring.zipkin.sender.type=kafka
spring.zipkin.kafka.topic=zipkintest
spring.kafka.bootstrap-servers=192.168.100.16:31367

3.验证

请求http://192.168.100.209:8081/test后在,zipkin查看,出现信息,说明成功
spring cloud alibaba [feigen/dubbo] + zipkin实战_第2张图片

四.微服务搭建(dubbo调用)

1.service-provider

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
://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">
    >4.0.0>
    >
        >org.springframework.boot>
        >spring-boot-starter-parent>
        >2.2.8.BUILD-SNAPSHOT>
        > <!-- lookup parent from repository -->
    
    com.example
    dubbo-service-provider
    0.0.1-SNAPSHOT
    dubbo-service-provider
    Demo project for Spring Boot

    >
        >1.8>
        -cloud-alibaba.version>2.2.1.RELEASE-cloud-alibaba.version>
    >

    >
        >
            >org.springframework.boot>
            >spring-boot-starter-web>
        >
        >
            >com.alibaba.cloud>
            >spring-cloud-starter-alibaba-nacos-discovery>
        >

        >
            >org.projectlombok>
            >lombok>
            >true>
        >
        >
            >org.springframework.boot>
            >spring-boot-starter-test>
            >test>
            >
                >
                    >org.junit.vintage>
                    >junit-vintage-engine>
                >
            >
        >
        >
            >com.yufu>
            >api>
            >1.0-SNAPSHOT>
        >

        >
            >com.alibaba.cloud>
            >spring-cloud-starter-dubbo>
        >

        >
            >org.springframework.cloud>
            >spring-cloud-starter-zipkin>
            >2.2.0.RELEASE>
        >
        >
            >io.zipkin.brave>
            >brave-instrumentation-dubbo>
            >5.9.5>
        >
        >
            >org.springframework.kafka>
            >spring-kafka>
        >

    >

    >
        >
            >
                >com.alibaba.cloud>
                >spring-cloud-alibaba-dependencies>
                >${
     spring-cloud-alibaba.version}>
                >pom>
                >import>
            >
        >
    >

    >
        >
            >
                >org.springframework.boot>
                >spring-boot-maven-plugin>
            >
        >
    >



>


DubboProviderApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class DubboProviderApplication {
     

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

}

UserServiceImpl.java

import com.example.demo.User;
import org.apache.dubbo.config.annotation.Service;

@Service
public class UserServiceImpl implements UserService {
     
    @Override
    public User getUserById(User user) {
     
        if ("1".equals(user.getId())) {
     
            User resuser = new User();
            resuser.setId(user.getId());
            resuser.setName("郭阳");
            return resuser;
        } else {
     
            User resuser = new User();
            resuser.setId(user.getId());
            resuser.setName("郭阳other");
            return resuser;
        }
    }
}


application.yml

spring:
  application:
    name: dubbo-service-provider
  cloud:
    nacos:
      server-addr: 192.168.100.16:31641
      discovery:
        watch:
          enabled: false
  main:
    allow-bean-definition-overriding: true
  zipkin:
    kafka:
      topic: zipkintest
    sender:
      type: kafka
  sleuth:
    sampler:
      rate: 100
  kafka:
    bootstrap-servers: 192.168.100.16:31367
server:
  port: 8080
dubbo:
  scan:
    base-packages: com.example.demo.service
  registry:
    address: spring-cloud://192.168.100.16
  protocol:
    name: dubbo
    port: -1

  consumer:
    filter: tracing
  provider:
    filter: tracing

debug: true

2.service-consumer

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
://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">
    >4.0.0>
    >
        >org.springframework.boot>
        >spring-boot-starter-parent>
        >2.2.8.BUILD-SNAPSHOT>
        > <!-- lookup parent from repository -->
    
    com.example
    dubbo-service-consumer
    0.0.1-SNAPSHOT
    dubbo-service-consumer
    Demo project for Spring Boot

    >
        >1.8>
        -cloud-alibaba.version>2.2.1.RELEASE-cloud-alibaba.version>
    >

    >
        >
            >org.springframework.boot>
            >spring-boot-starter-web>
        >
        >
            >com.alibaba.cloud>
            >spring-cloud-starter-alibaba-nacos-discovery>
        >

        >
            >org.projectlombok>
            >lombok>
            >true>
        >
        >
            >org.springframework.boot>
            >spring-boot-starter-test>
            >test>
            >
                >
                    >org.junit.vintage>
                    >junit-vintage-engine>
                >
            >
        >
        >
            >com.yufu>
            >api>
            >1.0-SNAPSHOT>
        >
        >
            >com.alibaba.cloud>
            >spring-cloud-starter-dubbo>
        >
        >
            >org.springframework.cloud>
            >spring-cloud-starter-zipkin>
            >2.2.0.RELEASE>
        >
        >
            >io.zipkin.brave>
            >brave-instrumentation-dubbo>
            >5.9.5>
        >
        >
            >org.springframework.kafka>
            >spring-kafka>
        >
    >

    >
        >
            >
                >com.alibaba.cloud>
                >spring-cloud-alibaba-dependencies>
                >${
     spring-cloud-alibaba.version}>
                >pom>
                >import>
            >
        >
    >

    >
        >
            >
                >org.springframework.boot>
                >spring-boot-maven-plugin>
            >
        >
    >


>


DubboConsumerApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class DubboConsumerApplication {
     

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

    }

}



TestController.java

import com.example.demo.User;
import com.example.demo.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
     

    @Reference
    UserService userService;

    @GetMapping("test")
    public User test() {
     
        User user = new User();
        user.setId("1");
        return userService.getUserById(user);
    }
}

application.yml

spring:
  application:
    name: dubbo-service-consumer
  cloud:
    nacos:
      server-addr: 192.168.100.16:31641
      discovery:
        register-enabled: false
  main:
    allow-bean-definition-overriding: true
  zipkin:
    kafka:
      topic: zipkintest
    sender:
      type: kafka
  sleuth:
    sampler:
      rate: 100
  kafka:
    bootstrap-servers: 192.168.100.16:31367
server:
  port: 8081
dubbo:
  registry:
    address: spring-cloud://192.168.100.16
  cloud:
    subscribed-services: dubbo-service-provider
  consumer:
    check: false
    filter: tracing

  provider:
    filter: tracing
debug: true

3.验证

请求http://192.168.100.209:8081/test后在,zipkin查看,出现信息,说明成功
spring cloud alibaba [feigen/dubbo] + zipkin实战_第3张图片

你可能感兴趣的:(spring,cloud,alibaba,spring,boot,cloud)