;annotatedConsumer")
public class GreetingServiceConsumer {
// 这里特别注意的是、由于当前 Dubbo 版本受限于 istio 的通信模型无法获取接口所对应的应用名,
// 因此需要配置 providedBy 参数来标记此服务来自哪个应用。
@DubboReference(version = "1.0.0", providedBy = "dubbo-samples-xds-provider")
private GreetingService greetingService;
public String doSayHello(String name) {
return greetingService.sayHello(name);
}
}
编写启动类
public class ConsumerBootstrap {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
context.start();
GreetingServiceConsumer greetingServiceConsumer = context.getBean(GreetingServiceConsumer.class);
while (true) {
try {
String hello = greetingServiceConsumer.doSayHello("xDS Consumer");
System.out.println("result: " + hello);
Thread.sleep(100);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
@Configuration
@EnableDubbo(scanBasePackages = "org.apache.dubbo.samples.action")
@PropertySource("classpath:/spring/dubbo-consumer.properties")
@ComponentScan(value = {"org.apache.dubbo.samples.action"})
static class ConsumerConfiguration {
}
}
编写配置信息
dubbo.application.name=dubbo-samples-xds-consumer
dubbo.application.metadataServicePort=20885
dubbo.registry.address=xds://istiod.istio-system.svc:15012
dubbo.consumer.timeout=3000
dubbo.consumer.check=false
dubbo.application.qosEnable=true
dubbo.application.qosAcceptForeignIp=true
编写Deployment.yml和Service.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: dubbo-samples-xds-consumer
namespace: dubbo-demo
spec:
replicas: 2
selector:
matchLabels:
app: dubbo-samples-xds-consumer
template:
metadata:
labels:
app: dubbo-samples-xds-consumer
spec:
containers:
- name: server
image: apache/dubbo-demo:dubbo-samples-xds-consumer_0.0.1
livenessProbe:
httpGet:
path: /live
port: 22222
initialDelaySeconds: 5
periodSeconds: 5
readinessProbe:
httpGet:
path: /ready
port: 22222
initialDelaySeconds: 5
periodSeconds: 5
startupProbe:
httpGet:
path: /startup
port: 22222
failureThreshold: 30
periodSeconds: 10
apiVersion: v1
kind: Service
metadata:
name: dubbo-samples-xds-consumer
namespace: dubbo-demo
spec:
clusterIP: None
selector:
app: dubbo-samples-xds-consumer
ports:
- name: grpc
protocol: TCP
port: 50051
targetPort: 50051
编写Dockerfile
FROM openjdk:8-jdk
ADD ./target/dubbo-samples-xds-consumer-1.0-SNAPSHOT.jar dubbo-samples-xds-consumer-1.0-SNAPSHOT.jar
CMD java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=31000 /dubbo-samples-xds-consumer-1.0-SNAPSHOT.jar
? 到目前为止我们的环境和代码就全都准备完毕了!
构建镜像
1、启动docker
2、启动minikube
因为minikube是一个本地的k8s,他启动需要一个虚拟引擎,这里我们用docker来管理。我们通过如下命令启动
minikube start
我们可以在docker里看到minikube
3、检查istio的状态
4、构建镜像
在本地找到代码所在位置、依次执行以下命令
# 找到provider所在路径
cd ./dubbo-samples-xds-provider/
# 构建provider的镜像
docker build -t apache/dubbo-demo:dubbo-samples-xds-provider_0.0.1 .
# 找到consumer所在路径
cd ../dubbo-samples-xds-consumer/
# 构建consumer的镜像
docker build -t apache/dubbo-demo:dubbo-samples-xds-consumer_0.0.1 .
5、检查本地镜像
6、创建namespace
# 初始化命名空间
kubectl apply -f https://r