2022-07-08 16:10

基于SpringCloud快速搭建微服务入门级demo(二)

wanmatea

JavaEE

(1192)

(0)

收藏

上节课我们搭建了一个服务中心,这次课我们来看下如何搭建生产者和消费者模块,并实现简单的负载均衡。

一、创建生产者模块 springclouddemo-provider

在父工程上右键,新建一个module:springclouddemo-provider1

1、springclouddemo-provider1模块的pom文件:

<?
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.6.3</version>
        <relativePath/> 
<!-- lookup parent from repository -->
    
</parent>
    <groupId>com.wanmait</groupId>
    <artifactId>cloud-provider1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud-provider1</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>2021.0.0</spring-cloud.version>
    </properties>
    <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.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、springclouddemo-provider1模块的配置文件application.properties:

要在配置文件中声了服务注册中心的地址

spring.application.name=provider
server.port=8002
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

 

3、springclouddemo-provider1模块的启动类:

添加注解:@EnableEurekaClient来声明这是一个client(生产者或者消费者,在这里是生产者)

@SpringBootApplication
@EnableEurekaClient 
//Eureka
客户端
public class CloudProvider1Application {
    public static void main(String[] args) {
      SpringApplication.
run
(CloudProvider1Application.class, args);
    }
}

4、创建一个控制器UserController,用来提供测试用的API

代码如下:

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/sayHello")
    public String sayHello(){
        return "我是provider1";
    }
}

5、springclouddemo-provider1结构如下:

image.png 

 

6、启动springclouddemo-provider1模块

image.png 

7、在浏览器访问或者刷新:http://localhost:8761,在eureka服务注册发现中心查看生产者服务是否已被发现注册

注册成功如下:

image.png 

 

二、创建消费者模块 springclouddemo-consumer

在父工程上右键,新建一个module:springclouddemo-consumer

1、springclouddemo-consumer模块的pom文件:

<?
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.6.3</version>
        <relativePath/> 
<!-- lookup parent from repository -->
    
</parent>
    <groupId>com.wanmait</groupId>
    <artifactId>cloud-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud-consumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>2021.0.0</spring-cloud.version>
    </properties>
    <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.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、springclouddemo-consumer模块的application.properties配置文件:

要在配置文件中声了服务注册中心的地址

server.port=8001
spring.application.name=consumer
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

3、springclouddemo-consumer模块的启动类:

添加注解:@EnableEurekaClient来声明这是一个client(生产者或者消费者,在这里是消费者)

@SpringBootApplication
@EnableEurekaClient 
//Eureka
客户端
public class CloudConsumerApplication {
    public static void main(String[] args) {
      SpringApplication.
run
(CloudConsumerApplication.class, args);
    }
    @Bean
    @LoadBalanced 
//
可以通过服务名访问
    
public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

4、创建一个控制器HelloController,对外提供测试API

代码如下:

@RestController
public class HelloController {
    @Resource
    RestTemplate restTemplate;
    @RequestMapping("/hello")
    public String hello(){
        String url="http://provider/user/sayHello";
        return restTemplate.getForObject(url,String.class);
    }
}


5、springclouddemo-consumer模块结构如下:

image.png 

 

6、启动springclouddemo-consumer模块

image.png 

7、在浏览器访问或者刷新:http://localhost:8761,在eureka服务注册发现中心查看消费者服务是否已被发现注册

注册成功如下:

image.png 

 

三、测试

在地址栏直接访问消费者地址,http://localhost:8001/hello,成功调用到 springclouddemo-provider1提供的接口并返回结果!

image.png 

四、创建多个生产者,来测试负载均衡

1、再次创建一个生产者模块springclouddemo-provider2,创建方式和springclouddemo-provider1相同

注意端口号不能和springclouddemo-provider1重复,我们把端口号修改为8003

spring.application.name=provider
server.port=8003
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

2、然后启动第二个生产者模块

3、访问服务注册中心http://localhost:8761,可以看到两个生产者都上线了

image.png 

 

4、在浏览器中重复访问网址:http://localhost:8001/hello,会发现返回结果在两个生产者中切换,实现了负载均衡。

image.png 

image.png 

至此,消费者和生产者模块搭建并测试完成。

0条评论

点击登录参与评论