上一篇
这个模块存储数据库实体对象,提供别的模块依赖,线上项目通常是这样的做法。构建model模块 操作不在具体介绍。
模块图如下:
新建实体UserVo:
package com.spring.cloud.model;
import lombok.Data;
/**
* @ClassName UserVo
* @Description TODO
* @Author cuixhao
* @Date 2019/3/7
* @Version 1.0
**/
@Data
public class UserVo {
private String userId;
private String userName;
}
pom.xml文件
<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>spring-cloudartifactId>
<groupId>com.spring.cloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>modelartifactId>
<packaging>jarpackaging>
project>
<parent>
<artifactId>spring-cloudartifactId>
<groupId>com.spring.cloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>eureka-providerartifactId>
<version>1.0-SNAPSHOTversion>
<name>eureka-providername>
<description>服务端description>
<packaging>jarpackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<spring-cloud.version>Greenwich.RELEASEspring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-ribbonartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.mockitogroupId>
<artifactId>mockito-coreartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.spring.cloudgroupId>
<artifactId>modelartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
<repositories>
<repository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/milestoneurl>
repository>
repositories>
package com.spring.cloud.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @ClassName EurekaProviderApplication
* @Description TODO
* @Author cuixhao
* @Date 2019/3/7
* @Version 1.0
**/
@SpringBootApplication
@EnableEurekaClient
public class EurekaProviderApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaProviderApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
注:@EnableEurekaClient 和 @EnableDiscoveryClient的区别:
package com.spring.cloud.eureka.controller;
import com.spring.cloud.model.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName ClientController
* @Description TODO
* @Author cuixhao
* @Date 2019/3/7
* @Version 1.0
**/
@RestController
@Slf4j
public class ClientController {
@GetMapping("add")
public UserVo service(@RequestParam String userId) {
UserVo userVo = new UserVo();
userVo.setUserId(userId);
userVo.setUserName("杰克");
log.info("执行了");
return userVo;
}
}
通过输入一个userId参数,返回一个UserVo对象。
编写application.properties文件:
# 服务名字
spring.application.name=PRODUCER-SERVICE
# 端口
server.port=7001
# 注册中心地址,如果是集群,那就逗号分隔
eureka.client.serviceUrl.defaultZone= \
http://localhost:8001/eureka/
先启动Eureka-Server,在启动Eureka-Provider,访问注册中心:http://localhost:8001
出现如图所示,表示服务已经注册成功。
服务集群配置和上一篇注册中心集群步骤一致。
编写两个服务:
application.properties
# 服务名称
spring.application.name=PRODUCER-SERVICE
# 注册中心地址
eureka.client.serviceUrl.defaultZone= \
http://localhost:8001/eureka/
application-service1.properties
server.port=7001
application-service2.properties
server.port=7002
启动命令:
java -jar eureka-provider.jar --spring.profiles.active=service1
java -jar eureka-provider.jar --spring.profiles.active=service2
访问注册中心:http://localhost:8001,就可以看到7001 7002 两个端口的服务注册成功了!
下一篇