开始实践服务注册发现功能前,请确保已完成了 SDK 下载。
服务注册依赖consul作为注册中心,需要本地安装consul,consul下载和安装参考官网https://www.consul.io。
通过一个简单的示例说明如何实践服务的注册和发现。
注意:需要完成“服务开发”的相关步骤后才进行以下操作。https://docs.ksyun.com/documents/37283
一、创建服务提供者
此服务提供一个简单的服务,并将自身注册到服务。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.ksyun.kmse</groupId>
<artifactId>spring-cloud-kmse-starter-consul-discovery</artifactId>
<version>${version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.demo.x.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping({"/account"})
@RestController
public class AccountController {
private static final Logger log = LoggerFactory.getLogger(AccountController.class);
public AccountController() {
}
@GetMapping({"/{id}"})
public String account(@PathVariable("id") Integer id) {
log.info("调用account " + id);
return id + "";
}
}
5.修改配置
在resource目录下的bootstrap.yml文件中配置应用名与监听端口号等信息。
server:
port: 8080
spring:
application:
name: server-demo
cloud:
host: http://127.0.0.1
port: 8500
discovery:
healthCheckPath: /actuator/health
healthCheckInterval: 15s
register: true
service-name: server-demo
enabled: true
instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
二、创建服务消费者
我们将创建一个服务消费者,消费者通过FeignClient客户端去调用服务提供者。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.ksyun.kmse</groupId>
<artifactId>spring-cloud-kmse-starter-consul-discovery</artifactId>
<version>${version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
@SpringBootApplication
@EnableFeignClients
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.demo.x.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient("server-demo")
public interface OrderClient {
@GetMapping({"/account/{id}"})
String getById(@PathVariable Integer id);
}
package com.demo.x.controller;
import com.demo.x.client.OrderClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping({"/account"})
@RestController
public class AccountController {
private static final Logger log = LoggerFactory.getLogger(AccountController.class);
@Autowired
private OrderClient orderClient;
@GetMapping({"/demo"})
public String demo() {
return orderClient.getById(12);
}
}
server:
port: 8081
spring:
application:
name: client-demo
cloud:
host: http://127.0.0.1
port: 8500
三、调用服务
分别启动server-demo和client-demo,然后调用client-demo的/demo接口,接口没有报错并成功返回,一个简单的服务注册与发现的例子就做完了。
文档内容是否对您有帮助?
评价建议不能为空
非常感谢您的反馈,我们会继续努力做到更好!