Spring Cloud系列-微服务篇(二) 服务注册和服务发现
一:创建“服务提供方”
在这里使用Spring Boot快速的创建三个基础服务,并且其中三者之间存在逻辑依赖关系。
- User Service
- Wish Service
- Tree Service
现在我们以User Service 为例通过 http://start.spring.io/ 使用SPRING INITIALIZR 创建一个基础的Spring Boot工程,
- Dependencies :Eureka Discovery
- Group : com.alanzh.portal
- Artifact : wish.user.server
- Spring boot 版本: 1.5.3
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.alanzh.platform</groupId>
<artifactId>wish.user.service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>wish.user.service</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
User Entry 领域实体
package com.alanzh.platform.model;
/**
* @author Alan.Zhang
*/
public class UserEntry {
// Constructor、Getter、Setter
private Long userEntryID;
private String userEntryName;
}
UserEntryAction 控制层
package com.alanzh.platform.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alanzh.platform.model.UserEntry;
import com.alanzh.platform.service.UserEntryService;
/**
* @author Alan.Zhang
*/
@RestController
@RequestMapping(value="/user")
public class UserEntryAction {
@RequestMapping(value="/{userEntryID}", method=RequestMethod.GET)
public UserEntry getUserEntry(@PathVariable("userEntryID") Long userEntryID) {
return userEntryService.getUserEntry(userEntryID);
}
@RequestMapping(value="/{userEntryID}", method=RequestMethod.DELETE)
public UserEntry deleteUserEntry(@PathVariable("userEntryID") Long userEntryID) {
return userEntryService.deleteUserEntry(userEntryID);
}
@Autowired
private UserEntryService userEntryService;
}
UserEntryService服务层
package com.alanzh.platform.service;
import java.util.Random;
import org.springframework.stereotype.Service;
import com.alanzh.platform.model.UserEntry;
/**
* @author Alan.Zhang
*/
@Service
public class UserEntryService {
public UserEntry getUserEntry(Long userEntryID) {
sleep();
return new UserEntry(userEntryID, "User-" + userEntryID);
}
public UserEntry deleteUserEntry(Long userEntryID) {
sleep();
return new UserEntry(userEntryID, "User-" + userEntryID);
}
private void sleep() {
try {
Thread.sleep(new Random().nextInt(2000));
}
catch (Exception e) {
}
}
}
Application 程序入口
package com.alanzh.platform;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
==@EnableDiscoveryClient注解==,该注解能激活Eureka中的DiscoveryClient实现.
配置客户端信息 application.yml 做一些配置工作.(在这里我们YMAL, application.propertie 同样可以)
# Portal Info
info:
app:
name: "@project.name@"
description: "@project.description@"
version: "@project.version@"
# Spring Info
spring:
application:
name: "@project.name@"
server:
port: 2000
eureka:
client:
serviceUrl:
defaultZone: http://admin:123123@localhost:1000/eureka/
spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。
@project.name@ 为Artifact 即wish.user.service
eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。
admin : 为上篇我们配置安全的用户 123123 为密码
启动该工程后,再次访问:http://localhost:1000/可以发现我们服务已经注册
现在我们构建Wish Service, 步骤和上面一致.
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.alanzh.platform</groupId>
<artifactId>wish.wish.service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>wish.wish.service</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
Wish Entry 领域实体
/**
*
*/
package com.alanzh.platform.model;
import java.util.Date;
/**
* @author Alan.Zhang
*/
public class WishEntry {
// Constructor、Getter、Setter
private Long wishEntryID;
private Long userEntryID;
private String userName;
private String title;
private String content;
private int type;
private Date createDate;
}
WishEntryAction 控制层
/**
*
*/
package com.alanzh.platform.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alanzh.platform.model.WishEntry;
import com.alanzh.platform.service.WishEntryService;
/**
* @author Alan.Zhang
*
*/
@RestController
@RequestMapping(value="/wish")
public class WishEntryAction {
@RequestMapping(value="/{wishEntryID}", method=RequestMethod.DELETE)
public WishEntry deleteWishEntry(@PathVariable("wishEntryID") Long wishEntryID) {
return wishEntryService.deleteWishEntry(wishEntryID);
}
@RequestMapping(value="/{wishEntryID}", method=RequestMethod.GET)
public WishEntry getWishEntry(@PathVariable("wishEntryID") Long wishEntryID) {
return wishEntryService.getWishEntry(wishEntryID);
}
@Autowired
private WishEntryService wishEntryService;
}
WishEntryService 服务层
/**
*
*/
package com.alanzh.platform.service;
import java.util.Random;
import org.springframework.stereotype.Service;
import com.alanzh.platform.model.WishEntry;
/**
* @author Alan.Zhang
*
*/
@Service
public class WishEntryService {
public WishEntry getWishEntry(Long wishEntryID) {
sleep();
return new WishEntry(wishEntryID, "WishEntry-" + wishEntryID);
}
public WishEntry deleteWishEntry(Long wishEntryID) {
sleep();
return new WishEntry(wishEntryID, "WishEntry-" + wishEntryID);
}
private void sleep() {
try {
Thread.sleep(new Random().nextInt(2000));
}
catch (Exception e) {
}
}
}
Application 程序入口
package com.alanzh.platform;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
配置文件
# Portal Info
info:
app:
name: "@project.name@"
description: "@project.description@"
version: "@project.version@"
# Spring Info
spring:
application:
name: "@project.name@"
server:
port: 2200
eureka:
instance:
lease-renewal-interval-in-seconds: 5 # 心跳时间,即服务续约间隔时间(缺省为30s)
lease-expiration-duration-in-seconds: 10 # 发呆时间,即服务续约到期时间(缺省为90s)
client:
serviceUrl:
defaultZone: http://admin:123123@localhost:1000/eureka/
注: 端口更改为2200 其余和 UserService 一致
WishService 完成, 启动服务.
依照上面步骤添加WishTree service.添加完成之后启动服务就会看到如图:
项目地址: https://code.aliyun.com/alanzh/ms-spring-cloud/tree/master/chapter-1/chapter-1-2
评论已关闭