博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
微服务之springCloud-docker-comsumer(三)
阅读量:6954 次
发布时间:2019-06-27

本文共 4892 字,大约阅读时间需要 16 分钟。

简介 

上一节,我们讲了创建spring cloud生产者,并利用docker-compose部署到swarm集群中,这节我们讨论一下最restTemlate调用生产者服务

一、创建模块(microservice-consumer-movie)

项目结构如下:

二、pom.xml文件

microservice-spring-cloud
com.jacky
1.0-SNAPSHOT
4.0.0
microservice-consumer-movie
pom
../microservice-provider-user
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-actuator
com.spotify
docker-maven-plugin
build-image
install
build
http://192.168.6.130:5678
true
${docker.repostory}/${docker.image.prefix}/${project.artifactId}:${project.version}
java:openjdk-8-jdk-alpine
["java", "-jar", "/${project.build.finalName}.jar"]
/
${project.build.directory}
${project.build.finalName}.jar

三、实体类(User.java)

package com.jacky.cloud.entity;import java.math.BigDecimal;public class User {  private Long id;  private String username;  private String name;  private Short age;  private BigDecimal balance;  public Long getId() {    return this.id;  }  public void setId(Long id) {    this.id = id;  }  public String getUsername() {    return this.username;  }  public void setUsername(String username) {    this.username = username;  }  public String getName() {    return this.name;  }  public void setName(String name) {    this.name = name;  }  public Short getAge() {    return this.age;  }  public void setAge(Short age) {    this.age = age;  }  public BigDecimal getBalance() {    return this.balance;  }  public void setBalance(BigDecimal balance) {    this.balance = balance;  }}

四、MovieController.java

package com.jacky.cloud.controller;import com.jacky.cloud.entity.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class MovieController {  @Autowired  private RestTemplate restTemplate;  @Value("${user.userServicePath}")  private String userServicePath;  @GetMapping("/movie/{id}")  public User findById(@PathVariable Long id) {    return this.restTemplate.getForObject(this.userServicePath + id, User.class);  }}

五、启动类(MicroserviceSimpleConsumerMovieApplication)

package com.jacky.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplication@EnableEurekaClientpublic class MicroserviceSimpleConsumerMovieApplication {  @Bean  public RestTemplate restTemplate() {    return new RestTemplate();  }  public static void main(String[] args) {    SpringApplication.run(MicroserviceSimpleConsumerMovieApplication.class, args);  }}

六、配置文件application.yml

server:  port: 7901spring:  application:    name: microservice-consumer-movie  #zipkin:    #base-url: http://127.0.0.1:7788user:   userServicePath: http://localhost:7900/simple/      #指定生产者的路径eureka:  client:    healthcheck:      enabled: true    serviceUrl:      defaultZone: http://jacky:admin@peer1:8761/eureka/,http://jacky:admin@peer2:8762/eureka/,http://jacky:admin@peer3:8763/eureka/  instance:    prefer-ip-address: true    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

转载地址:http://snvil.baihongyu.com/

你可能感兴趣的文章
ABP Zero 导航菜单之角色权限
查看>>
IFrame安全问题解决办法(跨框架脚本(XFS)漏洞)
查看>>
url参数中有+、空格、=、%、&、#等特殊符号的问题解决
查看>>
再也不用线上倒数据了,使用 Faker 来造一批假的数据吧。
查看>>
AOP 面向切面编程
查看>>
学习MongoDB 一:MongoDB 入门(安装与配置)
查看>>
现代php编程
查看>>
django rest framework(4)
查看>>
数据库总结
查看>>
揽货最短路径解决方案算法 - C# 蚁群优化算法实现
查看>>
nginx安装及配置
查看>>
fcntl函数用法详解
查看>>
Miscellaneos:ISV
查看>>
Vue表单和组件
查看>>
场,位,势与逆熵过程——评《活着之上》
查看>>
比特币算法
查看>>
Lottie 动画里有图片怎么办?设计师小姐姐也能帮你减少开发量!
查看>>
eclipse环境下无法创建android virtual Devices(AVD)问题解决的方法汇总
查看>>
ZooKeeper学习之路 (八)ZooKeeper原理解析
查看>>
oracle判断是否包含字符串的方法
查看>>