-java
一、在 eureka 服务治理框架中,微服务与微服务之间经过 Http 协议进行通讯; 二、用户微服务做为消费方、电影微服务做为提供方都注册到 EurekaServer 中; 三、在电影微服务Web层经过 application.yml 的硬编码配置方式实现服务之间的通讯;
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>springms-consumer-movie</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>com.springms.cloud</groupId> <artifactId>springms-spring-cloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <!-- web模块 --> <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-eureka</artifactId> </dependency> </dependencies> </project>
spring: application: name: springms-consumer-movie server: port: 7901 user: userServicePath: http://localhost:7900/simple/ eureka: client: # healthcheck: # enabled: true serviceUrl: defaultZone: http://admin:admin@localhost:8761/eureka instance: prefer-ip-address: true instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
package com.springms.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; } }
package com.springms.cloud.controller; import com.springms.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; @RestController public 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); } }
package com.springms.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; /** * 电影微服务,也注册到 EurekaServer 中,经过 Http 协议访问已注册到生态圈中的用户微服务。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/17 * */ @SpringBootApplication @EnableEurekaClient public class MsConsumerMovieApplication { @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(MsConsumerMovieApplication.class, args); System.out.println("【【【【【【 电影微服务 】】】】】】已启动."); } }
/**************************************************************************************** 1、电影微服务,也注册到 EurekaServer 中,经过 Http 协议访问已注册到生态圈中的用户微服务: 一、启动 springms-discovery-eureka 模块服务,启动1个端口; 二、启动 springms-provider-user 模块服务,启动1个端口; 三、启动 springms-consumer-movie 模块服务,启动1个端口; 四、在浏览器输入地址 http://localhost:7900/simple/1 能够看到信息成功的被打印出来,说明用户微服务正常; 五、在浏览器输入地址 http://localhost:8761 并输入用户名密码 admin/admin 进入Eureka微服务显示在网页中,验证用户微服务、电影微服务确实注册到了 eureka 服务中; 六、在浏览器输入地址http://localhost:7901/movie/1 能够看到用户信息成功的被打印出来; ****************************************************************************************/
https://gitee.com/ylimhhmily/SpringCloudTutorial.gitgit
SpringCloudTutorial交流QQ群: 235322432web
SpringCloudTutorial交流微信群: 微信沟通群二维码图片连接spring
欢迎关注,您的确定是对我最大的支持!!!apache