<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
package com.moonpac.icrms.config;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;
/**
* @Copyright (C), 2016-2022 MP
* @ClassName: ICRMSCloudConfig
* @Author: hf
* @Date: 2022/9/4 16:07
* @Description: ICRMS服务 - 关于Nacos配置-服务注册-服务发现-Feign远程调用
*/
@Configuration
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.moonpac.icrms.feign")
public class ICRMSCloudConfig {
}
spring:
application:
#服务名称
name: aiops-icrms
cloud:
nacos:
discovery:
# 集群方式多个地址以,分割
server-addr: 10.173.28.204:8848
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
package com.moonpac.personnel.feign;
import com.moonpac.personnel.feign.fallback.ICRMSTestFeignServiceFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* @Copyright (C), 2016-2022 MP
* @ClassName: ICRMSTestController
* @Author: hf
* @Date: 2022/9/4 16:27
* @Description: ICRMS 测试Feign调用
*/
@FeignClient(
//指定服务名,nacos注册的服务名
name = "aiops-icrms",
//指定容错类-当远程调用失败时进入容错类中
fallbackFactory = ICRMSTestFeignServiceFallbackFactory.class
)
//指定公共请求前缀
@RequestMapping("/icrms")
public interface ICRMSTestFeignService {
@GetMapping("/list")
List<String> list();
}
package com.moonpac.personnel.feign.fallback;
import com.moonpac.personnel.feign.ICRMSTestFeignService;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
/**
* @Copyright (C), 2016-2022 MP
* @ClassName: ICRMSTestServiceFallbackFactory
* @Author: hf
* @Date: 2022/9/4 16:29
* @Description: Feign 远程调用容错
*/
@Service
@Slf4j
public class ICRMSTestFeignServiceFallbackFactory implements FallbackFactory<ICRMSTestFeignService> {
@Override
public ICRMSTestFeignService create(Throwable throwable) {
//捕获到异常
throwable.printStackTrace();
return new ICRMSTestFeignService() {
//捕获到异常
@Override
public List<String> list() {
return Arrays.asList("Feign远程调用报错了...","错了");
}
};
}
}
package com.moonpac.personnel.config;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration;
/**
* @Copyright (C), 2016-2022 MP
* @ClassName: PersonnelCloudConfig
* @Author: hf
* @Date: 2022/9/4 16:23
* @Description: Personnel服务关于Nacos配置-服务注册-服务发现-Feign远程调用
*/
@Configuration
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.moonpac.personnel.feign")
public class PersonnelCloudConfig {
}
package com.moonpac.personnel.controller;
import com.moonpac.personnel.feign.ICRMSTestFeignService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Copyright (C), 2016-2022 MP
* @ClassName: PersonnelFeignController
* @Author: hf
* @Date: 2022/9/4 16:43
* @Description: Feign 远程调用服务
*/
@Slf4j
@RestController
@RequestMapping("/personnel")
@RequiredArgsConstructor
public class PersonnelFeignController {
private final ICRMSTestFeignService icrmsTestFeignService;
@GetMapping("/list")
public Object list() {
//远程调用开始,即将通过Feign 远程调用接口
log.info("远程调用开始,即将通过Feign 远程调用其它服务接口...");
return icrmsTestFeignService.list();
}
}
package com.moonpac.personnel.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @Copyright (C), 2016-2022 MP
* @ClassName: PersonnelRestTemplateConfig
* @Author: hf
* @Date: 2022/9/4 16:59
* @Description: Personnel 用户中心服务 RestTemplate配置
*/
@Configuration
public class PersonnelRestTemplateConfig {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.moonpac.personnel.controller;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @Copyright (C), 2016-2022 MP
* @ClassName: PersonnelTemplateController
* @Author: hf
* @Date: 2022/9/4 16:43
* @Description: RestTemplate 远程调用服务
*/
@Slf4j
@RestController
@RequestMapping("/personnel/template")
@RequiredArgsConstructor
public class PersonnelTemplateController {
private static final String ICRMS_SERVER_ADDR = "http://aiops-icrms";
private final RestTemplate restTemplate;
@ApiOperation("RestTemplate 测试调用")
@GetMapping("/list")
public Object list() {
log.info("RestTemplate 测试远程调用.....");
return restTemplate.getForEntity(ICRMS_SERVER_ADDR + "/icrms/list", Object.class);
}
}
配置中心:
集中管理配置、配置动态更新、回滚配置…
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
#Nacos 配置中心配置
spring:
application:
name: aiops-personnel
cloud:
nacos:
config:
# nacos 地址
server-addr: 10.173.28.204:8848
aiops-icrms.properties aiops-icrms.yaml
总结: 只需要给Nacos中创建一个dataId名称叫做 当前项目名.properties 当前项目名.yaml
package com.moonpac.personnel.controller;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
/**
* @Copyright (C), 2016-2022 MP
* @ClassName: PersonnelParamsConfig
* @Author: hf
* @Date: 2022/9/4 17:16
* @Description:
*/
@Slf4j
@RestController
@RequestMapping("/personnel/params")
@RequiredArgsConstructor
//开启域动态刷新主类配置
@RefreshScope
public class PersonnelParamsConfigController {
@Value("${personnel.version:11}")
private String personnelVersion;
@Value("${personnel.content:11}")
private String personnelContent;
@ApiOperation("Nacos配置中心测试")
@GetMapping("/getParams")
public Object getP() {
HashMap<Object, Object> hashMap = new HashMap<>(3);
hashMap.put("content", personnelContent);
hashMap.put("version", personnelVersion);
return hashMap;
}
}
最佳实践:
使用namespace来区分不同的服务【每个服务都有自己的名称空间】。
使用Group来区分不同环境【dev,prod,test】
#Nacos 配置中心配置
spring:
application:
name: aiops-personnel
cloud:
nacos:
config:
# nacos 地址
server-addr: 10.173.28.204:8848
# 设置默认的文件类型
file-extension: yaml
# 设置命名空间--此处填写的是ID 重要!!!
# 设置命名空间--此处填写的是ID 重要!!!
# 设置命名空间--此处填写的是ID 重要!!!
namespace: 9dbdfc95-0174-49bd-9c2a-bc276ca41ecc
#Nacos 配置中心配置
spring:
application:
name: aiops-personnel
cloud:
nacos:
config:
# nacos 地址
server-addr: 10.173.28.204:8848
# 设置默认的文件类型
file-extension: yaml
# 设置命名空间--此处填写的是ID 重要!!!
namespace: 9dbdfc95-0174-49bd-9c2a-bc276ca41ecc
extension-configs:
# 批量加载配置文件
- {dataId: "application-monitor.yaml",group: "DEV",refresh: true}
- {dataId: "application-logback.yaml",group: "DEV",refresh: true}
- {dataId: "application-sentinel.yaml",group: "DEV",refresh: true}
- {dataId: "application-zipkin.yaml",group: "DEV",refresh: true}