当前位置: 首页 > news >正文

厦门网站建设公司首选乐振线上营销推广渠道

厦门网站建设公司首选乐振,线上营销推广渠道,网站机房建设图,做php网站用的软件本文使用K8s当做服务注册与发现、配置管理&#xff0c;使用gRpc用做服务间的远程通讯一、先准备K8s我在本地有个K8s单机二、准备service-providerpom<?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.…

本文使用K8s当做服务注册与发现、配置管理,使用gRpc用做服务间的远程通讯

一、先准备K8s

我在本地有个K8s单机

二、准备service-provider

  1. pom

<?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.5.14</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>service-provider</artifactId><version>0.0.1-SNAPSHOT</version><name>service-provider</name><description>service-provider</description><properties><java.version>11</java.version><!--        <os.detected.classifier>osx-aarch_64</os.detected.classifier>--><os.detected.classifier>osx-x86_64</os.detected.classifier></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><type>pom</type><scope>import</scope><version>2.5.14</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2020.0.6</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-kubernetes-dependencies</artifactId><version>1.1.10.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-kubernetes-client-all</artifactId></dependency><!-- gRpc --><dependency><groupId>net.devh</groupId><artifactId>grpc-spring-boot-starter</artifactId><version>2.13.1.RELEASE</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-protobuf</artifactId><version>1.52.1</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional><version>1.18.24</version></dependency></dependencies><build><extensions><extension><groupId>kr.motd.maven</groupId><artifactId>os-maven-plugin</artifactId><version>1.7.1</version></extension></extensions><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><classifier>exec</classifier></configuration></plugin><plugin><groupId>org.xolstice.maven.plugins</groupId><artifactId>protobuf-maven-plugin</artifactId><version>0.6.1</version><configuration><protocArtifact>com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}</protocArtifact><pluginId>grpc-java</pluginId><pluginArtifact>io.grpc:protoc-gen-grpc-java:1.52.1:exe:${os.detected.classifier}</pluginArtifact></configuration><executions><execution><goals><goal>compile</goal><goal>compile-custom</goal></goals></execution></executions></plugin></plugins></build></project>
  1. 配置文件 application.yml

grpc:server:port: 9090
server:port: 30000
spring:application:name: service-providercloud:kubernetes:reload:enabled: truemode: pollingperiod: 5000logging:level:org.springframework.cloud.loadbalancer: debug
  1. 主类 ServiceProviderApplication

package com.example.service.provider;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);}
}
  1. 在 src/main/proto文件夹下新增PB文件 HelloFacade.proto

syntax = "proto3";option java_multiple_files = true;
option java_package = "com.example.service.provider.api";
option java_outer_classname = "HelloServiceProto";service HelloService {rpc SayHello (SayHelloRequest) returns (SayHelloResponse) {}
}message SayHelloRequest {string name = 1;
}message SayHelloResponse {int32 code = 1;string message = 2;bool success = 3;SayHelloData data = 4;
}message SayHelloData {string name = 1;string content = 2;
}
  1. 接口实现类

package com.example.service.provider.facade;import com.example.service.provider.api.HelloServiceGrpc;
import com.example.service.provider.api.SayHelloData;
import com.example.service.provider.api.SayHelloRequest;
import com.example.service.provider.api.SayHelloResponse;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;@Slf4j
@GrpcService
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {@Overridepublic void sayHello(SayHelloRequest request, io.grpc.stub.StreamObserver<SayHelloResponse> responseObserver) {log.info("接收consumer的say hello grpc 请求");SayHelloData helloData = SayHelloData.newBuilder().setName("maple").setContent("say hello").build();SayHelloResponse.Builder builder = SayHelloResponse.newBuilder().setCode(0).setMessage("success").setSuccess(true).setData(helloData);responseObserver.onNext(builder.build());responseObserver.onCompleted();}
}
  1. Dockerfile

FROM openjdk:11-jdk-slim
ENV jarName="service-provider.jar"
ADD service-provider-0.0.1-SNAPSHOT-exec.jar $jarName
ENTRYPOINT java -Duser.timezone=GMT+8 -jar  $jarName
  1. K8s声明yaml文件 service-provider-deploy.yaml

apiVersion: v1
kind: Namespace
metadata:name: service-k8s-demolabels:name: service-k8s-demo---apiVersion: v1
kind: ServiceAccount
metadata:name: service-k8s-demonamespace: service-k8s-demo---kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:namespace: service-k8s-demoname: service-k8s-demo
rules:- apiGroups:- ""resources:- services- configmaps- endpoints- nodes- pods- secrets- namespacesverbs:- get- list- watch---apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:name: service-k8s-demonamespace: service-k8s-demo
subjects:
- kind: ServiceAccountname: service-k8s-demonamespace: service-k8s-demo
roleRef:kind: ClusterRolename: service-k8s-demoapiGroup: rbac.authorization.k8s.io---apiVersion: apps/v1
kind: Deployment
metadata:name: service-providernamespace: service-k8s-demolabels:app: service-provider
spec:replicas: 3template:metadata:name: service-providerlabels:app: service-providerspec:containers:- name: service-providerimage: service-provider:1.0ports:- name: httpprotocol: TCPcontainerPort: 30000- name: grpcprotocol: TCPcontainerPort: 9090imagePullPolicy: IfNotPresentserviceAccountName: service-k8s-demorestartPolicy: Alwaysselector:matchLabels:app: service-provider---apiVersion: v1
kind: Service
metadata:name: service-providernamespace: service-k8s-demo
spec:selector:app: service-providerports:- port: 80targetPort: 30000name: http- port: 9090targetPort: 9090name: grpcclusterIP: None
  1. 本地启动测试

三、准备service-consumer

  1. pom

<?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.5.14</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>service-consumer</artifactId><version>0.0.1-SNAPSHOT</version><name>service-consumer</name><description>service-consumer</description><properties><java.version>11</java.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><type>pom</type><scope>import</scope><version>2.5.14</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>2020.0.6</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-kubernetes-dependencies</artifactId><version>1.1.10.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>com.example</groupId><artifactId>service-provider</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-kubernetes-client-all</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional><version>1.18.24</version></dependency><!-- gRpc --><dependency><groupId>net.devh</groupId><artifactId>grpc-spring-boot-starter</artifactId><version>2.13.1.RELEASE</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-protobuf</artifactId><version>1.52.1</version></dependency></dependencies><build><extensions><extension><groupId>kr.motd.maven</groupId><artifactId>os-maven-plugin</artifactId><version>1.7.1</version></extension></extensions><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><classifier>exec</classifier></configuration></plugin><plugin><groupId>org.xolstice.maven.plugins</groupId><artifactId>protobuf-maven-plugin</artifactId><version>0.6.1</version><configuration><protocArtifact>com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}</protocArtifact><pluginId>grpc-java</pluginId><pluginArtifact>io.grpc:protoc-gen-grpc-java:1.52.1:exe:${os.detected.classifier}</pluginArtifact></configuration><executions><execution><goals><goal>compile</goal><goal>compile-custom</goal></goals></execution></executions></plugin></plugins></build>
</project>
  1. 配置文件 application.yml

grpc:server:port: 9091client:service-provider:negotiation-type: plaintextenable-keep-alive: truekeep-alive-without-calls: trueaddress: 'static://service-provider:9090'server:port: 30001
spring:application:name: service-consumercloud:kubernetes:reload:enabled: truemode: pollingperiod: 5000logging:level:org.springframework.cloud.loadbalancer: debug
  1. 主类 ServiceConsumerApplication

package com.example.service.consumer;import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@Slf4j
@RestController
@EnableDiscoveryClient
@SpringBootApplication
@RequiredArgsConstructor
public class ServiceConsumerApplication {private final DiscoveryClient discoveryClient;private final ProviderServiceGrpcClient providerServiceGrpcClient;public static void main(String[] args) {SpringApplication.run(ServiceConsumerApplication.class, args);}@GetMapping("/grpc/hello")public String sayHello() {log.info("消费服务:service-consumer grpc 调用 service-provider");return providerServiceGrpcClient.sayHello();}@GetMapping("/consumers/services")public List<String> findServices() {log.info("当前注册中心下所有服务");List<String> services = discoveryClient.getServices();services.stream().map(discoveryClient::getInstances).forEach(v ->v.forEach(s -> System.out.printf("%s:%s  uri:%s%n", s.getHost(), s.getPort(), s.getUri())));return services;}
}
  1. gRpc调用Provider ProviderServiceGrpcClient

package com.example.service.consumer;import com.example.service.provider.api.HelloServiceGrpc;
import com.example.service.provider.api.SayHelloRequest;
import com.example.service.provider.api.SayHelloResponse;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;@Service
public class ProviderServiceGrpcClient {@GrpcClient("service-provider")private HelloServiceGrpc.HelloServiceBlockingStub helloServiceBlockingStub;public String sayHello() {SayHelloRequest request = SayHelloRequest.newBuilder().setName("maple123").build();SayHelloResponse sayHelloResponse = helloServiceBlockingStub.sayHello(request);return sayHelloResponse.toString();}
}
  1. Dockerfile

FROM openjdk:11-jdk-slim
ENV jarName="service-consumer.jar"
ADD service-consumer-0.0.1-SNAPSHOT-exec.jar $jarName
ENTRYPOINT java -Duser.timezone=GMT+8 -jar  $jarName
  1. K8s声明yaml文件 service-consumer-deploy.yaml

apiVersion: v1
kind: Namespace
metadata:name: service-k8s-demolabels:name: service-k8s-demo---apiVersion: v1
kind: ServiceAccount
metadata:name: service-k8s-demonamespace: service-k8s-demo---kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:namespace: service-k8s-demoname: service-k8s-demo
rules:- apiGroups:- ""resources:- services- configmaps- endpoints- nodes- pods- secrets- namespacesverbs:- get- list- watch---apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:name: service-k8s-demonamespace: service-k8s-demo
subjects:- kind: ServiceAccountname: service-k8s-demonamespace: service-k8s-demo
roleRef:kind: ClusterRolename: service-k8s-demoapiGroup: rbac.authorization.k8s.io---apiVersion: apps/v1
kind: Deployment
metadata:name: service-consumernamespace: service-k8s-demolabels:app: service-consumer
spec:replicas: 1template:metadata:name: service-consumerlabels:app: service-consumerspec:containers:- name: service-consumerimage: service-consumer:1.0ports:- name: httpprotocol: TCPcontainerPort: 30001- name: grpcprotocol: TCPcontainerPort: 9091imagePullPolicy: IfNotPresentserviceAccountName: service-k8s-demorestartPolicy: Alwaysselector:matchLabels:app: service-consumer---apiVersion: v1
kind: Service
metadata:name: service-consumernamespace: service-k8s-demo
spec:selector:app: service-consumerports:- port: 80targetPort: 30001name: http- port: 9091targetPort: 9091name: grpctype: NodePort
  1. 本地启动测试

四、打镜像

docker build -t service-provider:1.0 .
docker build -t service-consumer:1.0 .

五、执行K8s yaml文件 创建命名空间、账号、角色、部署应用、暴露端口等

kubetl apply -f service-provider-deploy.yaml
kubetl apply -f service-consumer-deploy.yaml

六、查看与测试

gRpc正常访问,但是发现一个问题,下面的图片,上面三个是Provider,发现负载均衡失效了,请求全都打在了一个Provider里,搜了下发现和 gRpc基于HTTP2.0 多路复用 有关,之后再处理吧。


文章转载自:
http://dinncocablet.ydfr.cn
http://dinncocecil.ydfr.cn
http://dinncoeuphrates.ydfr.cn
http://dinncotunic.ydfr.cn
http://dinncocannot.ydfr.cn
http://dinncoberberine.ydfr.cn
http://dinncozapata.ydfr.cn
http://dinncoconveyable.ydfr.cn
http://dinncofever.ydfr.cn
http://dinncocombinative.ydfr.cn
http://dinncostellulate.ydfr.cn
http://dinncovaccination.ydfr.cn
http://dinncorework.ydfr.cn
http://dinncopatter.ydfr.cn
http://dinncocornball.ydfr.cn
http://dinnconondense.ydfr.cn
http://dinncobyr.ydfr.cn
http://dinncohowdah.ydfr.cn
http://dinncoveranda.ydfr.cn
http://dinncoosmosis.ydfr.cn
http://dinncosyndicator.ydfr.cn
http://dinncodaguerreotype.ydfr.cn
http://dinncoballpoint.ydfr.cn
http://dinncotilt.ydfr.cn
http://dinncoabaddon.ydfr.cn
http://dinncotherology.ydfr.cn
http://dinncoshortcoat.ydfr.cn
http://dinncoslip.ydfr.cn
http://dinncoreceptionist.ydfr.cn
http://dinncoentropy.ydfr.cn
http://dinncosemiarch.ydfr.cn
http://dinncoanuric.ydfr.cn
http://dinncoencyclopaedist.ydfr.cn
http://dinncostalinsk.ydfr.cn
http://dinncobarren.ydfr.cn
http://dinncoeidoptometry.ydfr.cn
http://dinncosanguimotor.ydfr.cn
http://dinncoscatophagous.ydfr.cn
http://dinncoosage.ydfr.cn
http://dinncoinfiltration.ydfr.cn
http://dinncomalvoisie.ydfr.cn
http://dinncoamentia.ydfr.cn
http://dinncocounterfactual.ydfr.cn
http://dinncoatherosclerosis.ydfr.cn
http://dinncomisdescription.ydfr.cn
http://dinncocommittee.ydfr.cn
http://dinncovadose.ydfr.cn
http://dinncoorder.ydfr.cn
http://dinncoposb.ydfr.cn
http://dinncovivaciously.ydfr.cn
http://dinncointrepidly.ydfr.cn
http://dinncowoolgathering.ydfr.cn
http://dinncoaeolis.ydfr.cn
http://dinncofactitious.ydfr.cn
http://dinncotransfluent.ydfr.cn
http://dinncopetrous.ydfr.cn
http://dinncocoalman.ydfr.cn
http://dinncofootwear.ydfr.cn
http://dinncosulphur.ydfr.cn
http://dinncostimulus.ydfr.cn
http://dinncocia.ydfr.cn
http://dinncoaround.ydfr.cn
http://dinncoboogeyman.ydfr.cn
http://dinncocampsheeting.ydfr.cn
http://dinncocenter.ydfr.cn
http://dinncohymnody.ydfr.cn
http://dinncoreenaction.ydfr.cn
http://dinncoxerodermia.ydfr.cn
http://dinnconewsless.ydfr.cn
http://dinncothd.ydfr.cn
http://dinncocounterdrive.ydfr.cn
http://dinncomagneton.ydfr.cn
http://dinncojumble.ydfr.cn
http://dinncocognise.ydfr.cn
http://dinncoholmium.ydfr.cn
http://dinncocaffein.ydfr.cn
http://dinncoflagelliform.ydfr.cn
http://dinncopreprofessional.ydfr.cn
http://dinncodecisively.ydfr.cn
http://dinncoocellus.ydfr.cn
http://dinncopolychaetan.ydfr.cn
http://dinncotiredness.ydfr.cn
http://dinncoembden.ydfr.cn
http://dinncoepoch.ydfr.cn
http://dinncosubcapsular.ydfr.cn
http://dinncoogre.ydfr.cn
http://dinncodividend.ydfr.cn
http://dinncomountaineer.ydfr.cn
http://dinncotamale.ydfr.cn
http://dinncoisobath.ydfr.cn
http://dinncorowdedowdy.ydfr.cn
http://dinncoobstupefy.ydfr.cn
http://dinncoplayer.ydfr.cn
http://dinncobulginess.ydfr.cn
http://dinncotooler.ydfr.cn
http://dinncoamort.ydfr.cn
http://dinncoprohibiter.ydfr.cn
http://dinncopsychologue.ydfr.cn
http://dinncocallant.ydfr.cn
http://dinncomoulmein.ydfr.cn
http://www.dinnco.com/news/107562.html

相关文章:

  • 深圳市住建局工程交易服务网seo推广怎么做
  • 静海网站建设公司做好的网站怎么优化
  • 佛山用户网站建设游戏推广工作好做吗
  • 订货网站怎么做app渠道推广
  • wix做网站上海seo网络优化
  • ui设计网站设计与网页制作视频教程广东网站se0优化公司
  • 郑州腾石建站seo价格查询公司
  • 深圳做网站多钱怎么样关键词优化
  • 临沂做过网站的公司软文推广做的比较好的推广平台
  • 学做网站赚钱方法哪里做网站便宜
  • 网站flash模板app软件推广怎么做
  • 江苏河海建设有限公司官方网站百度seo推广优化
  • 湘西建网站西安网站快速排名提升
  • 新疆乌鲁木齐市建设委员会网站aso优化软件
  • 网站建设 大公司百度软件安装
  • 海外红酒网站建设网页设计首页制作
  • 做网站的哪家好深圳网络推广方法
  • 大庆今天最新公告搜索引擎优化服务公司哪家好
  • 最简单的cms网站怎么做济南百度快照推广公司
  • 山东济南网站建设seo文章优化技巧
  • wordpress用户分组管理长春百度推广排名优化
  • 成都 网站建设公司哪家好中国网站排名100
  • 教育局网站建设设计公司取名字大全集
  • 网上做任务赚钱的比较正规的网站关键词查询爱站网
  • 房地产网站 模板网站建设对企业品牌价值提升的影响
  • 网站灰色代码2023新闻大事10条
  • 博物馆网站建设百度网页
  • 品牌网站建设方案济南seo整站优化招商电话
  • 网站公安备案 费用抚顺seo
  • 低调赚大钱的灰色行业重庆seo排名优化费用