1、安装与使用
2、注册服务到consul
3、调用consul上注册的服务
4、配置中心
1、安装与使用
官网地址:Install | Consul | HashiCorp Developer
下载对应的系统
解压缩后文件
打开命令行运行
consul.exe agent -dev
即可运行
可以编写一个bat脚本每次运行bat脚本就行,就不用每次cmd敲命令行了
@echo off
start "" "F:\tool\consul_1.18.2_windows_amd64\consul.exe" agent -dev
记得将路径改为自己的
运行成功后访问可视化控制台地址:localhost:8500
一个实例就是当前的consul服务
正常即表示启动成功
2、注册服务到consul
新建一个maven项目,父项目导入依赖(依赖版本注意对应关系,cloud和springboot版本需对应)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
新建子项目service-provider
导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
配置application.yml文件
spring:
application:
name: service-provider
cloud:
consul:
host: localhost
port: 8500
discovery:
register: true #是否需要注册
instance-id: ${spring.application.name}-01 #实例名称(必须唯一)
service-name: ${spring.application.name} #服务名称
port: ${server.port} #服务端口
prefer-ip-address: true #是否使用ip注册服务
ip-address: ${spring.cloud.client.ip-address} #请求服务ip地址
server:
port: 7070
ProductController
@RestController
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("get")
public Product get(){
Product products = productService.selectProduct();
return products;
}
}
ProductService
public interface ProductService {
Product selectProduct();
}
ProductServiceImpl
@Service
public class ProductServiceImpl implements ProductService {
@Override
public Product selectProduct() {
return new Product(1,"手机",200,1999.0);
}
}
Product
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product implements Serializable {
private Integer id;
private String productName;
private Integer productNum;
private Double productPrice;
}
@SpringBootApplication
public class ProviderApplication {
public static void main( String[] args ){
SpringApplication.run(ProviderApplication.class, args);
}
}
启动项目 访问测试接口成功
启动成功后查看localhost:8500
这个就是我们配置的。点进去service-provider-01是我们配置的instance-id
service-name: ${spring.application.name} #服务名称
3、调用consul上注册的服务
在第二步我们将service-provider注册到了consul上,接下来我们写个模块来调用他
新建service-consumer模块
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: 9090
spring:
application:
name: service-consumer
cloud:
consul:
host: localhost
port: 8500
discovery:
register: false #是否需要注册,false不用注册到consul上
OrderController
@RestController
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping("/{id}")
public Order selectOrder(@PathVariable("id") Integer id){
return orderService.selectOrderById(id);
}
}
OrderService
public interface OrderService {
Order selectOrderById(Integer id);
}
OrderServiceImpl(调用地址:http://service-provider/get)
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private RestTemplate restTemplate;
@Override
public Order selectOrderById(Integer id) {
return new Order(id, "order-001", "中国", 100D,
selectProductListByLoadBalancer());
}
//发起请求去调用service-provider模块
private Product selectProductListByLoadBalancer(){
ResponseEntity<Product> response = restTemplate.exchange(
"http://service-provider/get",
HttpMethod.GET,
null,
new ParameterizedTypeReference<Product>() {
});
Product body = response.getBody();
return body;
}
}
Order
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
private Integer id;
private String orderNo;
private String orderAddress;
private Double orderPrice;
private Product product;
}
Product
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Product {
private Integer id;
private String productName;
private Integer productNum;
private Double productPrice;
}
@SpringBootApplication
public class App {
public static void main( String[] args ){
SpringApplication.run(App.class, args);
}
//远程调用
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
启动项目 访问localhost:9090/1 测试一下
我们可以看到通过调用http://service-provider/get已经成功获取到了数据
4、配置中心
我们用service-provider模块举例 (测试从consul上获取端口和自定义参数)
给service-provider模块新加两个依赖(consul配置中心和bootstrap的读取)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.3</version>
</dependency>
将之前的application.yml改为bootstrap.yml。因为bootstrap优先级比application高
bootstrap.yml
spring:
application:
name: service-provider
cloud:
consul:
host: localhost
port: 8500
discovery:
register: true #是否需要注册
instance-id: ${spring.application.name}-01 #实例名称(必须唯一)
service-name: ${spring.application.name} #服务名称
port: ${server.port} #服务端口
prefer-ip-address: true #是否使用ip注册服务
ip-address: ${spring.cloud.client.ip-address} #请求服务ip地址
config:
enabled: true #是否开启配置中心
format: yaml #配置文件格式,这里用的yaml
profile-separator: "-" #例如: service-provider和dev中间的符号 用-就是service-provider-dev
data-key: data #默认的值就是data 是config的key 写上方便阅读
prefix: config #默认的值就是config 是配置的前缀 写上方便阅读
profiles:
active: dev
我们没有配置这个项目的端口号,从consul上获取
打开控制台配置文件新建
为啥这样命名?因为consul中默认前缀就是config,默认后面的key就是data。得加
test.num用来测试动态刷新
重新启动项目
可以看到使用7070端口启动,成功从consul上获取到了端口
我们再来测试一下动态获取配置数据
@RestController
@RefreshScope //动态刷新获取,如果不加修改获取不到最新的
public class ProductController {
@Autowired
private ProductService productService;
//获取测试的数据
@Value("${test.num}")
private String num;
@GetMapping("get")
public Product get(){
Product products = productService.selectProduct();
return products;
}
//测试的方法
@GetMapping("test")
public String test(){
return num;
}
}
重新启动 调用localhost:7070/test
获取成功,测试动态获取 修改num的值