1.创建一个空项目

打开idea,需要先创建一个空项目,在下图所示,点击next后,输入创建项目的名称,完成创建。(创建空项目的好处你不需要在接下来的操作中创建好几个项目)

alt

2.创建一个公共的dm-common-module

alt

alt

alt

(1)添加公共依赖

在此模块中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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.itheima</groupId>
    <artifactId>dm-common-module</artifactId>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>
    <dependencies>
        <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>

3.搭建Eureka Server
(1)创建Eureka Server模块

在完成创建空项目后,在文件中新建一个Module(File->New->Module),如下图所示。

alt

alt

alt

(2)添加Eureka Server依赖

在此模块中pom.xml中添加如下代码。第一次在IDEA中使用Maven管理Euraka,需要在联网的状态下等待Maven自动下载Eureka的jar包,下载时间较长,请耐心等待。

<?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>dm-common-module</artifactId>
        <groupId>com.itheima</groupId>
        <version>2.1.7.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Eureka-Server</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>
(3)配置全局文件application.yml

在Eureka Server模块下,找到src/main/resources下创建文件application.yml,复制如下代码进行配置。。


server:
  port: 7000      #服务器端口号7000
spring:
  application:
    name: eureka-server      #端口号名称配置
eureka:
  client:
    fetch-registry: false         #表示是否向EurekaServer注册
    register-with-eureka: false   #表示是否从EurekaServer获取注册信息
    service-url:
      defaultZone:
        http://${eureka.instance.hostname}:${server.port}/eureka/
  instance: localhost

配置完成后application.yml的图标变成如下所示,如果图标不一样,可能需要重新配置一下(也可尝试清除缓存)

alt

(4)启动EurekaServerApplication

在src/main/java下创建启动类EurekaServerApplication的Java文件。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}


(5)启动示例

启动EurekaServerApplication的main方法,启动成功后,在浏览器中访问EurekaServer的主界面 http://localhost:7000/ ,效果如下图所示。。

alt

4.搭建Eureka Provider(与Eureka Server的搭建基本一致)
(1)创建Eureka Provider

同理,如上Eureka Server,建立Eureka Provider的模块。。

(2)添加Eureka Provider依赖
<?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>dm-common-module</artifactId>
        <groupId>com.itheima</groupId>
        <version>2.1.7.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Eureka-Provider</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
</project>
(3)配置全局文件application.yml

在Eureka Provider模块下,找到src/main/resources下创建文件application.yml,复制如下代码进行配置。。

server:
  port: 7006   #指定provider的端口号
spring:
  application:    #指定应用名称
    name: eureka-provider
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/
    instance:
      hostname: localhost

(4)启动EurekaProviderApplication

在src/main/java下创建启动类EurekaProviderApplication的Java文件。

package com.lz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaProvide {
    public static void main(String[] args) {
        SpringApplication.run(EurekaProvide.class,args);
    }
}


保证由Eureka Server启动的状态下,运行EurekaProviderApplication,启动Eureka Provide,启动成功后,再次打开 http://localhost:7000/ ,效果如下图所示。。

alt

(5)添加controller和entity包

https://blog.nowcoder.net/n/eeb1dff8825844f3917e1c5046d182df 中创建了controller和entity包。。

① controller包中有两个类,分别为 HelloController 和 PortController 。。

HelloController中的具体代码如下:

package com.lz.controller;

import com.lz.entity.User;
import org.springframework.web.bind.annotation.*;

@RestController
public class HelloController {
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String sayHello(){
        return "hello Feign";
    }


    @RequestMapping(value = "/hello1",method = RequestMethod.GET)
    public String hello(@RequestParam String name){
        return "hello,"+name;
    }

    @RequestMapping(value = "/hello2",method = RequestMethod.GET)
    public User hello(@RequestParam String name, @RequestHeader Integer age){
        return new User(name,age);
    }

    @RequestMapping(value = "/hello3",method = RequestMethod.POST)
    public String hello(@RequestBody User user){
        return "hello,"+user.getName()+","+user.getAge();
    }
}

PortController具体代码如下:

package com.lz.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PortController {
    @Value("${server.port}")
    String port;
    @RequestMapping("/port")
    public String getPort() {
        return "Hello,I'm from port:"+port;
    }
}

② entity包中有一个类,为User类,具体代码如下:

package com.lz.entity;

public class User {
    private String name;
    private Integer age;

    public User() {}

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "name=:"+name+"   "+"age:"+age;
    }
}

(6)启动服务并进行测试

依次启动Eureka Server,Eureka Provider。启动成功后,使用浏览器访问 http://localhost:7006/port ,效果如下所示。。

alt

4.搭建Eureka Feign-client(与Eureka Server的搭建基本一致)
(1)创建Eureka Feign-client

同理,如上Eureka Server,建立Eureka Feign-client的模块。。

(2)添加Eureka Provider依赖
<?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>dm-common-module</artifactId>
        <groupId>com.itheima</groupId>
        <version>2.1.7.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Eureka-Feign-client</artifactId>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

</project>
(3)配置全局文件application.yml

在Eureka Feign-client模块下,找到src/main/resources下创建文件application.yml,复制如下代码进行配置。。

spring:
  application:
    name: eureka-feign-client
server:
  port: 8764
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/

(4)启动EurekaFeginClientApplication

在src/main/java下创建启动类EurekaFeginClientApplication的Java文件。

package com.lz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class EurekaFeginClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaFeginClientApplication.class,args);
    }
}


(5)添加controller,entity,service包

https://blog.nowcoder.net/n/eeb1dff8825844f3917e1c5046d182df 中创建了controller和entity包。。

① controller包中有一个类,为 FeignController。。

FeignController 中的具体代码如下:

package com.lz.controller;

import com.lz.entity.User;
import com.lz.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignController {
    @Autowired
    FeignService feignService;
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(){
        return feignService.sayhello();
    }

    @RequestMapping("/port")
    public String port(){
        return feignService.port();
    }

    @RequestMapping(value = "/helloUser",method = RequestMethod.GET)
    public String helloUser(){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(feignService.hello("zhangsan")).append("<br/>");

        stringBuilder.append(feignService.hello("lisi",30)).append("<br/>");

        stringBuilder.append(feignService.hello(new User("wangwu",27))).append("<br/>");

        return stringBuilder.toString();
    }
}

② entity包中有一个类,为User类

User 中的具体代码如下:

package com.lz.entity;

public class User {
    private String name;
    private Integer age;

    public User() {}

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "name=:"+name+"   "+"age:"+age;
    }
}

③ service 包中有一个类,为 FeignService 类

FeignService 中的具体代码如下:

package com.lz.service;

import com.lz.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

@Service
@FeignClient(name="eureka-provider")
public interface FeignService {
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String sayhello();

    @RequestMapping("port")
    public String port();

    @RequestMapping(value = "/hello1",method = RequestMethod.GET)
    public String hello(@RequestParam("name") String name);

    @RequestMapping(value = "/hello2",method = RequestMethod.GET)
    public User hello(@RequestParam("name") String name,@RequestHeader("age") Integer age);

    @RequestMapping(value = "/hello3",method = RequestMethod.POST)
    public String hello(@RequestBody User user);
}

(6)启动服务并进行测试

依次启动Eureka Server,Eureka Provider,Eureka Feign-client。启动成功后,使用浏览器访问 http://localhost:8764/hello ,效果如下所示。。

alt

访问 http://localhost:8764/helloUser ,效果如下所示。。

alt

5.继承

(1)建立一个公共工程commom-client

alt

alt

alt

在此模块中建立两个包,entity 和 service

① 在entity包中有一个实体类User,具体代码如下:

public class User {
    private String name;
    private Integer age;

    public User() {}

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "name=:"+name+"   "+"age:"+age;
    }
}

② 在service包中有一个FeignService类,具体代码如下:

@Service
public interface FeignService {
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String sayhello();

    @RequestMapping("port")
    public String port();

    @RequestMapping(value = "/hello1",method = RequestMethod.GET)
    public String hello(@RequestParam("name") String name);

    @RequestMapping(value = "/hello2",method = RequestMethod.GET)
    public User hello(@RequestParam("name") String name,@RequestHeader("age") Integer age);

    @RequestMapping(value = "/hello3",method = RequestMethod.POST)
    public String hello(@RequestBody User user);
}

在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>org.example.jar</groupId>
    <artifactId>dm-common-module11</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>
    <name>dm-common-module11</name>


</project>

选择IDEA右侧的MEVEN的dm-common-module11中的 Lifecyle,双击Lifecyle中的 install进行打包。。

alt

alt

会出现如上信息,表示打包的地址及名称。。

(2)改造 Eureka Provider

① 在pom.xml中,引入刚才打的包。。

<dependency>
            <groupId>org.example.jar</groupId>
            <artifactId>dm-common-module11</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>