springCloud - 第2篇 - 服务的发现 seeParam

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

一、创建 Client 

1. file - new - module 

2. spring Initializr - module SDK 选择自己的 JDK ,其余的可以不用填写,next。

3. 填写工程相关信息:包名、工程名等,next。

 

4. spring cloud discovery - 勾选 eureka discover client,next。

5. 工程名,代码存放位置等,finish 。

 

6. 工程结构如下:

7.  pom.xml 中 引用 父 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.service</groupId>
	<artifactId>see-param</artifactId>
	<version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
	<name>see-param</name>
	<description>服务提供:查看参数</description>

    <parent>
        <groupId>com.base</groupId>
        <artifactId>base-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <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>

8. 在工程启动类上加注解:@EnableEurekaClient ,表示自已为 client 。

package com.service.seeparam;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication

// 标明自已为服务
@EnableEurekaClient
public class SeeParamApplication {

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

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

    /**
     * 查看参数
     * @param param
     * @return
     */
    @RequestMapping("/seeParam")
    public String seeParam(@RequestParam String param) {
        return " 我的端口是 :" + port + ",你输入的参数是: " + param;
    }

}

9. 配置文件相关设置:


# 注册中心 - 端口: 1234、工程名: eureka (见 eureka 工程中配置)。
eureka.client.serviceUrl.defaultZone= http://localhost:1234/eureka/

# 端口
server.port= 8801

# 工程名
spring.application.name= see-param

10.启动工程:

11. 浏览器访问:  http://localhost:1234/ 

 

12.  client-one 工程也可请求了:

至此,服务的发现已经OK 。

下一篇:springCloud - 第3篇 - 消费者 ribbon、调用已注册的服务

源码见:https://gitee.com/FJ_WoMenDeShiJie/springcloud-seeParam.git

--------------------------------------------------------------

PS:这个系列不定时更新,只是个人的学习分享,

内容全程参考书目:

《Spring Cloud 与 Docker 微服务架构空实战 》、

《Spring Cloud 微服务实战》及此书作者博客:http://blog.didispace.com/spring-cloud-learning/

《深入理解 Spring Cloud 与微服务构建》及此书作者博客:https://blog.csdn.net/forezp/article/details/70148833
--------------------------------------------------------------

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值