会员微服务项目
项目代码:
链接:https://pan.baidu.com/s/1nhmu0lsvPIixRQE92sG9EA 密码:st10
一、项目结构如下图
二、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.joychen</groupId>
<artifactId>servermenber</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>servermenber</name>
<description>servermanber</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.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>Finchley.M8</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>
三、application.yml 配制信息代码
spring.application.name这个名称是注册全Eureka当中的微服务名称。以后别的微服务要调用是通过服务名称来时行调用的。包括后面要配制负载均衡。也是跟据名称来。
server.port 这个是当前微服务启动的端口号
eureka.instance.instance-id:是当前微服务提供的实际地址
eureka.client.service-url.defaultZone:这个是当前微服务项目注测到注册中心的地址
spring:
application:
name: manber-server
cloud:
config:
discovery:
enabled: true
service-id: manber-server
server:
port: 7879
eureka:
instance:
hostname: localhost
instance-id: ${spring.application.name}:${spring.cloud.client.hostname}:${server.port}
prefer-ip-address: false
client:
service-url:
defaultZone: http://localhost:7878/eureka/
四、写一个控制器提供员工信息
package com.joychen.servermenber.controllers;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class MemberController {
@RequestMapping("/member")
public List<String> getMemberAll(){
List<String> users = new ArrayList<>();
users.add("小明");
users.add("小红");
users.add("小蓝");
users.add("小绿");
users.add("小萌");
return users;
}
}
五、启动项目配制
package com.joychen.servermenber;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ServermenberApplication {
public static void main(String[] args) {
SpringApplication.run(ServermenberApplication.class, args);
}
}
六、项目启动
启动项目后。可以在上一个项目的注册中心Eureka中看到会中微服务注册到Eureka中来了。
七、可以单独测试一下接口
这个说明是成功的。