然后我们在pom.xml里添加spring-boot-starter-web的Jar包引用 , 代码如下:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>然后创建一个controller文件 , 再创建一个HelloWorld的java类,如下图:

文章插图
然后编辑HelloWorld类 , 代码如下:
package com.client.kiba.controller;?import org.springframework.web.bind.annotation.*;?@RequestMapping(value = "https://www.huyubaike.com/helloWorld")@RestControllerpublic class HelloWorld {@RequestMapping(value = "https://www.huyubaike.com/GetName", method = RequestMethod.GET)public String GetName(){return "我是Kiba518";}@RequestMapping(value = "https://www.huyubaike.com/GetAge", method = {RequestMethod.GET,RequestMethod.POST})public int GetAge(){return 518;}@PostMapping("/GetAge1")public int GetAge1(){return 518;}@GetMapping("/GetAge2")public int GetAge2(){return 518;}}然后启动项目,然后在打开我们的eureka服务查询中心——http://localhost:5180/,可以看到服务已经成功注册进了服务中心 。

文章插图
注意:这里需要单独启动一下我们刚刚建好的项目 。

文章插图
到此,eureka服务注册就介绍完了 。
不得不说,eureka把服务注册处理的如此简单,仅仅用配置就搞定了,实在非常优秀 。
使用eureka内注册的服务
创建一个新moudle,创建过程如上 。
修改Kiba3Application的代码如下:
【一个C#开发者学习SpringCloud搭建微服务的心路历程】package com.clinet.kiba3;?import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;?@SpringBootApplication@EnableDiscoveryClientpublic class Kiba3Application {?public static void main(String[] args) {SpringApplication.run(Kiba3Application.class, args);}@Bean@LoadBalancedRestTemplate restTemplate(){return new RestTemplate();}}然后创建RemoteController接口,代码如下:
package com.clinet.kiba3.controller;?import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;?@RestController@RequestMapping("/Remote")public class RemoteController {@AutowiredRestTemplate restTemplate;?/*** http://localhost:5183/Remote/TestRestRequest* @return*/@GetMapping("/TestRestRequest")public ResponseEntity<String> TestRestRequest() {/*** 第一个参数:url——http://eureka-kiba2/helloWorld/GetName 这里把ip替换为在eureka中注册的名字* 第二个参数:返回值类型*/ResponseEntity<String> entity = restTemplate.getForEntity("http://eureka-kiba2/helloWorld/GetName", String.class);System.out.println("状态码:" + entity.getStatusCode());System.out.println("响应体" + entity.getBody());?return ResponseEntity.ok(entity.getBody());?}}如上所示,远程调用使用的是RestTemplate,不过调用的URL稍微做了修改,如上所示,我们请求的url地址是【http://eureka-kiba2/helloWorld/GetName】 , 可以看到,我们将ip替换为了在eureka中注册的应用名了 。
在其他的注册中心中,比如consul,也是通过应用名来调用单体服务的 , 这种调用模式属于潜规则了 。
下图为各个单体服务在注册中心注册的应用名 。

文章插图
在网页输入http://localhost:5183/Remote/TestRestRequest,输出结果如下图:

文章插图
推荐阅读
- 基于Netty的TCP服务框架
- 干嘛和怎么了的意思有什么区别(咋的了和怎么了是一个意思吗)
- Ruoyi字典源码学习
- [CG从零开始] 6. 加载一个柴犬模型学习UV贴图
- 女生说怎么了什么意思(一个女人能忍住几天不找你聊天)
- 记Windows的一个存在了十多年的bug
- iPad Air 3 深度评测
- 八 Netty 学习:新连接接入源码说明
- 学习记录-Python的局部变量和全局变量
- webpack打包思路与流程解析