Docker Swarm mode 微服务部署及调用

来源:互联网 发布:七爷交友平台知乎 编辑:程序博客网 时间:2024/05/29 18:53

Docker Swarm mode 微服务部署及调用

使用spring boot 开发两个应用,分别命名为test-a、test-b, test-a 为服务提供者、test-b 为服务消费者,test-b 在swarm mode 集群中通过服务 test-a 部署时指定的服务名称调用服务。


服务提供者 test-a 代码如下:

@RestControllerpublic classTestAContoller(){    @GetMapping("/info")    public String getAInfo(){        return "A 的服务信息"; //返回A服务的服务信息    }}
@SpringBootApplicationpublic class WebApplication(){    public static void main(String[] args){        SpringApplication.run(AWebApplication.class,args);    }}

application.properties

server.port=80spring.application.name=testAService

服务消费者 test-b 代码如下:

@RestControllerpublic class TestBContoller(){    @Bean    RestTemplate restTemplate();    @GetMapping("/a/info")    public String getAInfo(){        String message = restTemplate.getForObject("http://testAService/info",String.class);        return "A : " + message; //返回A服务的服务信息    }}
@SpringBootApplicationpublic class WebApplication(){    public static void main(String[] args){        SpringApplication.run(WebApplication.class,args);    }}

application.properties

server.port=80spring.application.name=testBService

Swarm mode 服务部署

运行 docker 服务创建命令分别部署test-a 、test-b:

docker service create   \      --replicas 2 \      --name testAService \      test-a:1.0docker service create   \      --replicas 2 \      --publish 8080:80      --name testBService \      test-a:1.0

服务部署之后即可通过 http://localhost:8080/a/info 验证服务是否调用成功!!!


Dockerfile 文件

test-a: docker build -t test-a:1.0 .

FROM java:8-jre-alpineADD test-a.jar app.jarRUN sh -c 'touch /app.jar'ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

test-b: docker build -t test-b:1.0 .

FROM java:8-jre-alpineADD test-b.jar app.jarRUN sh -c 'touch /app.jar'ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
原创粉丝点击