spring cloud alibaba [feigen/dubbo] + zipkin实战
一.架构二.软件安装1.mysql安装---apiVersion: apps/v1kind: Deploymentmetadata:annotations:deployment.kubernetes.io/revision: '1'k8s.eip.work/ingress: 'false'k8s.eip.work/service: NodePortk8s.eip.work/workload: my
·
一.架构
二.软件安装
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"?>
<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.2.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yufu.cloud</groupId>
<artifactId>service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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-starter-zipkin</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.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>
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"?>
<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.2.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yufu.cloud.serviceconsumer</groupId>
<artifactId>serviceconsumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>serviceconsumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.cloud</groupId>-->
<!--<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>-->
<!--</dependency>-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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-starter-zipkin</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
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查看,出现信息,说明成功
四.微服务搭建(dubbo调用)
1.service-provider
pom.xml
<?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.2.8.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>dubbo-service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-service-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>com.yufu</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-dubbo</artifactId>
<version>5.9.5</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.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>
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"?>
<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.2.8.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>dubbo-service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-service-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>com.yufu</groupId>
<artifactId>api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-dubbo</artifactId>
<version>5.9.5</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.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>
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查看,出现信息,说明成功
更多推荐
已为社区贡献1条内容
所有评论(0)