1.搭建Maven父项目
创建一个Maven父项目microservice-springcloud,删除src项目,配置pom.xml文件,添加依赖。
pom.xml
<?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>
<groupId>com.lz.jiaotong</groupId>
<artifactId>microservice-springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>microsevice-eureka-server</module>
<module>microservice-eureka-user</module>
<module>microservice-eureka-order</module>
</modules>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
<relativePath />
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.搭建服务端工程
在父项目microservice-springcloud,创建Maven子模块microservice-eureka-server作为服务端工程,是一个基础的Spring Boot工程。
(1)添加依赖:在pom.xml中添加Eureka Server的依赖
<?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>
<parent>
<artifactId>microservice-springcloud</artifactId>
<groupId>com.lz.jiaotong</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microsevice-eureka-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--Java9以后的版本中,默认没有加载JAXB。需要手动加载jaxb模块-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
(2)编写配置文件application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone:
#注册中心的地址 http://${eureka.instance.hostname}:${server.port}/eureka/
(3)修改服务端JAVA代码,添加注解@EnableEurekaServer该注解声明标注类是一个Eureka Server。EurekaApplication类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
(4)启动应用,在浏览器访问http://localhost:8761/查看Eureka信息面板
3.搭建客户端工程
在父项目microservice-springcloud,创建Maven子模块microservice-eureka-user作为客户端工程,是一个基础的Spring Boot工程。
(1)在pom.xml添加eureka依赖
<?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">
<parent>
<artifactId>microservice-springcloud</artifactId>
<groupId>com.lz.jiaotong</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-eureka-user</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
</project>
(2)编写配置文件application.yml.添加Eureka服务实例的端口号,服务端地址等信息。添加的时候注意格式。
server:
port: 8000 #指定Eureka实例的端口号
eureka:
instance:
prefer-ip-address: true #显示主机的ip
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #指定Eureka服务端地址
spring:
application:
name: microservice-eureka-user #指定应用名称
(3)修改客户端代,码添加注解@EnableEurekaClient,该注解声明标注类是一个Eureka客户端组件。Application类
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.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class Application {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
@RequestMapping("/hello")
public String home(){
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
(4)启动应用,查看信息。分别启动服务端和客户端工程。在浏览器中访问地址http://localhost:8761/
4.实现服务间的调用
1.搭建订单服务工程
在父项目microservice-springcloud,创建Maven子模块microservice-eureka-order。
(1)在pom.xml中添加依赖spring-cloud-starter-eureka
<?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">
<parent>
<artifactId>microservice-springcloud</artifactId>
<groupId>com.lz.jiaotong</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-eureka-order</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
</project>
(2)编写配置文件application.yml
server:
port: 7900 #指定该Eureka实例的端口号
eureka:
instance:
prefer-ip-address: true #是否显示主机ip
client:
service-url: http://localhost:8761/eureka/ #指定Eureka服务端地址
spring:
application:
name: microservice-eureka-order #指定应用名称
(3)创建订单实体类
public class Order {
private String id;
private Double price;
private String receiverName;
private String receiverAddress;
private String receiverPhone;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getReceiverPhone() {
return receiverPhone;
}
public void setReceiverPhone(String receiverPhone) {
this.receiverPhone = receiverPhone;
}
public String getReceiverName() {
return receiverName;
}
public void setReceiverName(String receiverName) {
this.receiverName = receiverName;
}
public String getReceiverAddress() {
return receiverAddress;
}
public void setReceiverAddress(String receiverAddress) {
this.receiverAddress = receiverAddress;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Order{" +
"id='" + id + '\'' +
", price=" + price +
", receiverName='" + receiverName + '\'' +
", receiverAddress='" + receiverAddress + '\'' +
", receiverPhone='" + receiverPhone + '\'' +
'}';
}
}
(4)创建订单的控制器类,在该类中模拟编写一个通过id查询订单的方法。
import com.lz.jiaotong.po.Order;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@GetMapping("/order/{id}")
public String findOrderById(@PathVariable String id){
Order order = new Order();
order.setId("123");
order.setPrice(23.5);
order.setReceiverName("Tom");
order.setReceiverAddress("beijing");
order.setReceiverPhone("11112221");
return order.toString();
}
}
(5)在引导类中添加@EnableEurekaClient注解。
2.编写用户服务功能
(1)在microservice-eureka-user工程引导类中,创建RestTemplate的Spring实例。
RestTemplate是Spring提供的用于访问Rest服务的客户端实例,提供了多种便捷访问Http服务的方法,大大提高客户端的编写效率。
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
(2)创建用户的控制器类,在该类中编写查询方法。
import org.springframework.beans.factory.annotation.Autowired;
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 UserController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("findOrdersByUser/{id}")
public String findOrdersByUser(@PathVariable String id){
int oid=123;
return this.restTemplate.getForObject("http://localhost:7900/order/"+oid,String.class);
}
}
3.启动服务应用,测试服务调用
访问地址http://localhost:8000/findOrdersByUser/123