基于eureka-server的服务注册与消费案例

基于eureka-server的服务注册与消费案例

  • 1.搭建eureka-server
    • 1.1 pom.xml
    • 1.2 application.properties
    • 1.3 主程序,开启eureka服务的功能
  • 2.搭建common工程
    • 2.1 User实体类
    • 2.2 pom.xml
  • 3.搭建提供者
    • 2.1 pom.xml
    • 2.2 application.properties
    • 2.3 controller
  • 4.搭建消费者
    • 2.1 pom.xml
    • 2.2 application.properties
    • 2.3 主程序
    • 2.4 注入相关模板工具
    • 2.4 服务消费例子(基于HttpURLConnection)
    • 2.5 服务消费例子(基于RestTemplate)
    • 2.6 RestTemplate的具体使用
      • 2.6.1 GET请求
      • 2.6.1 POST请求
      • 2.6.1 PUT请求
      • 2.6.1 DELETE请求

1.搭建eureka-server

1.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.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yl</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>2021.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <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>

1.2 application.properties

# 服务名称
spring.application.name=eureka
# 端口号
server.port=1111
# 其本身即是一个服务,也是一个注册中心,这里设置不把自己注册到注册中心去
eureka.client.register-with-eureka=false
# 从注册中心获取注册信息
eureka.client.fetch-registry=false

1.3 主程序,开启eureka服务的功能

基于eureka-server的服务注册与消费案例_第1张图片

2.搭建common工程

2.1 User实体类

package com.yl.model;

public class User {
    private Integer Id;
    private String username;
    private String password;

    public User() {
    }

    public User(Integer id, String username, String password) {
        Id = id;
        this.username = username;
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "Id=" + Id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public Integer getId() {
        return Id;
    }

    public void setId(Integer id) {
        Id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

2.2 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">
    <parent>
        <artifactId>mycloud</artifactId>
        <groupId>com.yl</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>common</artifactId>


</project>

3.搭建提供者

2.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.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yl</groupId>
    <artifactId>provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>provider</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2021.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.yl</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <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.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>

2.2 application.properties

# 应用名称
spring.application.name=provider
# 端口号
server.port=1114
# 注册中心地址
eureka.client.service-url.defaultZone=http://localhost:1111/eureka

2.3 controller

package com.yl.provider.controller;

import com.yl.model.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
public class HelloController {

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/hello")
    public String hello() {
        return "hello eureka:" + port;
    }


    @GetMapping("/testGet")
    public String testGet(String name) {
        return "hello" + name;
    }

    @PostMapping("/addUser")
    public User addUser(User user) {
        return user;
    }

    @PostMapping("/addUser1")
    public User addUser1(@RequestBody User user) {
        return user;
    }

    @PutMapping("updateUser")
    public void updateUser(User user) {
        System.out.println(user);
    }

    @PutMapping("updateUser1")
    public void updateUser1(@RequestBody User user) {
        System.out.println(user);
    }

    @DeleteMapping("/deleteUser")
    public void deleteUser(Integer id) {
        System.out.println(id);
    }

    @DeleteMapping("/deleteUser1/{id}")
    public void deleteUser1(@PathVariable Integer id) {
        System.out.println(id);
    }


}

package com.yl.provider.controller;

import com.yl.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RegisterController {

    @PostMapping("/register")
    public String register(User user) {
        return "redirect:http://provider/loginPage?username=" + user.getUsername(); //注意:这里一定要写绝对路径,写相对路径到时候host就变成实例在eureka上的实例名称了
    }

    @GetMapping("/loginPage")
    @ResponseBody
    public String loginPage(String username) {
        return "loginPage:" + username;
    }
}

4.搭建消费者

2.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.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yl</groupId>
    <artifactId>consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2021.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.yl</groupId>
            <artifactId>common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <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.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>

2.2 application.properties

# 应用名称
spring.application.name=consumer
# 端口号
server.port=1115
# 注册中心地址
eureka.client.service-url.defaultZone=http://localhost:1111/eureka


2.3 主程序

package com.yl.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

    // 加载RestTemplate
    @Bean
    RestTemplate restTemplateOne() {
        return new RestTemplate();
    }

    // 加载RestTemplate,并且开启负载均衡机制
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

2.4 注入相关模板工具

基于eureka-server的服务注册与消费案例_第2张图片

2.4 服务消费例子(基于HttpURLConnection)

基于eureka-server的服务注册与消费案例_第3张图片

2.5 服务消费例子(基于RestTemplate)

基于eureka-server的服务注册与消费案例_第4张图片

2.6 RestTemplate的具体使用

2.6.1 GET请求

基于eureka-server的服务注册与消费案例_第5张图片

2.6.1 POST请求

基于eureka-server的服务注册与消费案例_第6张图片

2.6.1 PUT请求

基于eureka-server的服务注册与消费案例_第7张图片

2.6.1 DELETE请求

基于eureka-server的服务注册与消费案例_第8张图片

你可能感兴趣的:(springcloud,eureka,spring,cloud,java)