3.spring cloud + zookeeper注册中心 + Feign调用案例
Posted to.to
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3.spring cloud + zookeeper注册中心 + Feign调用案例相关的知识,希望对你有一定的参考价值。
3.spring cloud + zookeeper注册中心 + Feign调用案例
3.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.0.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xxx.xxxxxxx</groupId>
<artifactId>feign-test</artifactId>
<version>3.7.1</version>
<name>feign-test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
<zookeeper.version>3.4.6</zookeeper.version>
<curator.version>4.0.0</curator.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-zookeeper-discovery</artifactId>
<!--<version>2.2.0.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<!--<version>2.2.1.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<!--<scope>provided</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>mvnrepository</id>
<name>mvnrepository</name>
<url>https://mvnrepository.com/artifact/</url>
</repository>
<repository>
<id>maven</id>
<name>maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
</project>
3.1.1.resources中定义application.yml
server:
port: 9988
spring:
application:
name: feign-test
cloud:
zookeeper:
connect-string: xxx-test2.dg.xxx:2181
discovery:
register: true
enabled: true
instance-id: 1
root: /xxxxx/dist/services
3.1.2.启动类编写
package com.xxx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignTestMain {
private static final Logger LOG = LoggerFactory.getLogger(FeignTestMain.class);
public static void main(String[] args) {
LOG.info("starting collector ......");
// new SpringApplicationBuilder().sources(FeignTestMain.class).run(args);
SpringApplication.run(FeignTestMain.class,args);
}
}
3.1.3.编写Feign的接口(调用别的工程时)
package com.xxx.xxxxxxx.collector.client;
import com.xxx.xxxxxxx.collector.domain.HttpResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author tuzuoquan
* @date 2021/8/31 18:46
*/
@FeignClient("zk-collector-provider")
public interface CollectorApi {
@PostMapping("/api/v1/collector/alarm/meta/job/{jobId}/init")
HttpResult<String> initAlarmMeta(@PathVariable(name = "jobId") String jobId,
@RequestParam String userId);
}
3.1.4.编写Controller
package com.xxx.xxxxxxx.collector.controller;
import com.xxx.xxxxxxx.collector.client.CollectorApi;
import com.xxx.xxxxxxx.collector.domain.HttpResult;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
private static final Logger LOG = LoggerFactory.getLogger(TestController.class);
@Autowired
CollectorApi collectorApi;
@GetMapping("/welcome")
public String welcome(String jobId, String userId) {
LOG.info("collectorApi.initAlarmMeta start ..... ");
HttpResult<String> result = collectorApi.initAlarmMeta(jobId, userId);
LOG.info("collectorApi.initAlarmMeta end ..... ");
return "code:" + result.getCode() + ", msg:" + result.getMsg();
}
}
3.1.5.编写HttpResult
package com.xxx.xxxxxxx.collector.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class HttpResult<T> {
private int code;
private String msg;
private T result;
public boolean isSuccess(){
return code == 200;
}
public static <T> HttpResult<T> success(T data){
return new HttpResult<>(200, null, data);
}
}
3.1.6.启动时使用外部配置application.yml,命令:
java -jar feign-test-3.7.1.jar --spring.config.location=application.yml
后台静默执行方式:
nohup java -jar feign-test-3.7.1.jar --spring.config.location=application.yml > feign-test.log 2>&1 &
3.1.7.使用curl做请求
Get 请求案例
curl -X GET 'http://127.0.0.1:9988/test/welcome?jobId=10252&userId=admin.mammut@163.com'
Post请求案例
curl -H "Content-Type:application/json" -X POST --data '{"monitorDBUrl":"xxx-tsdb0.dg.xxx:8091","type":0}' http://xxxxx-dev.bdms.xxx.com/web/v2/xxx/bootstrap/item/cluster/monitor/config?isDevelop=true
以上是关于3.spring cloud + zookeeper注册中心 + Feign调用案例的主要内容,如果未能解决你的问题,请参考以下文章
spring boot 2.0.3+spring cloud (Finchley)6配置中心Spring Cloud Config
CLOUD 04:zookeeper,kafka,hadoop高可用
为 spring-cloud-bus 设置 zookeeper 主机和端口